filename increment in php problem...

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
User avatar
tentuxius
Forum Newbie
Posts: 2
Joined: Sun Feb 01, 2004 6:25 pm
Location: United Kingdom
Contact:

filename increment in php problem...

Post 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.
User avatar
tentuxius
Forum Newbie
Posts: 2
Joined: Sun Feb 01, 2004 6:25 pm
Location: United Kingdom
Contact:

Alright I'll figure it myself

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Perhaps sprintf is a solution ;)
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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">';
}
Post Reply