Jump to content

Need help with an array


demuro1

Recommended Posts

Hello and thanks in advance for any help you are able to supply. I think this issue stems from my lack of knowledge about arrays in php . I am able to get the info I need out of a database. I will supply that code below as well as my code that does not work. I am trying to populate a table with 3 elements in every row. every element is a nested table. Thanks again so much. I am using a nested loop to extract information from the database. This is an example of the errors I am getting.

Notice: Undefined offset: 1 in /Library/WebServer/Documents/phpTestCode/web apps labs/elements/nestedloop.php on line 59Notice: Undefined offset: 1 in /Library/WebServer/Documents/phpTestCode/web apps labs/elements/nestedloop.php on line 64Notice: Undefined offset: 1 in /Library/WebServer/Documents/phpTestCode/web apps labs/elements/nestedloop.php on line 69Notice: Undefined offset: 1 in /Library/WebServer/Documents/phpTestCode/web apps labs/elements/nestedloop.php on line 74Notice: Undefined offset: 1 in /Library/WebServer/Documents/phpTestCode/web apps labs/elements/nestedloop.php on line 79
This code works:
<?phperror_reporting(E_ALL);ini_set('display_errors','1');$link = mysqli_connect('127.0.0.1:3306', 'root', '4pp1353c', 'storedatabase');/* check connection */if (mysqli_connect_errno()){printf("Connect failed: %s\n", mysqli_connect_error());exit();}$query = "SELECT * FROM inventory ORDER by id ASC";   $result = mysqli_query($link, $query);   //print_r($result);  ?><html> <head>  <title>test</title>   <style>/*sadly this is done without style*/  </style>  <script type='text/javascript'>/*make some magic*/  </script> </head> <body><!--begin catalog element-->   <table>     <?php	       /* fetch associative array */   while ($row = mysqli_fetch_assoc($result))    {	 print	 (      "<tr><!--catalog image--> <td valign='top'><img src='http://localhost/phpTestCode/web%20apps%20labs/Lab%2010/".$row["imageFilename"]."'></td>	 <td><table>	  <tr>	   <td valign='top'><b>Item ID:</b></td><!--catalog id-->    <td>".$row["id"]."</td>	  </tr>	  <tr>	   <td valign='top'><b>Item Name:</b></td><!--catalog name-->    <td>".$row["Product_name"]."</td>	  </tr>	  <tr>	   <td valign='top'><b>Shape:</b></td><!--catalog shape-->   <td>".$row["shape"]."</td>	  </tr>	  <tr>	   <td valign='top'><b>Size:</b></td><!--catalog size-->    <td>".$row["size"]."</td>	  </tr>	  <tr>	   <td valign='top'><b>Color:</b></td><!--catalog color-->   <td><font color=".$row['color'].">".$row["color"]."</td>	  </tr>	  <tr>	   <td valign='top'><b>Price:</b></td><!--catalog price-->   <td>$".$row["price"]."</td>	  </tr>	  <tr>	   <td valign='top'><b>Description:</b></td><!--catalog description-->  <td> This is an amazing".$row["size"]." <font color=".$row["color"]."><b>".$row["color"]."</b></font> ".$row["shape"]." with a super cool black border</td>	  </tr>  	 </table></td>    </tr>  <!--add to cart button--><!--id set by php-->    <tr><td><input type='button' value='Add to Cart' id=".$row["id"]." onclick='buttonSubmit(this,form);'></td></tr>   "   );	    };   ?>   </table><!--end catalog element--> </body></html> <?php/* free result set */mysqli_free_result($result);/* close connection */mysqli_close($link);?>

This code DOES NOT work.

