Page 1 of 1

N00b using a while loop to print contents from an array

Posted: Wed Nov 02, 2011 8:59 am
by RickyT23
Hi all

I have recently started the undergraduates course for computer science, and for part of my 'Web Apps' module I am required to make a website with a simulated database (PHP with no SQL).

Stupid, right?

Anyway, I am pretty much new to PHP, but I am trying to experiment and learn so that I can ace my coursework in a few months time.

Code: Select all

<html>
<body>

<?php

$products = array
	(
		"Chocolate Fireguard" => array
			(
			"Chocolate Fireguard",
			"The chocolate fireguard - especially designed to look super-classy infront of your fireplace or hearth, a lovely shade of chocolate brown.  Will absorb large quantities of heat, and then probably melt into your carpet and poison your dog!",
			"59.99"
			),
		"Sky Hooks" => array
			(	
			"Sky Hooks",
			"Portable, compact and cost effective - theese skyhooks can be attached to the sky almost anywhere.  Dont have enough hangers in your garden?  Well - why not try Sky Hooks to allow you to hang whatever you want, wherever you like!",
			"9.99"
			)
	);

$x = count($products);
$y = 0;
while ($y<=$x){
		echo "Product Name: " . $products[$y][0];
		$y++;
	}

?>

</body>
</html>
I have been understanding this, but there is a problem on the line
echo "Product Name: " . $products[$y][0];
And it gives me an 'undefined index' error. I dont inderstand why. The [$y][0] is supposed tp specify the first entry from each of the sub-arrays, within the $products multidimensional array. But obviously it's not functioning the way I imagined to would. Please help!

Best regards

Rick

Re: N00b using a while loop to print contents from an array

Posted: Thu Nov 03, 2011 1:58 am
by NigelRen
Have a look at foreach ( http://php.net/manual/en/control-structures.foreach.php ) for using arrays. The main thing to think about is that your trying to access the array using numeric values for the keys, but you've created the array with specific keys ( Chocolate Fireguard as the first ).

Re: N00b using a while loop to print contents from an array

Posted: Thu Nov 03, 2011 7:00 am
by RickyT23
Thanks very much for you help there, so among other things, I need the content of the primary array to be an index, rather than the title. I had assumed that I could call the array using an index, even though the arrays had actual titles.

(general musing and scurrying sounds) :D

Re: N00b using a while loop to print contents from an array

Posted: Thu Nov 03, 2011 7:06 am
by egami
You can't open a lock without the right key.

Re: N00b using a while loop to print contents from an array

Posted: Thu Nov 03, 2011 6:36 pm
by RickyT23
Thanks for your help. I have had my little eureka moment, I found some very useful information here:

http://www.informit.com/articles/articl ... 0&seqNum=4

This information seems very old, I worry about any problems which might occur. Either way, when I run this horrid little uncommented test code on a WAMP server, it seems to produce the correct output without any errors.

Code: Select all

<html>
<body>

<?php

$products = array
   (
      
	  array
         (
         "Chocolate Fireguard",
         "The chocolate fireguard - especially designed to look super-classy infront of your fireplace or hearth, a lovely shade of chocolate brown.  Will absorb large quantities of heat, and then probably melt into your carpet and poison your dog!",
         "59.99"
         ),
       
	  array
         (   
         "Sky Hooks",
         "Portable, compact and cost effective - theese skyhooks can be attached to the sky almost anywhere.  Dont have enough hangers in your garden?  Well - why not try Sky Hooks to allow you to hang whatever you want, wherever you like!",
         "9.99"
         )
   );
   
   $x = count($products);
   
for ( $row = 0; $row < $x; $row++ ){

	for ( $column = 0; $column < 3; $column++ ){
	
		echo $products[$row][$column] . '<br />';
		
	}
	
echo '<br />';

}

?>

</body>
</html>
Thanks again for your help! :)

Re: N00b using a while loop to print contents from an array

Posted: Fri Nov 04, 2011 12:46 am
by NigelRen
The main difference ( I can see at a glance ) is that in the original code you used '=>' when you were building the array. That is how you had created the association between the text ( e.g. Chocolate fireguard ) and an element in the array.