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.
Loop + counter
Moderator: General Moderators
Re: Loop + counter
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++;
}
?>Re: Loop + counter
It's not clear from your initial post; does switch_product_img take one argument or two?
Re: Loop + counter
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>";
}
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>";
}
Re: Loop + counter
Hi,
Is anyone able to assist me on this one please?
Cheers,
vfm.
Is anyone able to assist me on this one please?
Cheers,
vfm.
Re: Loop + counter
Look at the HTML source in your browser and compare it to the PHP string.
PHP Manual:
PHP Manual: