Jump to content

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


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>";
   
      
   }

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.