Page 1 of 1

filename increment in php problem...

Posted: Sun Feb 01, 2004 6:25 pm
by tentuxius
Hello there budding PHP coders. I figured since I've not really come up with any code that would get me to my "destination" I shouldn't really use the PHP - Code section as this is all... unknown really.
What I wish to do: Open files (images) with image names such as:
image001.gif
How I wish to do this: use a variable and increment each time which then loads the image onto the page.
The problem: you cannot go $var = 001; $var++;
Reason: 001 + 1 = would you believe it 1... preceding zeros seem to mean nothing OMG! lol Yes alright I was just making sure you wouldn't suggest this. Does anyone have any ideas how i would generate lists automatically that follow the pattern:
001 002 003 004 005 006 007 008 009 010 011

Ideas: increament the values and THEN place the preceeding zeros, but I don't know how to do this.

Alright I'll figure it myself

Posted: Sun Feb 01, 2004 6:58 pm
by tentuxius
I've decided to cheat....

Code: Select all

<?php
$CounterI = 0;

while($Counter <= 9) {
  echo '<IMG SRC="image00';
  echo $CounterI;
  echo '.gif">';
  $CounterI++;
}

$CounterI = 10;
while($Counter <=99) {
  echo '<IMG SRC="image0';
  echo $CounterI;
  echo '.gif">';
  $CounterI++;
}

$CounterI = 100;
while($Counter <=999) {
  echo '<IMG SRC="image';
  echo $CounterI;
  echo '.gif">';
  $CounterI++;
}
?>
There's probably a better solution: If anyone thinks of it post, for now this code will surfice. Thanks for you time reading this.

Posted: Mon Feb 02, 2004 12:46 am
by timvw
Perhaps sprintf is a solution ;)

Posted: Mon Feb 02, 2004 9:33 am
by lazy_yogi

Code: Select all

for ($i=0; $i<1000; $i++)
{
    echo '<IMG SRC="image'.str_pad($i, 3, '0', STR_PAD_LEFT).'.gif">';
}