Table of Contents
php inserts new characters at the specified position in the string, and php string specifies the characters
Home Backend Development PHP Tutorial php inserts new characters at the specified position in the string, php string specifies the characters_PHP tutorial

php inserts new characters at the specified position in the string, php string specifies the characters_PHP tutorial

Jul 12, 2016 am 08:49 AM
string

php inserts new characters at the specified position in the string, and php string specifies the characters

Since the project uses the DataTable table to load background data, it is necessary to query the selected policy status of the virtual machine through the table , so I thought of taking out the contents of the policy table first and forming a '<select><option value="1"></option>[n options]</select>' string, and then traversing the virtual When machine list, spell his strategy value into a string like 'value="1"', and then use explode() and implode() function composes a new string and returns it to the front desk, thus realizing the selected state.

<span><span> 1    </span><span>$option</span> = '&lt;select class="sla_list"&gt;'<span>;
</span><span> 2    </span> 
<span> 3    </span><span>//</span><span> 取出所有策略</span>
<span> 4    </span><span>$sla_query</span> = <span>$this</span>-&gt;db-&gt;select('sla_id, name')-&gt;get('sla'<span>);
</span><span> 5</span>         <span>$sla_res</span> = <span>$sla_query</span>-&gt;<span>result_array();
</span><span> 6</span>         <span>if</span> (!<span>empty</span>(<span>$sla_res</span><span>)) {
</span><span> 7</span>             <span>$option</span> .= '&lt;option value="0"&gt;未保护&lt;/option&gt;'<span>;
</span><span> 8</span>             <span>foreach</span> (<span>$sla_res</span> <span>as</span> <span>$k1</span> =&gt; <span>$v1</span><span>) {
</span><span> 9</span>                 <span>$option</span> .= '&lt;option value="' . <span>$v1</span>['sla_id'] . '"&gt;' . <span>$v1</span>['name'] . '&lt;/option&gt;'<span>;
</span><span>10</span> <span>            }
</span><span>11</span>             <span>$option</span> .= '&lt;/select&gt;'<span>;
</span><span>12</span> 
<span>13</span>         } <span>else</span><span> {
</span><span>14</span>             <span>$option</span> .= '&lt;option value="0"&gt;未保护&lt;/option&gt;&lt;/select&gt;'<span>;
</span><span>15</span> <span>        }
</span><span>16</span> 
<span>17</span> 
<span>18    </span><span>//</span><span> 选中默认的保护策略</span>
<span>19    </span><span>$vm_query</span> = <span>$this</span>-&gt;db-&gt;select('sla_id')-&gt;where('vm_id', <span>$</span>vm_id)-&gt;get('task_vm', 1<span>);
</span><span>20</span>    <span>$vm_res</span> = <span>$vm_query</span>-&gt;<span>row_array();
</span><span>21    </span><span>if</span> (<span>is_null</span>(<span>$vm_res</span>['sla_id'])) <span>$res</span>['sla_id'] = 0<span>;
</span><span>22    </span><span>$selected</span> = 'value="' . <span>$vm_res</span>['sla_id'] . '"'<span>;
</span><span>23    </span><span>$new_str_arr</span> = <span>explode</span>(<span>$selected</span>, <span>$option</span><span>);
</span><span>24</span>    <span>$new_option</span> = <span>implode</span>(" {<span>$selected</span>} selected", <span>$new_str_arr</span>);</span>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1137779.htmlTechArticlephp Insert new characters at the specified position in the string, php string specifies the characters because the project uses the DataTable table to load background data , we need to connect the table to query the policy status selected by the virtual machine, so we want to...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

How to determine whether a Golang string ends with a specified character

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang?

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

How to repeat a string in python_python repeating string tutorial

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP string manipulation: a practical way to effectively remove spaces

PHP techniques for deleting the last two characters of a string PHP techniques for deleting the last two characters of a string Mar 23, 2024 pm 12:18 PM

PHP techniques for deleting the last two characters of a string

See all articles