Loop + counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Loop + counter

Post by vfm »

Hi all,

I'm trying to output the following HTML onto my page:

<li><a href=\"javascript:void(0)\" onclick=\"switch_product_img('photo_1', '$count');\"><img src='$row[photo1_thumb]' alt='' /></a></li>

This is currently part of a for loop and what I want to do is replace the numbers above e.g. photo_1 using the counter to inject the number e.g. photo_[counter]

However, I don't know the correct syntax to do that. Is someone able to let me know how I should be going about this?

Cheers,
vfm.
djlex1928
Forum Commoner
Posts: 31
Joined: Sun Sep 19, 2010 3:23 pm

Re: Loop + counter

Post by djlex1928 »

Code: Select all

<?php
$count = 0;
while($count < 5) {
echo '<li><a href="javascript:void(0)" onclick="switch_product_img(\'photo_1\', \''.$count.'\');\"><img src='.$row[photo1_thumb].' alt="" /></a></li>';
$count++;
}
?>
Edit the 5 in the while statement to however many times you need it to loop.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Loop + counter

Post by Celauran »

It's not clear from your initial post; does switch_product_img take one argument or two?
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Re: Loop + counter

Post by vfm »

Hi,

I may not have explained myself clearly. I have in my db four columns which contain image paths. What I'm wanting to do is create a loop through these fields i.e. photo1_thumb, photo2_thumb, photo3_thumb and photo4_thumb. I want my for loop to essentially populate each of these. I had the following syntax but it's clearly wrong and I'm not sure how I would correct it to get it to work. Any help much appreciated.

for ($i = 1; $i <= $count; $i++) {
$thumbs .= "<li><a href=\"javascript:void(0)\" onclick=\"switch_product_img('photo_'$i, '$count');\"><img src='$row[photo'.$1.'_thumb]' alt='' /></a></li>";
}
vfm
Forum Commoner
Posts: 32
Joined: Tue Mar 30, 2010 10:47 pm

Re: Loop + counter

Post by vfm »

Hi,

Is anyone able to assist me on this one please?

Cheers,
vfm.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Loop + counter

Post by McInfo »

Look at the HTML source in your browser and compare it to the PHP string.

PHP Manual:
Post Reply