[英]CodeIgniter, displaying data from a sql search in html
我剛剛開始使用 CodeIgniter,無法在視圖中顯示我的數據。 這對我(非常有限)的理解應該在視圖中顯示表的第一、第二和第三列,但它返回未定義的變量:第一。 任何幫助將不勝感激,謝謝。
我的模型:
<?php class Model_model extends CI_model{
public function __construct() {
parent::__construct();
$this->load->database();
}
public function getData($string){
$sql = "SELECT first,second,third FROM Table WHERE first LIKE '%?%';";
$query = $this->db->query($sql,$string);
if ($query->num_rows() > 0) {
return $query;
}
我的控制器:
class User extends CI_Controller {
public function view($string) {
$this->load->model('Model_model');
$result = $this->Model_model-> getData($string);
echo $result->num_rows(); //returns correct number.
$this->load->view('view_display', $result);
}
}
我的看法:
...
// <table>
<tr><td>Poster:</td><td><?php echo $first; ?></td></tr>
<tr><td>Message:</td><td><?php echo $second; ?></td></tr>
<tr><td>Time posted:</td><td><?php echo $thrid; ?></td></tr>
// </table>
...
你需要改變這個:
public function view($string) {
$this->load->model('Model_model');
$result = $this->Model_model-> getData($string);
echo $result->num_rows(); //returns correct number.
$this->load->view('view_display', $result);
}
對此:
public function view($string) {
$this->load->model('Model_model');
$result = $this->Model_model-> getData($string);
$data["myModel"] = $result;
$this->load->view('view_display', $data);
}
然后在您的View
:
<tr><td>Poster:</td><td><?php echo $myModel->first; ?></td></tr>
<tr><td>Message:</td><td><?php echo $myModel->second; ?></td></tr>
<tr><td>Time posted:</td><td><?php echo $myModel->third; ?></td></tr>
public function getData($string){
$this->db->select("first,second,third");
$this->db->from("Table");
$this->db->like("first", $string);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query;
}
}
public function search(){ // Controller
$keyword = $this->input->get('keyword'); // You can use "post" as well. keyword is the input field name.
$data['results'] = $this->your_model->function($keyword);
$this->load->view('your_view', $data); }
public function search($keyword){ // Model
$this->db->like('keyword', $keyword);
$query = $this->db->get('table_name');
return $query->result();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.