N00b using a while loop to print contents from an array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

RickyT23
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Nov 02, 2011 8:44 am

N00b using a while loop to print contents from an array

Postby RickyT23 » Wed Nov 02, 2011 8:59 am

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

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

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

Postby NigelRen » Thu Nov 03, 2011 1:58 am

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 ).

RickyT23
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Nov 02, 2011 8:44 am

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

Postby RickyT23 » Thu Nov 03, 2011 7:00 am

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

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

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

Postby egami » Thu Nov 03, 2011 7:06 am

You can't open a lock without the right key.

RickyT23
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Nov 02, 2011 8:44 am

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

Postby RickyT23 » Thu Nov 03, 2011 6:36 pm

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! :)

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

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

Postby NigelRen » Fri Nov 04, 2011 12:46 am

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.

harbek2014
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Feb 21, 2014 9:01 pm

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

Postby harbek2014 » Fri Feb 21, 2014 9:20 pm

Sorry this isn't of much help but saw the 'sky hooks' part an just had to mention....

While sky hook has always been a major joke, in reality the military actually worked this
into a functional operation - called sky hook - where a single individual would be scooped
off the ground by an airplane flying over the field and hooking a wire attached to an individual
on the ground.

Just had to let others know the actual sky hook does exist.

I did try your code and it displayed everything quite well.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests