php help
- Started
- Last post
- 5 Responses
- toe_knee
Im trying to do a php loop so that I can get the below sequence but its not working
<media file="images/image_000.jpg"></media><media file="images/image_001.jpg"></media><media file="images/image_002.jpg"></media><media file="images/image_003.jpg"></media>
heres the code
<?php for ($i=0; $i < 200; $i++) { $image_prefix = ''; if($i<10) { $image_prefix = '00'; } else if($i<100) { $image_prefix = '0'; } print '<media file="images/image_' . $image_prefix . $i . '.jpg"></media>';
}
?>
- seeessess0
10 print images/image_000.jpg
20 goto 10- I thinkseeessess
- You forgot RUN on line 3. That's your problem right there.Continuity
- toe_knee0
Is the problem iwith the quotation marks maybe
print '<media file="images/image_' . $image_prefix . $i . '.jpg"></media>';
Cause the output needs to be:
<media file="images/image_000.jpg"></media>
- PersonaNonGrata0
what isnt working about it?
Does it not outupt anything? is it giving you the wrong number of zeros....
- PersonaNonGrata0
works perfectly when I ran it here : http://sandbox.onlinephpfunction…
- vdc0
Your code works fine, although this does the same thing in fewer lines:
for ($i=0; $i < 200; $i++) {
print '<media file="images/image_' . sprintf("%03d", $i) . '.jpg"></media>';
}