Jump to content

[SOLVED] Invalid argument supplied for foreach() - please help


shergold

Recommended Posts

Hey guys, i was wondering if anyone could help me, ive tried echoing the array but i get no output and it does not display Array.

Thanks allot,

shergold.

<?php

//Test Array
$tiles;
$tiles["01,01"] = "";
$tiles["02,01"] = "";
$tiles["03,01"] = "";


function draw_game()
{
echo "<table border=\"0\" width=\"500\" height=\"500\" />";
//for each of the tiles execute the code
print_r($tiles);

	foreach( $tiles as $id => $tile)
	{
	//add start of table row tag if count is equal to 10,20,30
	if ($count == 0 || $count == 10 || $count == 20 || $count == 30)
		{
			echo "<tr>";
		}

			echo "<td width=\"50\" height=\"50\" alight=\"left\" />";

		//check what tile image to output, default is grass	
			switch($id)
			{
				case 1:
				echo "<img src=\"$id.gif\" alt=\"$tile\" />";
				break;

				default:
				echo "<img src=\"grass.gif\" alt=\"$tile\" />";

			}

			echo "</td>";
		//add end of table row tag if count is equal to 10,20,30
		if ($count == 0 || $count == 10 || $count == 20 || $count == 30)
			{
				echo "<tr>";
			}

$count++;
	}

echo "</table>";


}

?>

you set the $tiles outside the function and try to use it into a function?

functions using only local vars (into the function) or you just make it global var...

 

<?php

//Test Array
$tiles = array();
$tiles["01,01"] = "";
$tiles["02,01"] = "";
$tiles["03,01"] = "";

function draw_game()
   {

   global $tiles;

   echo "<table border="0" width="500" height="500" />";
   //for each of the tiles execute the code
   print_r($tiles);
   
      foreach( $tiles as $id => $tile)
      {
      //add start of table row tag if count is equal to 10,20,30
      if ($count == 0 || $count == 10 || $count == 20 || $count == 30)
         {
            echo "<tr>";
         }
         
            echo "<td width="50" height="50" alight="left" />";
         
         //check what tile image to output, default is grass   
            switch($id)
            {
               case 1:
               echo "<img src="$id.gif" alt="$tile" />";
               break;
               
               default:
               echo "<img src="grass.gif" alt="$tile" />";
               
            }
            
            echo "</td>";
         //add end of table row tag if count is equal to 10,20,30
         if ($count == 0 || $count == 10 || $count == 20 || $count == 30)
            {
               echo "<tr>";
            }
         
   $count++;
      }
      
   echo "</table>";
   
      
   }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.