1

I'm just starting in CodeIgniter and can't get my data to display in the view. This to my (very limited) understanding should display the first, second and third columns of the table in the view but it returns Undefined variable: first. any help would be greatly appreciated thanks.

my model:

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

my controller:

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

my view:

...
    //    <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>
...
2
  • better use codeigniter inbuilt function "$this->db->like();" Commented Nov 17, 2016 at 9:26
  • 1
    Did my answer helps you?, if so, don't forget to mark it as correct ;) Commented Nov 17, 2016 at 16:13

2 Answers 2

1

You need to change this:

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

To this:

public function view($string) {
        $this->load->model('Model_model');
        $result = $this->Model_model-> getData($string);
        $data["myModel"] = $result;
        $this->load->view('view_display', $data);
 }

Then in your 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>

UPDATE

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm glad to help @NeilSimmons
sorry to bug you but do you know how to get this to work? $sql = "SELECT user_username,text,posted_at FROM Messages WHERE text LIKE '%?%';"; //new line $query = $this->db->query($sql,$string); id like the $string to be passed to the ? which is supposed to be a place holder instead of it searching for the ?
@NeilSimmons use this $query = $this->db->select(user_username,text,posted_at)->like('text',$string);
1
 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();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.