php中使用mysql_fetch_array输出数组至页面中展示

时间:2023-01-02 17:46:51

用的是CI框架,很好的MVC结构

在Model层

     public function showProteinCategory(){
$sql = "SELECT DISTINCT protein_name FROM protein";
$result = mysql_query($sql);
$arr = array();
while ($res = mysql_fetch_array($result)){
$arr[] = array(
"protein_name" => $res['protein_name']
);
}
return $arr;
}

Controllers层

 $this->load->model("selectInfo_model");
$protein['show'] = $this->selectInfo_model->showProteinCategory();
// print_r($protein);
$this->load->view('information_select',$protein);

view层

 <label>蛋白质种类:</label>
<select name="bigname" id="bigname" style="width:450px">
<option value="">-- 请选择蛋白质种类--</option>
<?php if(is_array($show)) foreach($show as $r):?>
<option value="<?php echo $r['protein_name'] ?>"><?php echo $r['protein_name'] ?></option>
<?php endforeach;?>
</select>

那我们看看这里的$protein吧,到底是什么样的呢,我们到底怎样把握呢?看看输出的

Array
(
[show] => Array
(
[0] => Array
(
[protein_name] => 重组人乳铁蛋白
) [1] => Array
(
[protein_name] => 人乳铁蛋白
) ) )

这里注意的是mysql_fetch_array和mysql_fetch_object的区别

mysql_fetch_array中在页面中是这样显示的foreach($show as $r) {$r['protein_name']}

mysql_fetch_object中在页面中是这样显示的foreach($show as $r) {$r->protein_name}