php - message undefined variable: news on view page -


i'm new php , codeigniter, , saw there many questions mentioning , been trying them nothing seems work. auto-loaded in configuration, database running , function posting database working writing view page doesn't work @ all. except displaying username, create new variable on view page.

controller

 public function shownews()  {      $data = array();      $this->load->model('user');      $data['news'] = $this->user->getnews();      $this->load->vars($data); } 

model

function getnews(){     $q = $this->db->get('news');     if($q->num_rows() > 0){         return $q->result();     }     return false; } 

view

<?php foreach($news $row) : ?>  <li><?php echo $row->title;  ?> </li> <li><?php echo $row->date; ?></li>        <?php endforeach; ?> 

this error edit ves news

a php error encountered  severity: notice  message: undefined variable: news  filename: admin/pocetna.php  line number: 64 php error encountered  severity: warning  message: invalid argument supplied foreach()  filename: admin/pocetna.php  line number: 64 

using wamp, netbeans codeigniter. saw has passing data controller view can't figure out , been trying few days already, having problems.

you're not passing $data view. controller should this:

public function show_news() {     $this->load->model('user');      $data = [];     $data['news'] = $this->user->get_news();      $this->load->view('news', $data); } 

your view should checking if $news false, because you'll have issues foreach if loop on value false. model should returning result_array not result, foreach cam't loop on objects..

public function get_news() {     $q = $this->db->get('news');      return($q->num_rows() > 0) ? $q->result_array() : false; } 

your view should this:

<?php     if($news !== false)     {         foreach($news $row)         {             echo "<li>{$row['title']}</li>";             echo "<li>{$row['date']}</li>";          }     }     else     {         echo "no news see here!";     } ?> 

your title doesn't link error in post, that's solution 1 in title.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -