For loop question?

This is a discussion on For loop question? within the PHP Language forums, part of the PHP Programming Forums category; I have a couple of arrays that represent different categories. One array per category. The arrays contain names of photos. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 5 Days Ago
SM
 
Posts: n/a
Default For loop question?



I have a couple of arrays that represent different categories. One
array per category. The arrays contain names of photos. The arrays
look like this:

$cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
$cat_2 = array(...);
$cat_3 = array(...);
$cat_4 = array(...);

Then, i want to create a thumbnail with all the photos like this:
for($i=0; ...){
....<img src="images/thumbs/<?php echo $cat_1[$i]; ?>" />
}

This works OK for the first array.


What if i'm getting the name of the array from a variable? How do i
construct my 'for loop'.

I do it like this:

$cat = $_GET['cat']; //output: 1
$sel_array = 'cat_' . $cat;//create the string: cat_1

Then i create the thumbs like this:
for($i=0; ...){
....<img src="images/thumbs/<?php echo HELP!!!! ;?>" />
}


How do i construct the thumbnail?


Thanks
Marco
Reply With Quote
  #2 (permalink)  
Old 5 Days Ago
Iván Sánchez Ortega
 
Posts: n/a
Default Re: For loop question?

SM wrote:

> $cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
>
> for($i=0; ...){


Don't.

When iterating through arrays, *always* use foreach().

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Now listening to: Anggun - Au nom de la lune (1997) - [7] La Mémoire des
rochers (4:03) (74.500000%)
Reply With Quote
  #3 (permalink)  
Old 5 Days Ago
SM
 
Posts: n/a
Default Re: For loop question?

On May 8, 3:24 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.org> wrote:
> SM wrote:
> > $cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');

>
> > for($i=0; ...){

>
> Don't.
>
> When iterating through arrays, *always* use foreach().
>
> --
> ----------------------------------
> Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
>
> Now listening to: Anggun - Au nom de la lune (1997) - [7] La Mémoire des
> rochers (4:03) (74.500000%)


I'm using the for loop for pagination not to iterate throught the
array. I've only created this example for demonstration purposes. The
idea what to learn how to get the values of the array if the array was
define in a variable?

Any ideas, anyone?

Thanks
Marco
Reply With Quote
  #4 (permalink)  
Old 5 Days Ago
Ivan Marsh
 
Posts: n/a
Default Re: For loop question?

On Thu, 08 May 2008 21:24:49 +0200, Iván Sánchez Ortega wrote:

> SM wrote:
>
>> $cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
>>
>> for($i=0; ...){

>
> Don't.
>
> When iterating through arrays, *always* use foreach().


Isn't foreach simply creating a for loop based on the size of the array?

--
"Remain calm, we're here to protect you!"

Reply With Quote
  #5 (permalink)  
Old 5 Days Ago
Iván Sánchez Ortega
 
Posts: n/a
Default Re: For loop question?

SM wrote:

> I'm using the for loop for pagination not to iterate throught the
> array. I've only created this example for demonstration purposes. The
> idea what to learn how to get the values of the array if the array was
> define in a variable?


In that case, read the PHP manual on the subject on "variable variables".
It's a feature seldom used, that may fit your case.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Nunca temas decirle al mundo quién eres.
-- Anónimo
Reply With Quote
  #6 (permalink)  
Old 5 Days Ago
Iván Sánchez Ortega
 
Posts: n/a
Default Re: For loop question?

Ivan Marsh wrote:

>> When iterating through arrays, *always* use foreach().

>
> Isn't foreach simply creating a for loop based on the size of the array?


No.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

"Give me chastity and continence, but not yet."
- Saint Augustine (354-430)
Reply With Quote
  #7 (permalink)  
Old 5 Days Ago
Ivan Marsh
 
Posts: n/a
Default Re: For loop question?

On Thu, 08 May 2008 22:05:48 +0200, Iván Sánchez Ortega wrote:

> Ivan Marsh wrote:
>
>>> When iterating through arrays, *always* use foreach().

>>
>> Isn't foreach simply creating a for loop based on the size of the array?

>
> No.


Can you expand on that?

--
"Remain calm, we're here to protect you!"

Reply With Quote
  #8 (permalink)  
Old 5 Days Ago
Iván Sánchez Ortega
 
Posts: n/a
Default Re: For loop question?

Ivan Marsh wrote:

>>> Isn't foreach simply creating a for loop based on the size of the array?

>>
>> No.

>
> Can you expand on that?


Sorry, I'm just not in the mood for a lesson on data structures, ordered
maps, and object iterators. It'd take a couple of hours.

However:

Stop thinking of PHP arrays as C arrays. PHP arrays are ordered maps. They
don't even have to have integer indexes.

Do read the PHP manual on arrays, the foreach() construct and object
iterators.

Grab a C/C++ book which covers the STL library, so you can see the
differences between a vector, an array, a map, a hash table, and an ordered
hash table.


Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

The naked truth of it is, I have no shirt.
-- William Shakespeare, "Love's Labour's Lost"

Reply With Quote
  #9 (permalink)  
Old 5 Days Ago
chander@otg-nc.com
 
Posts: n/a
Default Re: For loop question?

On May 8, 4:07*pm, Ivan Marsh <ivanma...@yahoo.com> wrote:
> On Thu, 08 May 2008 22:05:48 +0200, Iván Sánchez Ortega wrote:
> > Ivan Marsh wrote:

>
> >>> When iterating through arrays, *always* use foreach().

>
> >> Isn't foreach simply creating a for loop based on the size of the array?

>
> > No.

>
> Can you expand on that?
>
> --
> "Remain calm, we're here to protect you!"


Foreach loops work by iterating over the keys of an array, and are
universal in that they work with both simple arrays (sequential
integer keys) and associative arrays (those where the keys are not
strings).

Furthermore, the standard form of foreach retrieves the associated
value when it retrieves they key, hence making the iteration over the
values in an array a bit faster (since no additional lookup is
required).

Lastly, a foreach loop can operate on a function that returns an array
(as opposed to a variable that contains an array) in a fairly
straightforward manner, whereas a for loop requires that the array be
assigned to a variable beforehand.

--
Chander Ganesan
Open Technology Group, Inc.
One Copley Parkway, Suite 210
Morrisville, NC 27560
919-463-0999/877-258-8987
http://www.otg-nc.com
Ask me about Expert on-site and public enrollment PHP Training
delivered worldwide.
Reply With Quote
  #10 (permalink)  
Old 5 Days Ago
Rik Wasmus
 
Posts: n/a
Default Re: For loop question?

On Thu, 08 May 2008 21:05:03 +0200, SM <servandomontero@gmail.com> wrote:

>
>
> I have a couple of arrays that represent different categories. One
> array per category. The arrays contain names of photos. The arrays
> look like this:
>
> $cat_1 = array('moonlight.jpg'', shadow.jpg', 'luna.jpg');
> $cat_2 = array(...);
> $cat_3 = array(...);
> $cat_4 = array(...);


A lot of people lately seem to be using this kind of construct. To put it
simple: _don't_ do that. The name of a variable should hold no important
data save for identification of the variable by both interpreter & coder..

The correct way to do this is:

$cats = array(
1 => array('moonlight.jpg' .......),
2 => array(....),
3 => array(....));

And then just use:

foreach($cats[$_GET['cat'] as $img) echo $img;

> $cat = $_GET['cat']; //output: 1
> $sel_array = 'cat_' . $cat;//create the string: cat_1


You could use ${'cat_'.$cat} (or $$sel_array). This would be an unwise
choice though.
--
Rik Wasmus
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:39 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0