469,602 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

For loop question?

SM


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
Jun 2 '08 #1
11 1424
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%)
Jun 2 '08 #2
SM
On May 8, 3:24 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
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
Jun 2 '08 #3
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!"

Jun 2 '08 #4
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
Jun 2 '08 #5
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)
Jun 2 '08 #6
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!"

Jun 2 '08 #7
On May 8, 4:07*pm, Ivan Marsh <ivanma...@yahoo.comwrote:
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.
Jun 2 '08 #8
On Thu, 08 May 2008 21:05:03 +0200, SM <se*************@gmail.comwrote:
>

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
Jun 2 '08 #9
Rik Wasmus wrote:
The correct way to do this is:

$cats = array(
1 =array('moonlight.jpg' .......),
2 =array(....),
3 =array(....));
An alternate way to do the same is:

$cats[1] = array('moonlight.jpg' .......);
$cats[2] = array('foobar' .......);
$cats[3] = array(...);
And then just use:

foreach($cats[$_GET['cat'] as $img) echo $img;
You're missing a "]" here, Rik!

foreach($cats[ $_GET['cat'] ] as $img) echo $img;
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Jun 2 '08 #10
On Thu, 08 May 2008 23:14:44 +0200, Iván Sánchez Ortega
<ivansanchez-alg@rroba-escomposlinux.-.punto.-.orgwrote:
Rik Wasmus wrote:
>The correct way to do this is:

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

An alternate way to do the same is:

$cats[1] = array('moonlight.jpg' .......);
$cats[2] = array('foobar' .......);
$cats[3] = array(...);
>And then just use:

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

You're missing a "]" here, Rik!
Oh no! Now all I say is suspect, and therefor should not be followed.
Forget what I said, having $cat_1 & $cat_2 like variable names must be a
great idea :).
--
Rik Wasmus
Jun 2 '08 #11
SM
On May 8, 5:21 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Thu, 08 May 2008 23:14:44 +0200, Iván Sánchez Ortega

<ivansanchez-...@rroba-escomposlinux.-.punto.-.orgwrote:
Rik Wasmus wrote:
The correct way to do this is:
$cats = array(
1 =array('moonlight.jpg' .......),
2 =array(....),
3 =array(....));
An alternate way to do the same is:
$cats[1] = array('moonlight.jpg' .......);
$cats[2] = array('foobar' .......);
$cats[3] = array(...);
And then just use:
foreach($cats[$_GET['cat'] as $img) echo $img;
You're missing a "]" here, Rik!

Oh no! Now all I say is suspect, and therefor should not be followed.
Forget what I said, having $cat_1 & $cat_2 like variable names must be a
great idea :).
--
Rik Wasmus
Thanks Rik. Try it and it work perfectly! And i didn't need to read
the PHP Manual. I guess that's what a forum is for.
Thanks again for beeing 'in the mood'
Marco
Jun 2 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by Arthur | last post: by
3 posts views Thread by zeroDoNotYeSpamtype | last post: by
3 posts views Thread by Gustavo Randich | last post: by
22 posts views Thread by Jan Richter | last post: by
3 posts views Thread by nina297 | last post: by
44 posts views Thread by James Watt | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.