tags:

views:

37

answers:

2

Hi guys,

I need to link 9 thumbs called: project1.jpg, project2.jpg, project3.jpg... to php pages with the same name: project1.php, project2.php, project3.php.

It's my code so far:

<?php

$dir = 'portfolio/print/img/thumbs/';
$main_page ='portfolio/print/project1'; // How can I make $main_page to show the right project each time? 
$scan = scandir($dir);

for ($i = 0; $i<count($scan); $i++) {   
if ($scan[$i] != '.' && $scan[$i] != '..') {
 if (strpos($scan[$i], '.png') !== false) {
    $curr = substr($images[$i], 0, strlen($images[$i]) -4) . ".php";

  echo '<li>
    <a href="'. *** $main_page *** . $curr . '">
    <img src="' . $dir . $scan[$i] . '" alt="' . $scan[$i] . '" />
    <strong>'. $scan[$i] .'</strong>
    </a>               
     </li>';
  }
 }

};    

?>

* Do I need to create loop to find out the right link to each page? I am not really sure how to do it.

Thank you!!

A: 

Maybe this is what you're looking:

   <a href="'. substr($main_page, 0, strlen($main_page) - 1) . $i . '/'. $curr . '">
shamittomar
If I remove the ('/') from your code it works: <a href="'. substr($main_page, 0, strlen($main_page) - 1) . $i . $curr . '">However, The first thumbs(project1.jpg) links to project3.php and not to project1.phpDo you know why? Thanks
Martin
A: 

do i understand it right:

  • you want to scan your thumbs dir each
  • thumb-image (all of them png's) have corresponding php files under
    portfolio/print

imo the cleanest solution would be:

<?php
$dir = 'portfolio/print/img/thumbs/';
$path = 'portfolio/print/';

foreach( glob($dir.'*.png') as $projectImage ) {
    $projectName = basename($projectImage, '.png');
    $project = $projectName . '.php';
    echo '<li>
        <a href="'. $path . $project . '">
        <img src="' . $projectImage . '" alt="' . $projectName . '" />
        <strong>'. $projectName .'</strong>
        </a>               
        </li>';
}

basename glob

<?php
$dir = 'portfolio/print/img/thumbs/';
$path = 'portfolio/print/';

$fileTypes = array( '.png', '.jpg', '.gif');

foreach( $fileTypes as $extension ) {
    foreach( glob($dir . '*' . $extension) as $projectImage ) {
        $projectName = basename($projectImage, $extension);
        $project = $projectName . '.php';
        echo '<li>
            <a href="'. $path . $project . '">
            <img src="' . $projectImage . '" alt="' . $projectName . '" />
            <strong>'. $projectName .'</strong>
            </a>
            </li>';
    }
}
maggie
DAMN MAN!!! It works like a charm!Thank you so much!!!
Martin
Sorry! 1 more question. Can I add jpg, gif like so: foreach( glob($dir.'.png', '.jpg','.gif') as$projectImage ) {Thanks again!
Martin
yes of course. see the edit
maggie