473,378 Members | 1,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1580
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
3
by: zeroDoNotYeSpamtype | last post by:
(I tried to post this earlier, but it seems without success) A similar question to this has been answered many times, to whit the question applying when the index variable of a for loop is...
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
22
by: Jan Richter | last post by:
Hi there, the Code below shows DJBs own implementation of strlen (str_len): unsigned int str_len(char *s) { register char *t; t = s; for (;;) { if (!*t) return t - s; ++t;
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
3
by: nina297 | last post by:
Good morning, How do I set up a For each loop to loop through my records in the database? My fields are topics, question and answers. I want the one topic displayed that goes with each...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
2
by: perkykoala | last post by:
I apologize in advance for being REALLY detailed/verbose. It's the result of staring/tweaking code for too long. Using VB 2005: I need to design a multiple choice test (unfortunately, I can't...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.