Foreach()

A real noob question

My error:

“Warning: Invalid argument supplied for foreach() in … on line 11”

The code:

<?php
 function fetch_sub_pg($id) {
 	global $fw;
	
  	$sql = "select * from 2visual_page where page_parent_id = '".$id."' and page_sitemap = '1' ";
	$num = mysql_num_rows($fw->db->query($sql));
	if($num == 1)
	$result[] = $fw->db->getArray( $sql );
	else 
	$result = $fw->db->getArray( $sql );
	 foreach( $result as $key => $row ){
	 if($row['page_title'] != '') {
		$content123 .= '<tr><td style="padding-left:30px;">&raquo;
	  						<a href="'.urlify($row['page_title']).'">'.$row['page_menu_title'].'</a>
				   </td></tr><tr><td height="4"></td></tr>';
		}			   
	  }
	 return $content123;
  
  }
switch( $_GET['action'] ){
  default:   
  	 global $fw;
	 $sql = "select * from 2visual_page where page_parent_id = '0' and page_sitemap = '1' ";
	 $result = $fw->db->getArray( $sql );
	 $content123 .= '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td><h1>Sitemap</h1></td></tr><tr><td height="6"></td></tr>';
	  foreach( $result as $key => $row ){
	  $content123 .= '<tr><td>
	  						<a href="'.urlify($row['page_title']).'">'.$row['page_menu_title'].'</a>
				   </td></tr>'.fetch_sub_pg($row['page_id']).'<tr><td height="8"></td></tr>';
	  }
	 $content123 .= '</table>';
	 print $content123;
	
	 	
	
         break;
}

?>

And i cant find what the array solution is…
Can someone point me in the good direction or give me the solution?

Yes :slight_smile:

Btw, my guess is that $fw->db->getArray( $sql ) returns NULL when no records are found, instead of an empty array.

True,

But at this moment i am helped…

Thanks guys for your input…

See Guido, regardless of your suggestion he just suppressed the error for now. Different people asks questions and wants the solutions differently as well :-).

But for the OP, it is still good to know why the query did not return any records. It might be the case it is supposed to return some records.

The code provided by Guido is also not working…Still the same error

The code I provided is from an sitemap module of a cms, which my brother wrote for me.
I could ask my brother, but he’s very busy at this moment.

I think i have the solution ( i looked in mailarchive, which is large ) and i entered:

if( ! is_array( $result ) ){
$result = array();
}

above the foreach rule

Now i dont see no errors and its working

To prevent the error/warning on the page, you just check as i suggested in my previous post.

True

But without seeing the database class ($fw) I cannot say that if the result set is required for getArray($result) or it directly returns the array only with SQL.

Also true :slight_smile:

Let’s see if my code helps him. If not, he’ll have to give some more info.

OP’s first problem was that I thought. I don’t know if he is asking why the query does not return any records. But without seeing the database class ($fw) I cannot say that if the result set is required for getArray($result) or it directly returns the array only with SQL.

Guido is correct - you need to set your result on line 6, then make line 7 $num = mysql_num_rows($result);, and update the rest of your calls as necessary.

The var_dump gives me an NULL
So what to do next?

What is the output of the $result ( $result = …)

When the foreach is executed, $result is not an array. If you want to know what it contains, try a var_dump($result);

It is always good practice to check if the variable is array (is_array()) or not if you are not sure before you using with foreach.


if(is_array($result) && count($result) >= 1){
    foreach($result as $key=>$row){
        //-------------
    }
}

But that would not resolve the problem. Only hide it.

So it isn’t even set.
Try this:


function fetch_sub_pg($id) {
  global $fw;

  $sql = "select * from 2visual_page where page_parent_id = '".$id."' and page_sitemap = '1' ";
  $result = $fw->db->getArray( $sql );
  foreach( $result as $key => $row ) {
    if ($row['page_title'] != '') {
      $content123 .= '<tr><td style="padding-left:30px;">&raquo;
                            <a href="'.urlify($row['page_title']).'">'.$row['page_menu_title'].'</a>
                 </td></tr><tr><td height="4"></td></tr>';
    }               
  }
  return $content123;
}