Welcome Guest, Not a member yet? Register   Sign In
Why is foreach not working?
#1

[eluser]Kraig[/eluser]
Do you have any idea why this foreach would not work? If I just echo $this->variable it displays the last index only..however I want to display it all, but I get an error 'Message: Invalid argument supplied for foreach()'

Code:
$var = $this->variable; //$this->variable is a global array and it does work
foreach($var as $yup){
  echo $yup;
}
#2

[eluser]Aken[/eluser]
Well if you're echoing $this->variable and it's not showing an array, then it can't be used in a foreach(). Use var_dump() to see what it actually is, and figure out why it's not what you expect.
#3

[eluser]Kraig[/eluser]
I did that and this is what I get:
Code:
string(10) "7-mini.jpg"

I tested it out with the following array and it worked fine, so it's something with my variable
Code:
$x=array("one","two","three");

Here's how I declare it:
Code:
public $variable;

function  __construct() {
  parent::__construct();
  // Grabbing all the files associated with the current user
  $this->load->model("upload_model");
  $q = $this->upload_model->get_files();
        $this->variable = $q;
    }
#4

[eluser]Aken[/eluser]
Well if you know where it's declared, figure out why that function is not returning an array!
#5

[eluser]Kraig[/eluser]
I've been messing around with this for awhile now...could it be something with my query results?
Code:
function get_files()
{
  $userID = $this->session->userdata('id');  
  $query = $this->db->query('SELECT * FROM upload WHERE userID = '.$userID);
  $num_row = $query->num_rows();
  $val = $query->row_array();

  $test ="";
  foreach ($query->result_array() as $row)
  {
     $test .= $row['id'];
     $test .= $row['name'];
  }
  return $test;
}
#6

[eluser]xtremer360[/eluser]
That's probably because all you need to do is return $val you do not need this:

Code:
$test ="";
  foreach ($query->result_array() as $row)
  {
     $test .= $row['id'];
     $test .= $row['name'];
  }
  return $test;

Should be:
Code:
...
$val = $query->row_array();
return $val;
#7

[eluser]Kraig[/eluser]
When I do this:
Code:
foreach ($test_var as $value)
{
  echo $value . "<br />";
}

Model:
Code:
function get_files()
{
  $userID = $this->session->userdata('id');  
  $query = $this->db->query('SELECT * FROM upload WHERE userID = '.$userID);
  $num_row = $query->num_rows();
  $val = $query->row_array();

  return $val;
  
}

It only shows one result...there's more than one row that this query should return.
#8

[eluser]Kraig[/eluser]
Got it, changed it to result_array(). Thanks everyone!!!
#9

[eluser]xtremer360[/eluser]
Ah snap my bad. Yeah it should have been result_array(). Good luck.
#10

[eluser]vitoco[/eluser]
row_array() returns only 1 row, use result_array() instead
Code:
function get_files()
{
  $userID = $this->session->userdata('id');  
  $query = $this->db->query('SELECT * FROM upload WHERE userID = '.$userID);
  $num_row = $query->num_rows();
  return $query->result_array();

}
Slds




Theme © iAndrew 2016 - Forum software by © MyBB