Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Basics (Retired) PHP Conditionals & Loops For Loops

php making image links using loops

Hi Guys,

Trying to make a little script that will write all my a href links as I've renamed all my files using Adobe bridge to 001, 002 etc... I wrote this but I can't get it to work, I only really went through PHP basic course yesterday so I'd appreciate an explanation too, I feel that I'm close though?

" <?php

for($imagenumber = 001; $imagenumber < 100; $imagenumber++){

echo "< website reference* . <?php echo $imagenumber ?> . "width="100%"> <(closing off a tag)>"

}

?> "

  • = treehouse removes html ref links I think as it doesn't appear

Thank you!

for($imagenumber = 001; $imagenumber < 100; $imagenumber++){ echo "<a href="http://www.imagelink.com/images/category/" . <?php echo $imagenumber?> . "width="100%"> </a>" }

Hi Juice Dont think assigning 001 as an integer like that will work. I ran your code and the output was 1,2,3 not 001,002 etc because php is converting 001 to an integer 1 so you end up with 1,2,3. I think if you append a string in front of the number like this.

<?php 
$imagenumber_s="00";
 for($imagenumber = 001; $imagenumber < 100; $imagenumber++)
    { 
        $imagenumber=$imagenumber_s.$imagenumber;
        echo 'website reference* '.$imagenumber." width='100%' "."</br>"; 

    } 
?>

Ok I understand that bit, how would I write it so the code generated would be www.imagelink.com/image/category/001 - 002 003 etc?

<?php

      for($imagenumber = 1; $imagenumber < 100; $imagenumber++){

echo "<p> <website link" . $imagenumber . " width="100%"> <a tag> </p>" ; }?>

is as far as I've got - Thanks for your help! Sorry I'm having troubles displaying all of my code on there for some reason

the code in theory works great so thank you for your help i was really close in it being correct! however once it reaches 9 afterwards it will produce 0010, 0011, 0012 am I correct in what I'm thinking? As its a constant value of $imagenumber_s? not, would have to be and if statement? so when image number == 009,

$imagenumber_s="00";
 for($imagenumber = 001; $imagenumber < 100; $imagenumber++)
    { 
        $imagenumber=$imagenumber_s.$imagenumber;
        echo 'website reference* '.$imagenumber." width='100%' "."</br>"; 

    } if else{

 $imagenumber_b = 0;
($imagenumber = 9; $imagenumber < 99; $imagenumber++){

$imagenumer = $imagenumber_b.$imagenumber'
echo 'website reference* '.$imagenumber." width='100%' "."</br>";

}

then continue so its 010, 011, 012?

Juice

how many images do you have and how exactly you want it output ?? might be to write the code once and not have to do two if statements?? and yes after 9 it will output 0010,0011,0012 ect

its like 280 images all together but like 50 in each folder. I'm just looking for it to produce the html text so I can copy it into this input editor as it will save so much time, sorry about the messy replies, treehouse is blocking them.

try this just change the variables that should work. or just change the code around to get the desired html result.

<?php
$yourfoldername="foldername";
$no_of_images=50;
$counter=0;
$imagenumber_s="www.imagelink.com/image/category/".$yourfoldername."/";

 for($i = 1; $i <= $no_of_images; $i++)
    { 
        $counter++; // using this counter to check the last round of the loop to add            the - only when it is not the last counter
           if($counter ==$no_of_images){
                         $imagenumber_s=$imagenumber_s.'00'.$i;
               }
             else{
                   $imagenumber_s=$imagenumber_s.'00'.$i.'-';
              }

     } 
echo $imagenumber_s." width=100%";
   ?>

Clever idea, looks fun to try.

2 Answers

Hi Guys, Managed to do it after using yours as reference I simplified it a bit. note that the images are the same name as the folder.

 <?php
$yourfoldername="foldername";
$no_of_images=50;
$counter=0;
$format = ".jpg'";

$imagenumber_s="<img src='http://www.website.com/wp-content/uploads/category/".$yourfoldername."/".$yourfoldername."_";

 for($i = 1; $i <= $no_of_images; $i++)

 {

   echo $imagenumber_s."00".$i.$format .  " width='100%'>this is a link</a>"    ;

     } 
   ?>
<?php 
for($imgenum = 001; $imgenum < 100; $imgenum++){

if ($imgenum < 10){

echo "00$imgenum" ."<br />";

}else{

echo  "0$imgenum" ."<br />";

}}
?>