Php inserts new characters at the specified position of the string, and php Strings specify characters
Because the project uses a DataTable table to load background data and needs to connect the table to query the selected policy status of the VM, it is expected to retrieve the policy table content first, to form a '<select> <option value = "1"> </option> [n options] </select>' string, when traversing the Virtual Machine list, the policy value is combined into a string like 'value = "1" 'and then reused.Explode ()AndImplode ()Function to form a new string and return it to the foreground.
1 $ option = '<select class = "sla_list">'; 2 3 // retrieve all policies 4 $ sla_query = $ this-> db-> select ('sla _ id, name ')-> get ('sla'); 5 $ sla_res = $ sla_query-> result_array (); 6 if (! Empty ($ sla_res) {7 $ option. = '<option value = "0"> unprotected </option>'; 8 foreach ($ sla_res as $ k1 => $ v1) {9 $ option. = '<option value = "'. $ v1 ['sla _ id']. '"> '. $ v1 ['name']. '</option>'; 10} 11 $ option. = '</select>'; 12 13} else {14 $ option. = '<option value = "0"> unprotected </option> </select> '; 15} 16 17 18 // select the default protection policy 19 $ vm_query = $ this-> db-> select ('sla _ id ') -> where ('vm _ id', $ vm_id)-> get ('Task _ vm ', 1); 20 $ vm_res = $ vm_query-> row_array (); 21 if (is_null ($ vm_res ['sla _ id']) $ res ['sla _ id'] = 0; 22 $ selected = 'value = "'. $ vm_res ['sla _ id']. '"'; 23 $ new_str_arr = explode ($ selected, $ option); 24 $ new_option = implode (" {$ selected} selected ", $ new_str_arr );