如何使用php填充db的<select>下拉列表

时间:2017-09-18 17:37:12

标签: php opencart

我在OpenCart中工作,我正在尝试在我的表单中创建一个动态下拉列表。我无法从db填充它。我一直收到这个错误:   警告:非法字符串偏移&#39;名字&#39;位于第507行的/var/www/html/Acendia-bit-bucket/vK23o003xdkxLp2-09zxkjekzcDt/controller/campaigns/campaign.php 我真的只想要一个下拉列表,显示该表中每个人的名字和姓氏。 模型: public function getCustomers(){ $ query = $ this-&gt; db-&gt; query(&#34; SELECT customer_id,firstname,lastname FROM&#34; .DB_PREFIX。&#34; customer&#34;); return $ query-&gt; result_array(); //返回$ query-&gt;行; } 控制器:(控制器的全部功能) protected function getForm(){     $ data [&#39; heading_title&#39;] = $ this-&gt; language-&gt; get(&#39; heading_title&#39;);     $ data [&#39; text_form&#39;] =!isset($ this-&gt; request-&gt; get [&#39; event_id&#39;])? $ this-&gt; language-&gt; get(&#39; text_add&#39;):$ this-&gt; language-&gt; get(&#39; text_edit&#39;);     $ data [&#39; text_add_component&#39;] = $ this-&gt; language-&gt; get(&#39; text_add_component&#39;);     $ data [&#39; entry_attending&#39;] = $ this-&gt; language-&gt; get(&#39; entry_attending&#39;);     $ data [&#39; entry_attended&#39;] = $ this-&gt; language-&gt; get(&#39; entry_attended&#39;);     $ data [&#39; entry_interested&#39;] = $ this-&gt; language-&gt; get(&#39; entry_interested&#39;);     $ data [&#39; entry_date&#39;] = $ this-&gt; language-&gt; get(&#39; entry_date&#39;);     $ data [&#39; entry_campaign_start_date&#39;] = $ this-&gt; language-&gt; get(&#39; entry_campaign_start_date&#39;);     $ data [&#39; entry_campaign_end_date&#39;] = $ this-&gt; language-&gt; get(&#39; entry_campaign_end_date&#39;);     $ data [&#39; entry_owner&#39;] = $ this-&gt; language-&gt; get(&#39; entry_owner&#39;);     $ data [&#39; entry_goal&#39;] = $ this-&gt; language-&gt; get(&#39; entry_goal&#39;);     $ data [&#39; entry_goal_count&#39;] = $ this-&gt; language-&gt; get(&#39; entry_goal_count&#39;);     $ data [&#39; text_publish&#39;] = $ this-&gt; language-&gt; get(&#39; text_publish&#39;);     $ data [&#39; text_preview&#39;] = $ this-&gt; language-&gt; get(&#39; text_preview&#39;);     $ data [&#39; text_cancel&#39;] = $ this-&gt; language-&gt; get(&#39; text_cancel&#39;);     $ data [&#39; text_save&#39;] = $ this-&gt; language-&gt; get(&#39; text_save&#39;);     $ data [&#39; text_filter_type&#39;] = $ this-&gt; language-&gt; get(&#39; text_filter_type&#39;);     $ data [&#39; entry_name&#39;] = $ this-&gt; language-&gt; get(&#39; entry_name&#39;);     $ data [&#39; button_publish&#39;] = $ this-&gt; language-&gt; get(&#39; button_publish&#39;);     $ data [&#39; button_preview&#39;] = $ this-&gt; language-&gt; get(&#39; button_preview&#39;);     $ data [&#39; button_save&#39;] = $ this-&gt; language-&gt; get(&#39; button_save&#39;);     $ data [&#39; button_cancel&#39;] = $ this-&gt; language-&gt; get(&#39; button_cancel&#39;);     if(isset($ this-&gt; error [&#39; warning&#39;])){         $ data [&#39; error_warning&#39;] = $ this-&gt;错误[&#39;警告&#39;];     } else {         $ data [&#39; error_warning&#39;] =&#39;&#39 ;;     }     if(isset($ this-&gt; error [&#39; campaign_id&#39;])){         $ data [&#39; error_campaign_id&#39;] = $ this-&gt;错误[&#39; campaign_id&#39;];     } else {         $ data [&#39; error_campaign_id&#39;] =&#39;&#39 ;;     }     if(isset($ this-&gt; error [&#39; campaign_name&#39;])){         $ data [&#39; error_campaign_name&#39;] = $ this-&gt;错误[&#39; campaign_name&#39;];     } else {         $ data [&#39; error_campaign_name&#39;] =&#39;&#39 ;;     }     if(isset($ this-&gt; error [&#39; campaign_giving_goal&#39;])){         $ data [&#39; error_campaign_giving_goal&#39;] = $ this-&gt;错误[&#39; campaign_giving_goal&#39;];     } else {         $ data [&#39; error_campaign_giving_goal&#39;] =&#39;&#39 ;;     }     if(isset($ this-&gt; error [&#39; campaign_giving_count_goal&#39;])){         $ data [&#39; error_campaign_giving_count_goal&#39;] = $ this-&gt;错误[&#39; campaign_giving_count_goal&#39;];     } else {         $ data [&#39; error_campaign_giving_count_goal&#39;] =&#39;&#39 ;;     }     if(isset($ this-&gt; error [&#39; campaign_owner&#39;])){         $ data [&#39; error_campaign_owner&#39;] = $ this-&gt;错误[&#39; campaign_owner&#39;];     } else {         $ data [&#39; error_campaign_owner&#39;] =&#39;&#39 ;;     }     $ url =&#39;&#39 ;;     if(isset($ this-&gt; request-&gt; get [&#39; filter_name&#39;])){         $ url。=&#39;&amp; filter_name =&#39; 。 urlencode(html_entity_decode($ this-&gt; request-&gt; get [&#39; filter_name&#39;],ENT_QUOTES,&#39; UTF-8&#39;));     }     if(isset($ this-&gt; request-&gt; get [&#39; sort&#39;])){         $ url。=&#39;&amp; sort =&#39; 。 $这个 - &GT;请求 - &GT;得到[&#39;排序&#39;];     }     if(isset($ this-&gt; request-&gt; get [&#39; order&#39;])){         $ url。=&#39;&amp; order =&#39; 。 $这 - &GT;请求 - &GT;得到[&#39;为了&#39;];     }     if(isset($ this-&gt; request-&gt; get [&#39; page&#39;])){         $ url。=&#39;&amp; page =&#39; 。 $这个 - &GT;请求 - &GT;得到[&#39;页面&#39;];     }     if(!isset($ this-&gt; request-&gt; get [&#39; event_id&#39;])){         $ data [&#39; action&#39;] = $ this-&gt; url-&gt; link(&#39; campaign / campaign / add&#39;,&#39; token =&#39;。$ this - &gt; session-&gt; data [&#39; token&#39;]。$ url,true);     } else {         $ data [&#39; action&#39;] = $ this-&gt; url-&gt; link(&#39; campaign / campaign / edit&#39;,&#39; token =&#39;。$ this - &gt;会话 - &gt;数据[&#39;令牌&#39;]。&#39;&amp; event_id =&#39;。$ this-&gt; request-&gt; get [&#39; event_id&#39 ;]。$ url,true);     }     $ getCustomers = $ this-&gt; model_campaigns_campaign-&gt; getCustomers();     foreach($ getCustomers as $ customers){         $ data [&#39; customer_list&#39;] [] =数组(             &#39; customer_firstname&#39; =&GT; $客户[&#39;姓名&#39;]         );     }     $ data [&#39; breadcrumbs&#39;] = array();     $ data [&#39; breadcrumbs&#39;] [] =数组(         &#39;文本&#39; =&GT; $这个 - &GT;语言 - &GT;获得(&#39; text_home&#39;),         &#39; HREF&#39; =&GT; $ this-&gt; url-&gt; link(&#39; common / dashboard&#39;,&#39; token =&#39;。$ this-&gt; session-&gt; data [&#39; token&# 39;],真实)     );     $ data [&#39; breadcrumbs&#39;] [] =数组(         &#39;文本&#39; =&GT; $这个 - &GT;语言 - &GT;获得(&#39; HEADING_TITLE&#39;),         &#39; HREF&#39; =&GT; $ this-&gt; url-&gt;链接(&#39;广告系列/广告系列&#39;,&#39;令牌=&#39;。$ this-&gt;会话 - &gt;数据[&#39;令牌&# 39;]。$ url,true)     );     $ data [&#39;取消&#39;] = $ this-&gt; url-&gt;链接(&#39;广告系列/广告系列&#39;,&#39;令牌=&#39;。$ this-&gt ; session-&gt; data [&#39; token&#39;]。$ url,true);     $ data [&#39; token&#39;] = $ this-&gt; session-&gt; data [&#39; token&#39;];     $这 - &GT;负载&GT;模型(&#39;定位/语言&#39);     $ data [&#39; languages&#39;] = $ this-&gt; model_localisation_language-&gt; getLanguages();     $ data [&#39; header&#39;] = $ this-&gt; load-&gt; controller(&#39; common / header&#39;);     $ data [&#39; column_left&#39;] = $ this-&gt; load-&gt; controller(&#39; common / column_left&#39;);     $ data [&#39; footer&#39;] = $ this-&gt; load-&gt; controller(&#39; common / footer&#39;);     $ this-&gt; response-&gt; setOutput($ this-&gt; load-&gt; view(&#39; campaigns / campaign_form&#39;,$ data)); } 视图:    &lt; div class =&#34; col-sm-5 col-sm-push-1 form-group required&gt;                   &lt; label class =&#34; col-sm-2 control-label&#34; for =&#34; input-campaign-owner&#34;&gt;&lt;?php echo $ entry_owner; ?&GT;&LT; /标签&gt;                 &lt; select name =&#34; customer-list&#34; ID =&#34;顾客列表&#34; &GT;                   &LT; PHP                 foreach($ customer_list as $ list){                  echo&#34;&lt; option value =&#39;&#39;&gt; $ list [&#39; customer_firstname&#39;]&lt; / option&gt;&#34 ;;                 }                 ?&GT;                   &LT; /选择&GT;                 &LT; / DIV&GT;

