kafia Posted August 27, 2009 Share Posted August 27, 2009 Regards i have a simple image gallery with php i have image category list with number of pictures with in a category, but i dont know how to display that category with a thumbnail image, any one can help me, and i want to change that image after every refresh on teh page.my code is as below,<?php include("config.inc.php"); // initialization $result_array = array(); $counter = 0; $cid = (int)($_GET['cid']); $pid = (int)($_GET['pid']); // Category Listing if( empty($cid) && empty($pid) ) { $number_of_categories_in_row = 4; $result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id) FROM gallery_category as c LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id GROUP BY c.category_id" ); while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."<br/></a> "."[".$row[2]."photos]"; } mysql_free_result( $result ); $result_final = "<tr>\n"; foreach($result_array as $category_link) { if($counter == $number_of_categories_in_row) { $counter = 1; $result_final .= "\n</tr>\n<tr>\n"; } else $counter++; $result_final .= "\t<td>".$category_link."</td>\n"; }?> quick help is needed Link to comment Share on other sites More sharing options...
justsomeguy Posted August 27, 2009 Share Posted August 27, 2009 Inside this loop: while( $row = mysql_fetch_array( $result ) ){ You need another query for the current category. It's not the best idea to put a query in a loop, but doing a join to return a random image from the category in the first query might be a little complex. So inside that loop, you need to send a query to get a random image from the category. You can use a query like this to return a random record:SELECT field FROM table ORDER BY RAND() LIMIT 1 Link to comment Share on other sites More sharing options...
kafia Posted August 28, 2009 Author Share Posted August 28, 2009 yes i did it but its not working and remember that my image files are located in a directory at my server pleaze breaf it with example if you can. thank you in advance. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 28, 2009 Share Posted August 28, 2009 yes i did it but its not workingThen you didn't do it right, what code did you add? Link to comment Share on other sites More sharing options...
kafia Posted August 28, 2009 Author Share Posted August 28, 2009 yes in real i miss to understand all all here is all my code to view my gallery <?php include("config.inc.php"); // initialization $result_array = array(); $counter = 0; $cid = (int)($_GET['cid']); $pid = (int)($_GET['pid']); // Category Listing if( empty($cid) && empty($pid) ) { $number_of_categories_in_row = 4; $result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id) FROM gallery_category as c LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id GROUP BY c.category_id" ); while( $row = mysql_fetch_array( $result )) { $result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."<br/></a> "."[".$row[2]."photos]"; } mysql_free_result( $result ); // $result_final = "<tr>\n"; foreach($result_array as $category_link) { if($counter == $number_of_categories_in_row) { $counter = 1; //$result_final .= "\n</tr>\n<tr>\n"; } else $counter++; $result_final .= "\t<td>".$category_link."</td>\n"; } if($counter) { if($number_of_categories_in_row-$counter) $result_final .= "\t<td colspan='".($number_of_categories_in_row-$counter)."'>Â </td>\n"; $result_final .= "</tr>"; } } // Thumbnail Listing else if( $cid && empty( $pid ) ) { $number_of_thumbs_in_row = 5; $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" ); $nr = mysql_num_rows( $result ); if( empty( $nr ) ) { $result_final = "\t<tr><td>No Category found</td></tr>\n"; } else { while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='viewgallery.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>"; } mysql_free_result( $result ); $result_final = "<tr>\n"; foreach($result_array as $thumbnail_link) { if($counter == $number_of_thumbs_in_row) { $counter = 1; $result_final .= "\n</tr>\n<tr>\n"; } else $counter++; $result_final .= "\t<td>".$thumbnail_link."</td>\n"; } if($counter) { if($number_of_photos_in_row-$counter) $result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'>Â </td>\n"; $result_final .= "</tr>"; } } } // Full Size View of Photo else if( $pid ) { $result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" ); list($photo_caption, $photo_filename) = mysql_fetch_array( $result ); $nr = mysql_num_rows( $result ); mysql_free_result( $result ); if( empty( $nr ) ) { $result_final = "\t<tr><td>No Photo found</td></tr>\n"; } else { $result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" ); list($category_name) = mysql_fetch_array( $result ); mysql_free_result( $result ); $result_final .= "<tr>\n\t<td> <a href='viewgallery.php'>Categories</a> > <a href='viewgallery.php?cid=$cid'>$category_name</a></td>\n</tr>\n"; $result_final .= "<tr>\n\t<td align='center'> <br /> <img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' /> <br /> $photo_caption </td> </tr>"; } }// Final Outputecho <<<__HTML_END<html><head> <title>Gallery View</title></head><body><table width='100%' border='0' align='center' style='width: 100%;'>$result_final </table></body></html>__HTML_END;?>atthe firt page it show only category links and when i click on any of the link it go to next page and show all the thumbnail into that category.my question is that on the 1st page i like to show a image too with the category link and it much be changed after every referesh.plaese solve my problem i am much confused Link to comment Share on other sites More sharing options...
justsomeguy Posted August 28, 2009 Share Posted August 28, 2009 Well my answer from post 2 is still the same, so what about that did you not understand? You have this loop printing the links: while( $row = mysql_fetch_array( $result )){ $result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."<br/></a> "."[".$row[2]."photos]";} So the row contains the category ID, category name, and the number of pictures. You need to use the category ID to get a random image from that category. So you need to select a record from gallery_photos where the category ID is the one that you're printing. That query looks like this:SELECT photo_filename FROM gallery_photos WHERE photo_category=123 ORDER BY RAND() LIMIT 1That gets a random photo from category 123. Link to comment Share on other sites More sharing options...
kafia Posted September 5, 2009 Author Share Posted September 5, 2009 Regards, i was out tour and so i miss to connect here for some days. i try the code as you advised me but again its not working , its working but its not showing a thumnail picture. just an empty icon,plese solve my problem my code as i did is as below. while( $row = mysql_fetch_array( $result )) {$result2= mysql_query("SELECT photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."' ORDER BY RAND() LIMIT 1"); //$result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."<br/></a> "."[".$row[2]."photos]"; $result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>"; } //mysql_free_result( $result ); mysql_free_result( $result2); Link to comment Share on other sites More sharing options...
justsomeguy Posted September 8, 2009 Share Posted September 8, 2009 You're not doing anything with $result2, you need to get the record from it using something like mysql_fetch_assoc and then print the value out. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.