tags:

views:

49

answers:

3

Hi all, I have a link that I have to repeat 50 times, for each folder, and I have 15 folders.the link that I have to repeat looks like this:

<a href="update/images/Cars/car (x).JPG" rel="lightbox[cars]"></a>

now, the jpg files are named car 1- car 50. and I would really like to be able to generate this script so that I can input the path "update/images/Cars/" the picture title (car) and the input the number of times that I need this link, and then have it spit out something that looks like this:

<a href="update/images/Cars/car (1).JPG" rel="lightbox[cars]"></a>
<a href="update/images/Cars/car (2).JPG" rel="lightbox[cars]"></a>
<a href="update/images/Cars/car (3).JPG" rel="lightbox[cars]"></a>
<a href="update/images/Cars/car (4).JPG" rel="lightbox[cars]"></a>
<a href="update/images/Cars/car (5).JPG" rel="lightbox[cars]"></a>
<a href="update/images/Cars/car (6).JPG" rel="lightbox[cars]"></a>

and then it keeps repeating, I'm assuming this can be done with a counter, but I'm not sure. Thanks!

+2  A: 

You can do it with a for loop:

$path = "update/images/Cars/";
$title = "car";
$times = 50;

for($i = 1; $i <= $times; $i++)
    echo "<a href=\"$path$title ($i).JPG\" rel=\"lightbox[$title]\"></a>\n";

I used $title for the lightbox argument since you didn't specify

Michael Mrozek
cool, thanks! I think that will work
Ryan
+2  A: 

Use a powerful text editor. ;-)

For example, in Vim, I can use the following sequence of keystrokes to create your required text:

  • i <a href="update/images/Cars/car (0).JPG" rel="lightbox[cars]"></a> Esc
  • qa (start recording macro into register a)
  • Y (yank (= copy) whole line)
  • p (paste into the following line)
  • / ( Return (search for opening brace)
  • Space (advance cursor one character so it now sits on the number)
  • Ctrl+a (increment the number)
  • q (stop recording the macro)
  • 49@a (invoke the macro 49 times)
Konrad Rudolph
A: 

If you're going to add or remove images from the folder, then you might get better results using the DirectoryIterator object from the Standard PHP Library. Using it would require PHP5, but there's an old-school way of handling it, too. This snippet assumes that all of the files in the directory are the images you want to list:

$link = '<a href="/path/to/update/images/Cars/%s" rel="lightbox[cars]">%s</a>';
$dir = new DirectoryIterator("/path/to/update/images/Cars");
foreach($dir as $file) if(!$file->isDot()) echo sprintf($link, $file, $file);

Notice that I put the information about the anchor-element into the $link variable and then used sprintf to print those anchors to the screen. If you don't have PHP5 available to you, you'd want to do it this way:

$link = '<a href="/path/to/update/images/Cars/%s" rel="lightbox[cars]">%s</a>';
$dir = opendir("/path/to/update/images/Cars");
while(($file = readdir($dir)!==false) if($file != "." && $file != "..") echo sprintf($link, $file, $file);
closedir($dir);

These would only be necessary if you're adding more car photos into the library and don't want to update the page that produces all the links. Both of these snippets should automatically search through the directory of car images and create the links you need.

You can also alter these snippets to search through sub-directories, so you could slam out the links to the images in all 15 folders all with a little bit more code. Let me know if you want to see that code, too.

David Kees