fastammo 发表于 2014-12-10 10:48:56

CI传值显示问题

要如何传id值到model?
test_model.php

public function test_modela(id)    {
$query = $this->db->query("SELECT id,nameFROM ec_free WHERE id=$id");
return $query->result_array();
}


test_controllers.php

        public function test_controllers()
        {
                $data['test_controllers'] = $this->test_model->test_model();
               
                $this->load->view('test_view', $data);
        }



test_view.php

<?php
foreach($test_controllers AS $row)
{
      if($id==$row['id'])
      {
            echo $row['name'];
      }
}
?>


Closer 发表于 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

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/

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);
}



public function test_model($id){
  $query = $this->db->query("SELECT id,nameFROM ec_free WHERE id=".$id."");
  if($query->num_rows() > 0){
    return $query->result_array();
  }
  return;
}

<?php
  for($x=0;$x<$max;$x++){//跟 foreach 還不熟, 於是用 for
    echo $test_controllers[$x]['name'];
  }
?>

Closer 发表于 2014-12-10 16:30:11

fastammo 发表于 2014-12-10 15:53
以前都是直接写一个function,直接带就可以,现在用CI感觉挺复杂的
這樣子試試?能的話 test02 的 name 改其他名稱
我沒測試過同樣的 name 會不會變成 name / name


//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']; //印出商品
  echo $test_controllers[$x]['name'];
}




fastammo 发表于 2014-12-10 15:02:00

抱歉,重新叙述问题,要做图这样的功能

[

//model
public function test01_modela()    {
$query = $this->db->query("SELECT id,nameFROM test01");
return $query->result_array();
}

public function test02_modela($id)    {
$query = $this->db->query("SELECT id,nameFROM 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印出?
}
?>




fastammo 发表于 2014-12-10 11:26:12

test_view.php呼叫方是因该是错误的,要怎么呼叫才对?

Hex 发表于 2014-12-10 13:46:57

$data['test_controllers'] = $this->test_model->test_model('id');

这样不就可以传 ID 了吗,就是普通方法调用啊。

fastammo 发表于 2014-12-10 14:04:07


      public function test_controllers()
      {
                //改这样吗?
                $data['test_controllers'] = $this->test_model->test_model('id');
               
                $this->load->view('test_view', $data);
      }

test_view.php这样写没问题吗?

Closer 发表于 2014-12-10 15:27:51

本帖最后由 Closer 于 2014-12-10 15:30 编辑

等等好像不太對

fastammo 发表于 2014-12-10 15:53:37

以前都是直接写一个function,直接带就可以,现在用CI感觉挺复杂的
<?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]);
}
?>

fastammo 发表于 2014-12-10 16:45:16

db没有用join是因为原始码已经做太多join改不了,所以才用这种方式写
页: [1] 2
查看完整版本: CI传值显示问题