用的是CI框架,很好的MVC结构
在Model层
1 public function showProteinCategory(){ 2 $sql = "SELECT DISTINCT protein_name FROM protein"; 3 $result = mysql_query($sql); 4 $arr = array(); 5 while ($res = mysql_fetch_array($result)){ 6 $arr[] = array( 7 "protein_name" => $res['protein_name'] 8 ); 9 } 10 return $arr; 11 }
Controllers层
1 $this->load->model("selectInfo_model"); 2 $protein['show'] = $this->selectInfo_model->showProteinCategory(); 3 // print_r($protein); 4 $this->load->view('information_select',$protein);
view层
1 <label>蛋白质种类:</label> 2 <select name="bigname" id="bigname" style="450px"> 3 <option value="">-- 请选择蛋白质种类--</option> 4 <?php if(is_array($show)) foreach($show as $r):?> 5 <option value="<?php echo $r['protein_name'] ?>"><?php echo $r['protein_name'] ?></option> 6 <?php endforeach;?> 7 </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}