Jump to content

showing an array by id?


Kolinski

Recommended Posts

I have a script that shows 1 thumbnail for every album that has been created. (figure 1.0)

 

 

I would like to be able to click on the image and be taken to that albums thumbnails. I hope the code underneath is correct.

 

echo '<a href="view_album_front.php?album_id"><img src="admin/uploads/thumbs/'.$row['album_id'].'/'.$dir_arr[$random_key].'" /></a>'; 

 

 

This page of course will use another script similar to the one (figure 1.0)

 

Using the code of test1.php that I have attached. How can I manipulate it to show the thumbnails of a given album id. ??

test1.php

Link to comment
https://forums.phpfreaks.com/topic/277050-showing-an-array-by-id/
Share on other sites

Assuming view_album_front.php is the page for viewing an album, and that it accepts a GET variable of album_id to load the album, just add the album id into the url on line 30:

echo '<a href="view_album_front.php?album_id='.$row['album_id'].'"><img src="admin/uploads/thumbs/'.$row['album_id'].'/'.$dir_arr[$random_key].'" /></a>';

The same way you show one thumbnail for the album:

include 'admin/init.php';
$result = mysql_query("SELECT * FROM albums WHERE album_id = ".$_GET['album_id']);

while($row = mysql_fetch_array($result)) {

 $dir = 'admin/uploads/thumbs/'.$row[0].'';

 unset($dir_arr);
 $dir_arr = "";
 
 // Open the directory, echo getting a list of albums
 if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
   while (($file = readdir($dh)) !== false) {
    if(get_file_extension($file) == "jpg" || get_file_extension($file) == "png" || get_file_extension($file) == "jpeg") {
     $dir_arr[] = $file; // puts in array
    }
   }
   closedir($dh);
  }
 }
 
 //check to see if in an array, if there are any images
 if(is_array($dir_arr)) {
  foreach ($dir_arr as $img)
  {
  // add a href tag around the image, to view all the thumbnails
  echo '<img src="admin/uploads/thumbs/'.$row['album_id'].'/'.$img.'" />'; 
  }
 } else {
  // If there are no images, what do you want to happen?
 } 
}

function get_file_extension($file_name) {
 return substr(strrchr($file_name,'.'),1);
}

Just loop through the images instead of picking a random one.

I thought I had to foreach it. Wow I can't thank you enough for the help, I've been stuck on it for nearly 2 weeks now. Thank you so much. 

 

Just one more question like before we put a a href tag around the thumbnail so we could enter the album how can I do so to view the original sized image ? 

 

 

echo '<a href=""><img src="admin/uploads/thumbs/'.$row['album_id'].'/'.$img.'" /></a>'; 

Exactly the same way you display the thumbnail. I don't know where your full size images are, but assuming they are a directory down, it would be like this:

echo '<a href="admin/uploads/'.$row['album_id'].'/'.$img.'"><img src="admin/uploads/thumbs/'.$row['album_id'].'/'.$img.'" /></a>'; 

Hello again, If I go back to square one. Test1.php , how can include a line of php to enable the title and description of the album to show on every thumbnail ? 

 

the details are kept in the table album ? 

 //check to see if in an array, if there are any images
 if(is_array($dir_arr)) {
  // Choose a random image 
  $random_key = array_rand($dir_arr);
  // add a href tag around the image, to view all the thumbnails
 echo '<div id="photocontain"><div id="thumbnail"><a class="thickbox" href="view_album_front.php?album_id='.$row['album_id'].'"><img src="admin/uploads/thumbs/'.$row['album_id'].'/'.$dir_arr[$random_key].'" /></a></div></div>';
 } else {
  // If there are no images, what do you want to happen?
 } 
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.