bigheadedd Posted April 26, 2010 Share Posted April 26, 2010 Hi! I'm a little stuck with something I know is quite simple! What i'm stuck on doing is the following.. On the full size image section (Last else if) I want to have a 'previous and next' buttons. Where it takes you to the next/previous image in the category. I thought I could achieve this through using the ID, however this takes into account all of the other categories too which I don't want. Please let me know if i'm not making any sense Many thanks -E $result_array = array(); $counter = 0; $cid = (int)($_GET['cid']); $pid = (int)($_GET['pid']); $view = (string)($_GET['view']); //Display Categories if ($view == "text") { if( empty($cid) && empty($pid) ) { $number_of_categories_in_row = 1; $result = mysql_query("SELECT category_id,category_name,category_colour, published FROM art_category WHERE published='1'"); while( $row = mysql_fetch_array( $result ) ) { $var_array = explode("/", $PATH_INFO); $result_array[] = "<a style=\"color:#".$row[2]."\" href=\"artwork.php?cid=".$row[0]."\">".$row[1]."</a>"; } mysql_free_result( $result ); $result_final = "<li>\n"; foreach($result_array as $category_link) { if($counter == $number_of_categories_in_row) { $counter = 1; $result_final .= "\n</li>\n<li>\n"; } else $counter++; $result_final .= "\t".$category_link."\n"; } if($counter) { if($number_of_categories_in_row-$counter) $result_final .= "</li>"; } } } //LISTING else if( $cid && empty( $pid ) ) { $number_of_thumbs_in_row = 1; $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM art_photos WHERE photo_category='".addslashes($cid)."' ORDER BY photo_id DESC" ); $nr = mysql_num_rows( $result ); if( empty( $nr ) ) { $result_final = "No Photos Found!"; } else { while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='artwork.php?cid=$cid&pid=".$row[0]."'><img src='".$artwork_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></br>".$row[1]."</a>"; } mysql_free_result( $result ); $result_final = "<li>\n"; foreach($result_array as $thumbnail_link) { if($counter == $number_of_thumbs_in_row) { $counter = 1; $result_final .= "\n</li>\n<li>\n"; } else $counter++; $result_final .= "\t".$thumbnail_link."\n"; } } } //Full size image else if( $pid ) { $result = mysql_query( "SELECT photo_caption,photo_filename FROM art_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 art_category WHERE category_id='".addslashes($cid)."'" ); list($category_name) = mysql_fetch_array( $result ); mysql_free_result( $result ); $result_final .= "<img src='".$artwork_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/199774-image-gallery-help/ Share on other sites More sharing options...
Psycho Posted April 26, 2010 Share Posted April 26, 2010 What I understand of your request is that you are displaying an image that belongs to a specific category and when the user selects next you want the next image - in that category - to be displayed. I'm not going to wade through your code to try and understand it, but you would be better off having an explicit field for the sequence of the images instead of relying upon the image IDs. It would make this functionality much easier and the database format would be better off as well. Anyway, here are a couple of solutions: 1. Using your current design you could do two additional queries when displaying an image: one to get the prev photo id and one to get the next photo id. Example: $cid = mysql_real_escape_string($cid); $pid = mysql_real_escape_string($pid); //Query to get the next photo $query = "SELECT id FROM art_photos WHERE photo_category='{$cid}' AND id > '{$pid}' LIMIT 1"; //Query to get the prevphoto $query = "SELECT id FROM art_photos WHERE photo_category='{$cid}' AND id < '{$pid}' LIMIT 1"; You could then append the appropriate image IDs to the prev/next links 2. Alternatively, the next/prev link could simply pass two variables a) the current photo ID and b) either 'next' or 'prev'. Using those two variables you could first determine the category of the image that was viewed and then query for the next or prev image n that category. This would be a more efficient solution. Since you would not need to run the two queries identified in #1 each time an image is displayed. However, if you would not be able to determine if the prev/next links should be disabled in this process if the image being viewed is the first/last. Quote Link to comment https://forums.phpfreaks.com/topic/199774-image-gallery-help/#findComment-1048604 Share on other sites More sharing options...
bigheadedd Posted April 28, 2010 Author Share Posted April 28, 2010 Hi, Thanks for that! I managed to get it working! I am however, now stuck on another matter! I am now trying to have a page where you can view all of the images as thumbnails. The following code, does just that, however I'm having difficulty trying to get it to do the following: Display a row of images per category. For example.. (As shown on a page). CAT1(IMAGE 1), CAT1(IMAGE 2), CAT1(IMAGE 3) CAT2(IMAGE 1), CAT2(IMAGE 2) CAT3(IMAGE 1) etc etc.. At the moment its just doing it all on one line. I know its a relatively simple script to get it working, but no luck Thanks! else if ($view == "image") { $number_of_thumbs_in_row = 0; $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename, photo_category FROM art_photos ORDER BY photo_category AND photo_id ASC " ); $nr = mysql_num_rows( $result ); if( empty( $nr ) ) { $result_final = "\t<tr><td>No Photos Found!</td></tr>\n"; } else { while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='manage_art.php?cid=".$row[3]."&pid=".$row[0]."'><img src='../".$artwork_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></br></a></br></br>"; } mysql_free_result( $result ); $result_final = "<li>\n"; foreach($result_array as $thumbnail_link) { if($counter == $number_of_thumbs_in_row) { $counter = 1; $result_final .= "\n</li>\n<li>\n"; } else $counter++; $result_final .= "\t".$thumbnail_link."\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 .= "</li>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/199774-image-gallery-help/#findComment-1049829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.