PHP判断字符类型 php 在字符串指定位置插入新字符

WBOY
发布: 2016-07-28 08:28:55
原创
1021 人浏览过

  因为项目用到DataTable表格加载后台数据,要连表查询虚拟机选中的策略状态,所以想到先把策略表内容取出来,组成一个''字符串,在遍历虚拟机列表时把他的策略值拼成 'value="1"' 这样的字符串,再利用explode()implode() 函数,组成新的字符串返回给前台,就实现了选中状态。

<span><span> 1    </span><span>$option</span> = '<select>'<span>;
</span><span> 2    </span><span> 3    </span><span>//</span><span> 取出所有策略</span><span> 4    </span><span>$sla_query</span> = <span>$this</span>->db->select('sla_id, name')->get('sla'<span>);
</span><span> 5</span><span>$sla_res</span> = <span>$sla_query</span>-><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> .= '<option value="0">未保护</option>'<span>;
</span><span> 8</span><span>foreach</span> (<span>$sla_res</span><span>as</span><span>$k1</span> => <span>$v1</span><span>) {
</span><span> 9</span><span>$option</span> .= '<option value="' . <span>$v1</span>['sla_id'] . '">' . <span>$v1</span>['name'] . '</option>'<span>;
</span><span>10</span><span>            }
</span><span>11</span><span>$option</span> .= '</select>'<span>;
</span><span>12</span><span>13</span>         } <span>else</span><span> {
</span><span>14</span><span>$option</span> .= '<option value="0">未保护</option>'<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>->db->select('sla_id')->where('vm_id', <span>$</span>vm_id)->get('task_vm', 1<span>);
</span><span>20</span><span>$vm_res</span> = <span>$vm_query</span>-><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>
登录后复制

以上就介绍了PHP判断字符类型 php 在字符串指定位置插入新字符,包括了PHP判断字符类型方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!