用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3085|回复: 16
收起左侧

[HELP] CI传值显示问题

[复制链接]
发表于 2014-12-10 10:48:56 | 显示全部楼层 |阅读模式
要如何传id值到model?
test_model.php
PHP复制代码
 
 public function test_modela(id)    {
 $query = $this->db->query("SELECT id,name  FROM ec_free WHERE id=$id");
 return $query->result_array();
 }
 
复制代码


test_controllers.php
PHP复制代码
 
        public function test_controllers()
        {
                $data['test_controllers'] = $this->test_model->test_model();
               
                $this->load->view('test_view', $data);
        }
 
 
复制代码


test_view.php
PHP复制代码
 
<?php
foreach($test_controllers AS $row)
{
      if($id==$row['id'])
      {
              echo $row['name'];
      }
 }
?>
 
 
复制代码

发表于 2014-12-10 14:29:38 | 显示全部楼层
本帖最后由 Closer 于 2014-12-10 14:45 编辑

正確來說,取用順序為:
網址 => controller => model (如果有使用) => view

http://127.0.0.1/CI/index.php/test_controllers
假設這是進入點,那對他第一時間用PHP傳值有兩種

1. GET
http://127.0.0.1/CI/index.php/test_controllers?id=15

PHP复制代码
public function test_controllers(){
  $id = $this->input->get('id', TRUE);
  $data['test_controllers'] = $this->test_model->test_model($id);
  $data['max'] = count($data['test_controllers']);
  $this->load->view('test_view', $data);
}
复制代码


2. CI參數
http://127.0.0.1/CI/index.php/test_controllers/15/

PHP复制代码
public function test_controllers($id){
  $data['test_controllers'] = $this->test_model->test_model($id);
  $data['max'] = count($data['test_controllers']);
  $this->load->view('test_view', $data);
}
复制代码



PHP复制代码
public function test_model($id){
  $query = $this->db->query("SELECT id,name  FROM ec_free WHERE id=".$id."");
  if($query->num_rows() > 0){
    return $query->result_array();
  }
  return;
}
复制代码

PHP复制代码
<?php
  for($x=0;$x<$max;$x++){  //跟 foreach 還不熟, 於是用 for
    echo $test_controllers[$x]['name'];
  }
?>
复制代码
发表于 2014-12-10 16:30:11 | 显示全部楼层
fastammo 发表于 2014-12-10 15:53
以前都是直接写一个function,直接带就可以,现在用CI感觉挺复杂的

這樣子試試?能的話 test02 的 name 改其他名稱
我沒測試過同樣的 name 會不會變成 name[0] / name[1]


PHP复制代码
 
//model
public function test_model(){
  $query = $this->db->join('test02','test02.id = test01.id','left')
           ->select('test01.id, test01.name, test02.name')
           ->get('test01');
 
  if($query->num_rows() > 0){
    return $query->result_array();
  }
  return;
}
 
//controllers
public function test_controllers(){
  $data['test_controllers'] = $this->test_model->test_model();
  $data['max_test01'] = count($data['test_controllers']);
 
  $this->load->view('test_view', $data);
}
 
//view
for($x=0;$x<$max_test01;$x++){
  echo $test_controllers[$x]['name'][0]; //印出商品
  echo $test_controllers[$x]['name'][1];
}
 
 
 
复制代码


 楼主| 发表于 2014-12-10 15:02:00 | 显示全部楼层
抱歉,重新叙述问题,要做图这样的功能

test.png [
PHP复制代码
 
//model
public function test01_modela()    {
 $query = $this->db->query("SELECT id,name  FROM test01");
 return $query->result_array();
}
 
public function test02_modela($id)    {
 $query = $this->db->query("SELECT id,name  FROM test02 WHERE id=$id");
 return $query->result_array();
}
 
//controllers
public function test01_controllers()
{
  $data['test01_controllers'] = $this->test_model->test01_model();
  $this->load->view('test_view', $data);
}
 
public function test02_controllers($id)
{
  $data['test02_controllers'] = $this->test_model->test02_model($id);  
  $this->load->view('test_view', $data);
}
 
//view
<?php
foreach($test01_controllers AS $row1)
{
        echo $row1['name']; //印出商品
        //怎么呼叫test02_controllers印出?
}
?>
 
 
 
复制代码


 楼主| 发表于 2014-12-10 11:26:12 | 显示全部楼层
test_view.php呼叫方是因该是错误的,要怎么呼叫才对?
发表于 2014-12-10 13:46:57 | 显示全部楼层
$data['test_controllers'] = $this->test_model->test_model('id');

这样不就可以传 ID 了吗,就是普通方法调用啊。
 楼主| 发表于 2014-12-10 14:04:07 | 显示全部楼层
PHP复制代码
 
        public function test_controllers()
        {
                //改这样吗?
                $data['test_controllers'] = $this->test_model->test_model('id');
               
                $this->load->view('test_view', $data);
        }
 
复制代码

test_view.php这样写没问题吗?
发表于 2014-12-10 15:27:51 | 显示全部楼层
本帖最后由 Closer 于 2014-12-10 15:30 编辑

等等  好像不太對
 楼主| 发表于 2014-12-10 15:53:37 | 显示全部楼层
以前都是直接写一个function,直接带就可以,现在用CI感觉挺复杂的
PHP复制代码
<?php
public function test02($id){
  $str="SELECT name FROM test02 WHERE id=$id";
  $result=mysql_query($str);
  list($name)=mysql_fetch_row($result);
 
  return $name
}
?>
<?php
  $query = "SELECT * FROM test01 ";
  $result = mysql_query($query) or die("cannot connect to table" . mysql_error( ));
  while ( $row = mysql_fetch_array($result) ) {
{
        echo test02($row1[$id]);
}
?>
复制代码
 楼主| 发表于 2014-12-10 16:45:16 | 显示全部楼层
db没有用join是因为原始码已经做太多join改不了,所以才用这种方式写

本版积分规则