Welcome Guest, Not a member yet? Register   Sign In
T_PAAMAYIM_NEKUDOTAYIM
#1

[eluser]4ever[/eluser]
I have a simple example:

Code:
<?php
class Site_model extends CI_Model {

  function get_all(){
   $data = null;
   $q = $this->db->get("table_name");  
   if ($q->num_rows > 0) echo "hi";
   foreach ($q->result() as row){ echo "";
   }
   $data[] = $row;
  return $data;  
  }
}

?>


Now - this causes error:

Code:
foreach ($q->result() as row){...}
Missing a colon? According tutorial this formula should work! Problem with error reporting setting? Where is problem...
#2

[eluser]osci[/eluser]
Code:
<?php
class Site_model extends CI_Model {

  function get_all(){
   $q = $this->db->get("table_name");  
   if ($q->num_rows > 0)
   {
      foreach ($q->result() as $row)
      {
         $data[] = $row;
      }
      return $data;  
  }
  return false;
}
?>

edit: corrected $row as stated below and removed echoes and $data=null added return false if no rows are returned
#3

[eluser]eoinmcg[/eluser]
Code:
foreach ($q->result() as row){...}

should be
Code:
foreach ($q->result() as $row){...}

note: $row not row
#4

[eluser]osci[/eluser]
true eoinmcg for $, i didn't noticed.
Still the code would return null the way it is written
#5

[eluser]4ever[/eluser]
Then

Code:
<?php
class Site_model extends CI_Model {
  function get_all(){
   $fdata = null; $row = null;
   $q = $this->db->get("table_name");  
   if ($q->num_rows > 0) foreach ($q->result() as $row) $fdata[] = $row;
  return $fdata;  
  }
}
?>

Why I can't declare var $row; but $row = null; ? 1st returns error. The mistake with row instead $row is infantile. I saw this report 1st time.

Thanks for reply.
#6

[eluser]osci[/eluser]
look at my example above
your foreach statement runs even if $q->num_row = 0
#7

[eluser]4ever[/eluser]
[quote author="osci" date="1305550264"]look at my example above
your foreach statement runs even if $q->num_row = 0[/quote]

No. I forgot to delete the echo. So it run always. Now repaired.
#8

[eluser]eoinmcg[/eluser]
tbh, your question is not very clear. from your first post the error was caused by the missing $

as for this;
Quote:Why I can’t declare var $row; but $row = null;

you cannot use var $row in the scope of a function. read here for more info: http://www.php.net/manual/en/language.oo...erties.php
#9

[eluser]4ever[/eluser]
Yes, I agree. My first question T_PAAMAYIM_NEKUDOTAYIM -> missed $.




Theme © iAndrew 2016 - Forum software by © MyBB