【问题标题】:CI, How to get data call by id from database?CI,如何通过 id 从数据库中获取数据调用?
【发布时间】:2017-05-19 03:06:38
【问题描述】:

+ 这是我的模特:

public function call_by_id($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->result();
    }

+ 这是我的控制器:

public function index()
    {   
        $data = array();
        $w = '';
        $data['call_id'] = $this->mo_product->call_by_id($w);

        $this->load->view('...', $data);
    }

+ 我想调用如下视图:

$call_id[$id]->username;

【问题讨论】:

  • 是的,应该是这样的。但是如何从表中获取一些数据。我的目的是想通过 id 来调用它。但我不知道编码它?帮助 !!!谢谢

标签: codeigniter


【解决方案1】:
  • 在模型中,您将 $where 放在了错误的位置。把它作为参数 => call_by_id($where)

  • 在控制器中,$w 应该是一个整数,就像你的 id 值一样,例如:$w = 1;。而且你不需要写$data = array()

+ 这是我的模特:

public function call_by_id($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('id', $where);
        $query = $this->db->get();
        return $query->result();
    }

+ 这是我的控制器:

public function index()
    {   
        $w = 1; //we need to set value for spesific id
        //we can only call the model's function in controller 
        $data['call_id'] = $this->mo_product->call_by_id($w);

        $this->load->view('...', $data);
    }

+ 我想调用如下视图:

// view is just to show the data after we call the model's function in controller
print_r($call_id);

试试吧,希望对你有帮助:)

【讨论】:

  • 感谢您的帮助。但它仍然不起作用然后我跟着你。请帮助我更多?谢谢
  • 从哪里获得$w 值?
  • 我只是从 codeigniter 开始。所以我对此不太了解。然后我向您展示了我的代码,我只想从视图上的语法中获取数据。例如:$call_id['5']->product_name;我只是想要这样。谢谢
  • 我怎样才能得到它?
  • 您的意思是要在视图中调用call_by_id()
【解决方案2】:

试试这样的

public function call_by_id($where){
    $this->db->where('your_column_name', $where);
    $query = $this->db->get($this->table);
    return $query->result_array();
}

在控制器上

public function index()
{   

    $data['results'] = array();

    $w = '1'; // some id

    $data['results'] = $this->mo_product->call_by_id($w);

    $this->load->view('...', $data);
}

查看

foreach ($results as $result) {
    echo $result['something_from_database'];
}

只更新一个值使用 row_array();

https://www.codeigniter.com/user_guide/database/results.html#CI_DB_result::row_array

型号

public function call_by_id($where){
    $this->db->where('your_column_name', $where);
    $query = $this->db->get($this->table);
    return $query->row_array();
}

控制器

public function index()
{   

    $w = '1'; // some id

    $caller_data = $this->mo_product->call_by_id($w);

    $data['caller_id'] = $caller_data['caller_id'];

    $this->load->view('...', $data);
}

查看

<?php echo $caller_id;?>

【讨论】:

  • OK 在这种情况下,如果我有 5 或 6 个代码,只显示一个值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多