<html><?php#error reportingerror_reporting(E_ALL);ini_set('display_errors','1');$link = mysqli_connect('127.0.0.1:3306', 'root', '4pp1353c', 'storedatabase');#check connectionif (mysqli_connect_errno()){printf("Connect failed: %s\n", mysqli_connect_error());exit();}#query string$query = "SELECT * FROM inventory ORDER by id ASC";$result = mysqli_query($link, $query);$row = mysqli_fetch_assoc($result);#variables$itemsperpage=count($row,0);$itemsperrow=3;$rowsperpage=$itemsperpage/$itemsperrow;?> <head>  <title>nested loop</title>   <style>/*sadly this is done without style*/  </style>  <script type='text/javascript'>/*make some magic*/  </script> </head> <body>  <table>  <?php   for ($r=1; $r<=$rowsperpage; $r++)   {    echo "<tr>";      for ($i=1; $i<=$itemsperrow; $i++)    {	 print	 (	  "<tr>	  <!--catalog image-->	   <td valign='top'><img src='http://localhost/phpTestCode/web%20apps%20labs/Lab%2010/".$row[$i]["imageFilename"]."'></td>	   <td><table>	    <tr>	    <!--catalog id-->		 <td valign='top'><b>Item ID:</b></td>		 <td>".$row[$i]["id"]."</td>	    </tr>	    <tr>	    <!--catalog name-->		 <td valign='top'><b>Item Name:</b></td>		 <td>".$row[$i]["Product_name"]."</td>	    </tr>	    <tr>	    <!--catalog shape-->  		 <td valign='top'><b>Shape:</b></td>		 <td>".$row[$i]["shape"]."</td>	    </tr>	    <tr>	    <!--catalog size-->		 <td valign='top'><b>Size:</b></td>		 <td>".$row[$i]["size"]."</td>	    </tr>	    <tr>	    <!--catalog color-->		 <td valign='top'><b>Color:</b></td>		 <td><font color=".$row[$i]['color'].">".$row[$i]["color"]."</td>	    </tr>	    <tr>	    <!--catalog price-->		 <td valign='top'><b>Price:</b></td>		 <td>$".$row[$i]["price"]."</td>	    </tr>	    <tr>	    <!--catalog description-->		 <td valign='top'><b>Description:</b></td>		 <td> This is an amazing".$row[$i]["size"]." <font color=".$row[$i]["color"]."><b>".$row[$i]["color"]."</b></font> ".$row[$i]["shape"]." with a super cool black border</td>	    </tr>  	   </table></td>	  </tr>	 <!--add to cart button-->	 <!--id set by php-->	  <tr><td><input type='button' value='Add to Cart' id=".$row[$i]["id"]." onclick='buttonSubmit(this,form);'></td></tr>	 "	 );    }    echo "</tr>";   }  ?>  </table> </body>  </html>

Link to comment
Share on other sites

You are trying to acces element which does not exist. $row will have current row as associative array when you use mysql_fetch_assoc() and will move internal pointer ahead. you are using that function once so it will give you one row and will move the p[ointer ahead once, thats all. you need to loop through the result set

Link to comment
Share on other sites

Thanks. I got it figured out. The code ended up looking like this:

<html><?php#error reportingerror_reporting(E_ALL);ini_set('display_errors','1');$link = mysqli_connect('127.0.0.1:3306', 'root', '4pp1353c', 'storedatabase');#check connectionif (mysqli_connect_errno()){printf("Connect failed: %s\n", mysqli_connect_error());exit();}#query string$query = "SELECT * FROM inventory ORDER by id ASC";$result = mysqli_query($link, $query);$row = mysqli_fetch_row($result);#variables$itemsperpage = $result->num_rows;$itemsperrow=3;$rowsperpage=$itemsperpage/$itemsperrow;?> <head>  <title>nested loop</title>   <style>/*sadly this is done without style*/  </style>  <script type='text/javascript'>/*make some magic*/  </script></head> <body><?php echo $itemsperpage; ?>  <table>  <?php   for ($r=0; $r<=$rowsperpage-1; $r++)   {    echo "<tr>";	      for ($i=1; $i<=$itemsperrow; $i++)    {	 $query = "SELECT * FROM inventory Where id =".($i+$r*3)." ORDER by id ASC";	 $result = mysqli_query($link, $query);	 $row = mysqli_fetch_assoc($result);		 print	 (	  "<td>	  <table>	  <tr>	  <!--catalog image-->	   <td valign='top'><img src='http://localhost/phpTestCode/web%20apps%20labs/Lab%2010/".$row["imageFilename"]."'></td>	   <td><table>	    <tr>	    <!--catalog id-->		 <td valign='top'><b>Item ID:</b></td>		 <td>".$row["id"]."</td>	    </tr>	    <tr>	    <!--catalog name-->		 <td valign='top'><b>Item Name:</b></td>		 <td>".$row["Product_name"]."</td>	    </tr>	    <tr>	    <!--catalog shape-->  		 <td valign='top'><b>Shape:</b></td>		 <td>".$row["shape"]."</td>	    </tr>	    <tr>	    <!--catalog size-->		 <td valign='top'><b>Size:</b></td>		 <td>".$row["size"]."</td>	    </tr>	    <tr>	    <!--catalog color-->		 <td valign='top'><b>Color:</b></td>		 <td><font color=".$row['color'].">".$row["color"]."</td>	    </tr>	    <tr>	    <!--catalog price-->		 <td valign='top'><b>Price:</b></td>		 <td>$".$row["price"]."</td>	    </tr>	    <tr>	    <!--catalog description-->		 <td valign='top'><b>Description:</b></td>		 <td> This is an amazing".$row["size"]." <font color=".$row["color"]."><b>".$row["color"]."</b></font> ".$row["shape"]." with a super cool black border</td>	    </tr>  	   </table></td>	  </tr>	 <!--add to cart button-->	 <!--id set by php-->	  <tr><td><input type='button' value='Add to Cart' id=".$row["id"]." onclick='buttonSubmit(this,form);'></td></tr>		 </table>	 </td>"	 );    }    echo "</tr>";   }  ?>  </table> </body>  </html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...