0

I'm trying to display the content of the fields of my "projects" table on the index page but cant seem to get it working. What am I doing wrong here?

Model:

public function prodView() {
            $sql = 'SELECT * FROM projects';
            $query = $this->db->query($sql);
            return $query->result();
}

Controller:

public function index() {
         $this->load->model('main_model');
         $data ['query'] = $this->main_model->prodView(); 
         $this->load_view('/index', $data);
}

View:

<?php foreach($query as $row): ?>
  <tr> 
    <td><?php echo $row->project_name; ?></td>
  </tr>
<?php endforeach; ?>
8
  • 1
    What is going wrong? What happens when you run the above code? Commented Sep 15, 2016 at 17:55
  • Nothing is being outputted. @DFriend Commented Sep 15, 2016 at 17:57
  • 1
    But no error displays? Commented Sep 15, 2016 at 17:58
  • no its just blank. Commented Sep 15, 2016 at 17:59
  • Try this. Before the foreach line of code add this line var_dump($query); Commented Sep 15, 2016 at 18:01

1 Answer 1

1

Model

public function prodView() {
  $result = array();
  $this->db->select("*")->from("projects");
  $query = $this->db->get();
  if($query->num_rows() > 0){
     $result = $query->result_array();
  }
  return $result;
}

Controller

public function index() {
   $this->load->model('main_model');
   $data ['query'] = $this->main_model->prodView(); 
   $this->load->view('missio/index', $data);
}

View

<?php foreach($query as $row): ?>
  <tr> 
    <td><?php echo $row['project_name']; ?></td>
  </tr>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

18 Comments

try to print_r($query) or var_dump($query) check is this is empty or not
That syntax would OK be if you used return $query->result_array(); But you did not.
also check view path $this->load_view('/index', $data); it should be $this->load_view('index', $data); or $this->load_view('FOLDER_NAME/index', $data);
@DFriend you are correct my friend but if no result then foreach will not run
var_dump($query) returned null
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.