2 个答案:

答案 0 :(得分:1)

我认为它在模型中应该是$query->rows;,你只需要在控制器中$data['customers'] = $this->model_campaigns_campaign->getCustomers();,然后在视图中显示它:

<div class="col-sm-5 col-sm-push-1 form-group required">
    <label class="col-sm-2 control-label" for="input-campaign-owner"><?php echo $entry_owner; ?></label>
    <select name="customer-list" id="customer-list">
    <?php foreach ($customers as $customer) { ?>
        <option value="<?php echo $customer['customer_id']; ?>"><?php echo $customer['firstname'] . ' ' . $customer['lastname']; ?></option>
    <?php } ?>
    </select>
</div>

答案 1 :(得分:0)

我认为问题在于你在回声中的引用。此外,echo语句中有/bin/sh: 1: |ew2017: not found `enter code here`Traceback (most recent call last): File "<stdin>", line 4, in <module> File "/usr/lib/python3.5/subprocess.py", line 626, in check_output **kwargs).stdout File "/usr/lib/python3.5/subprocess.py", line 708, in run output=stdout, stderr=stderr) subprocess.CalledProcessError: Command '['nmcli d wifi connect abcnetwork password Pass|\\|ew2017;']' returned non-zero exit status 127 <?php个标记,这些标记不是必需的。请尝试以下操作,注意对echo语句所做的修改:

?>

此外,您的模型中<select name="customer-list" id="customer-list" > <?php foreach ($customer_list as $list){ echo "<option value=''> $list['customer_firstname'] </option>"; } ?> 可能应为return $query->row;