Tutorial :printing a UL from an array of images



Question:

I am trying to print a UL list of images ending with _th.jpg returned from a specific folder. All i get back is array()

<?php  require '../../config.php';  $conn = new PDO(DB_DSN, DB_USER, DB_PASS);    $id = (int)$_GET['id'];  $q = $conn->query("SELECT * FROM cms_page WHERE id=$id");   $project = $q->fetch(PDO::FETCH_ASSOC);  $q->closeCursor();    $imagesDir = 'public/images/portfolio/'.$project['slug'].'/';  $images = glob($imagesDir . '*_th.{jpg,jpeg,png,gif}', GLOB_BRACE);    echo $imagesDir ;  print_r($images);    ?>    <h1><?=htmlspecialchars($project['title'])?></h1>  <?php   if ($images < 1) {       echo '<div id="slider">';      echo '<ul>';          foreach($images as $key => $value) {               echo '<li><a href="public/images/portfolio/i-vis/1.jpg" rel="shadowbox[iVis]" title="Fully Content Managed Website"><img src="public/images/portfolio/'.$project['title'].'/'.$value.'.jpg" width="160" height="96" /></a></li>';          }      echo '</ul>';      echo '</div>';  } else {       echo 'There are currently no images to display for tihis project.';  }  ?>  

Anyone got any ideas?


Solution:1

First of all : are you sure you are reading from the right directory ?

What I mean, is : does 'public/images/portfolio/'.$project['slug'].'/' really exist, and is accessible from your script, using this relative path, in this PHP script ?

(You can test that with is_dir, for instance)


As a precaution, when working with files and directories, I like using absolute paths, so I always know to which directory exactly I'm accessing.

For instance, something like this :

$imagesDir = '/var/www/public/images/portfolio/'.$project['slug'].'/';  

If you do not know the absolute path to your directories, you can use dirname(__FILE__), to get the absolue path to the current PHP file -- and then, starting from that file, use a relative path ; for instance :

$imagesDir = dirname(__FILE__) . '/public/images/portfolio/'.$project['slug'].'/';  

This way, you don't have to care about the current working directory : your path will always be the same, as it's not relative.


Hope this helps...

If it doesn't : first of all, make sure you are accessing the right directory...


Solution:2

<h1><?=htmlspecialchars($project['title'])?></h1>  

Is <? a valid PHP opening tag?

Since your last block of PHP code isn't apparently executed (since it would either print an empty ul or the text from the else block) I suspect the PHP doesn't get parsed correctly.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »