cgm225 Posted February 12, 2008 Share Posted February 12, 2008 I am still relatively new to PHP, and coded this small gallery script (see below). Basically, I wanted to use it as a learning experience, and am interested in getting some constructive criticism regarding it (i.e. what I could do better). I am looking for any feedback, but here are a few areas I am particularly interested in: -logic (are there better ways to do something) -structure of code (are there better ways to separate certain sections of code (procedural code seems to get messy)) -database structure -syntax -use of comments -security -anything else? Thank you all in advance! <?php //Getting required variables $page = $_GET['page']; if (!$page) {$page = 1;} $album = $_GET['album']; //If there is no album directory by that name, will send the user to an error page if (!file_exists("$photos_server_path/photos/$album/")) { header("Location:http://www.kilbad.com/home/error");} //If the photo album is restricted by .htaccess, will also add authentication as well if (file_exists("$photos_server_path/photos/$album/.htaccess")) { general_permissions();} /* Will determine the number of photos to display based on a default value or on a user selected value from a form at the bottom of the page */ if (!$_POST["number_displayed"]) { if (!$_SESSION['number_displayed']) { $number_displayed = 6; } else { $number_displayed = $_SESSION['number_displayed']; } } else { $number_displayed = $_POST["number_displayed"]; $_SESSION['number_displayed'] = $number_displayed; } /*//////////////////////////////////////////////////////////////////////////////////////// // // Database Name: Gallery // Database Structure // // Albums Table: id || full_title || short_title || dir || timestamp // | // +----------------------------------------------------+ // | // Images Table: id || filename || caption date || location || album || timestamp // ////////////////////////////////////////////////////////////////////////////////////////*/ //Connecting to MySQL gallery database db_connect(); //Bringing in album information from MySQL gallery database, album table $query = "SELECT * FROM albums WHERE dir = '$album'"; $album_results = mysql_fetch_array(mysql_query($query)); $album_id = $album_results['id']; $full_title = $album_results['full_title']; //Setting page title plain_page_title($full_title); //Finding the total number of images for any particular album from MySQL gallery database, image table $query = "SELECT * FROM images WHERE album = $album_id"; $number_of_images = mysql_num_rows(mysql_query($query)); //Calculating total number of pages $number_of_pages = ceil($number_of_images / $number_displayed); //When the page number provided in the URL, exceeds the number of pages needed, will reset $page to last page if ($page > $number_of_pages) { $page = $number_of_pages; } //Setting upper and lower limits for queries $lower_limit = (($page * $number_displayed) - $number_displayed); $upper_limit = $lower_limit + $number_displayed; //Retrieving images for any one particular page $query = "SELECT * FROM images WHERE album = $album_id ORDER BY filename ASC LIMIT $lower_limit, $number_displayed"; $image_results = mysql_query($query); echo "\t\t\t<div class='photo_container'>\n"; echo "\t\t\t\t<div class=\"photos_navigation_container\">\n"; echo "\t\t\t\t\t<div class=\"photos_navigation_right\">\n"; //If there are more image entries beyond the upper MySQL limit a "Next" link will be provided if ((mysql_num_rows(mysql_query("SELECT * FROM images WHERE album = $album_id ORDER BY timestamp DESC LIMIT $upper_limit, 1"))) > 0) { echo "\t\t\t\t\t\t<a href='http://www.kilbad.com/photos/$album/" . ($page + 1) . "'>Next</a>\n"; } echo "\t\t\t\t\t</div>\n"; echo "\t\t\t\t\t<div class=\"photos_navigation_left\">\n"; //If the user is beyond the first page, a "Previous" link will be provided if ($page > 1) { echo "\t\t\t\t\t\t<a href='http://www.kilbad.com/photos/$album/" . ($page - 1) . "'>Previous</a>\n"; } else { echo "\t\t\t\t\t\t<span style=\"color:#ffffff;\">_</span>\n"; } echo "\t\t\t\t\t</div>\n"; echo "\t\t\t\t\t<div class=\"photos_navigation_center\">\n\t\t\t\t\t\t"; /* Navigation via a numerical index will be provided with links directly to the two most adjacent pages on either side of the current page number, and then additional links to the starting and ending pages of the image gallery will also be provided, as well as a video link (if there are videos), all such that the output will resemble "<< 3 4 5 6 7 >> | Videos" where 5 is the current page */ $starting_pagination_number = $page - 2; if ($starting_pagination_number <= 1) { $starting_pagination_number = 1; } else { echo "<a href=\"http://www.kilbad.com/photos/$album/1\" title=\"First Page\">«</a> "; } $ending_pagination_number = $page + 2; if ($ending_pagination_number > $number_of_pages) {$ending_pagination_number = $number_of_pages;} for($i = $starting_pagination_number; $i <= $ending_pagination_number; $i++){ if($i == $page){ echo("<span style='font-weight:800;text-decoration:underline;'>".$i."</span> "); }else{ echo("<a href='http://www.kilbad.com/photos/$album/$i'>$i</a> "); } } if ((($number_of_pages / $number_displayed) < 1) AND ($number_of_images != $number_displayed) AND ($i == $page)) { echo("<span style='font-weight:800;text-decoration:underline;'>".$i."</span> "); } if ($ending_pagination_number < $number_of_pages) { echo " <a href=\"http://www.kilbad.com/photos/$album/$number_of_pages\" title=\"Last Page\">»</a>"; } else { } if (file_exists("$videos_server_path/videos/$album")){ echo " | <a href='http://www.kilbad.com/videos/$album'>Videos</a> ";} echo "\n\t\t\t\t\t</div>\n"; echo "\t\t\t\t</div>\n\n"; //Image thumbnails will be displayed in groups equaling the $number_displayed session variable while($row = mysql_fetch_array($image_results)) { $filename = $row['filename']; if (!file_exists("$photos_server_path/photos/$album/thumbnails/small_thumbnail-$filename")) { system("chmod -R 777 $photos_server_path/photos/$album"); list($image_width, $image_height) = getimagesize("$photos_server_path/photos/$album/$filename"); if ($image_height > $image_width) { $small_thumbnail_height = ceil(250 * ($image_width/$image_height)); } else { $small_thumbnail_height = ceil(250 * ($image_height/$image_width));} /* Currently, all photos, regardless of aspect ratio, are resize, and cropped such that their aspect ratio is maintained; however, if cropping into widescreen thumbnails, the following command can be used: system("convert $photos_server_path/photos/$album/$filename -resize 250x -gravity center -quality 100 -crop 250x141+0+0 $photos_server_path/photos/$album/thumbnails/small_thumbnail-$filename"); */ system("convert $photos_server_path/photos/$album/$filename -resize 250x -gravity center -quality 100 -crop 250x$small_thumbnail_height+0+0 $photos_server_path/photos/$album/thumbnails/small_thumbnail-$filename"); system("chmod -R 555 $photos_server_path/photos/$album"); } $exif_data = exif_read_data("$photos_server_path/photos/$album/thumbnails/small_thumbnail-$filename"); $height = "'" . $exif_data["COMPUTED"]["Height"] . "px'"; $width = "'" . $exif_data["COMPUTED"]["Width"] . "px'"; echo "\t\t\t\t<a href='http://www.kilbad.com/photos/$album/$page/$number'><img class='thumbnail' src='$photos_URL_path/photos/$album/thumbnails/small_thumbnail-$filename' height=$height width=$width alt='' /></a>\n"; } //When the total number of images is odd, a spacer image is added to keep the final image aligned to the left if (mysql_num_rows($image_results) % 2) { echo "\t\t\t\t<img class='thumbnail_spacer' src='http://kilbad.com/media/graphics/spacer.jpg' height=$height width=$width alt='' />\n"; } echo "\t\t\t</div>\n"; //The following code below generates the form //to select how many photos to display on each page, and then POST's the //data to this same script ?> <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>" class="number_displayed"> <div class="number_displayed"> Display <select name="number_displayed" size="1" class="number_displayed" onchange="form.submit()"> <option value="2" class="number_displayed" <?php if ($number_displayed == 2) {echo "selected=\"selected\"";} ?>>2</option> <option value="4" class="number_displayed" <?php if ($number_displayed == 4) {echo "selected=\"selected\"";} ?>>4</option> <option value="6" class="number_displayed" <?php if ($number_displayed == 6) {echo "selected=\"selected\"";} ?>>6</option> <option value="8" class="number_displayed" <?php if ($number_displayed == {echo "selected=\"selected\"";} ?>>8</option> <option value="10" class="number_displayed" <?php if ($number_displayed == 10) {echo "selected=\"selected\"";} ?>>10</option> <option value="20" class="number_displayed" <?php if ($number_displayed == 20) {echo "selected=\"selected\"";} ?>>20</option> <option value="40" class="number_displayed" <?php if ($number_displayed == 40) {echo "selected=\"selected\"";} ?>>40</option> </select> <?php //Providing Slideshow link echo "\n\t\t\t\t | <a href='http://www.kilbad.com/slideshow/$album'>Slideshow</a>"; //If there are more image entries beyond the upper MySQL limit a "Next" link will be provided if ((mysql_num_rows(mysql_query("SELECT * FROM images WHERE album = $album_id ORDER BY timestamp DESC LIMIT $upper_limit, 1"))) > 0) { echo " | <a href='http://www.kilbad.com/photos/$album/" . ($page + 1) . "'>Next</a>\n\n"; } else { echo "\n";} //Closing MySQL connection mysql_close(); ?> <noscript><p class="no_style">JavaScript required to change the display number and/or watch the slideshow!</p></noscript> </div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/90784-how-would-you-improve-this-short-gallery-script/ Share on other sites More sharing options...
cgm225 Posted February 13, 2008 Author Share Posted February 13, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/90784-how-would-you-improve-this-short-gallery-script/#findComment-466089 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.