Page 1 of 1

Image gallery

Posted: Sat Nov 07, 2009 2:02 pm
by ckm2k1
Hello,

I must start by saying that I don't really have a specific question, rather a critic request if you wish. I wrote some php code to generate an image gallery on the fly, navigation and everything. It basically fetches the amount of rows in the database to find out how many images are there, calculates the amount of pages necessary and then writes 8 images per page with a navigation bar at the bottom of the page. What I'd like is for you guys to take a look at the code and give some suggestions, pointers, or any other advice regarding the quality of the code. Is it crappy and messy? is it OK but could be much better? is it a stroke of genious? (I doubt that last one...).
Your help is very appreciated!

Code: Select all

 
                    include 'db/dbconnect.php';
                                    
                    if (!($pageNum = $_GET['page'])) $pageNum = 0;
                                                                       
                                        $query = 'SELECT COUNT(*) FROM galleryData';
                                        //$query = "SELECT * FROM galleryData";                 
                                        $result = mysql_query($query);
 
                                        
                                        if (!$result) {
                                            die("Unable to select:" . mysql_error() );
                                        }
                                        else {
                                            $total = mysql_fetch_array($result);
                                            $totalPics = $total[0];
                                            if ($totalPics < 8) { 
                                                  $totalPages = 0;
                                            }
                                            else {
                                                //devide by 8 to find out how many pages we need. 
                                                $temp = floor($totalPics / 8);
                                                //do a mod 8 to find out if we have an uneven # of pictures and 
                                                // in consequence create an additional page. 
                                                if ( ($totalPics % 8) > 0)
                                                  $totalPages = $temp;
                                                else
                                                   $totalPages = $temp - 1;
                                                 }
                            if ($pageNum > $totalPages) {
                            echo 'Sorry, it seems the page you\'re looking for no longer exists.<br>';
                        echo 'Click here to go to main gallery page: <a href="itemsForSale.php">Main</a>';
                        exit();
                        }
                                      }
                                    
                                    if (!$pageNum){ 
                                        $query = 'SELECT * FROM galleryData ORDER BY id DESC LIMIT 0,8';
                                        $pictureList = mysql_query($query);
                                        if (!$pictureList)
                                            die("Unable to select:" . mysql_error());                                       
                                    }
                                    else {
                                        $query = 'SELECT * FROM galleryData LIMIT '. ($pageNum * 8) . ',8';
                                        $pictureList = mysql_query($query);
                                        if (!$pictureList)
                                            die("Unable to select:" . mysql_error());
                                    }
                                        
                                    
                                    //loop through the picture list and create each picture container on the page. 
                                    while($row = mysql_fetch_assoc($pictureList)) {
                                        //Variables initialization. 
                                        $fileName = $row['fileName'];
                                        $pathToFile= $row['pathToFile'];
                                        $pathToThumb = $row['pathToThumb'];
                                        $galleryGroup= $row['group'];
                                        $description = $row['description'];
                                        $price = $row['price'];
                                                    
                                        //write out the thumbnail and link
                                        echo '<div class="imgFloat">' .
                                            '<a href="' . $pathToFile . $fileName . '" rel="'. $galleryGroup . '" class="fancyboxLink">' . 
                                            '<img src="' . $pathToThumb . $fileName . '" class="gallery"></a><br>';
                                        
                                        //write out the image description
                                        echo '<p class="description"><span class="colored">Description:</span> '. $description . 
                                            '<br><span class="colored">Price:</span> '.$price.'$ </p></div>';
                                        
                                    }
                                    
                                    // This section takes care of the navigation at the bottom of the page. */  
                                        //if we only have one page the navigation is not necessary. get out. 
                                        if (!$totalPages) {
                                            //get out
                                        }
                                        else {
                                        //otherwise figure out where to point the various back,next and numbering. 
                                            if (!$pageNum)
                                                $back = $pageNum;
                                            else
                                                $back = $pageNum - 1;
                                            //set the forward nav point. 
                                            if ($pageNum < $totalPages)
                                                $next = $pageNum + 1; 
                                            else
                                                $next = $pageNum;
 
                                            echo '<div style="clear: both;"></div>';
                                            echo '<div id="galleryNav">';
                                            echo '<a href="/itemsForSale.php?page=' . $back . '">Back</a> | ';
                                            for ($i = 0 ; $i <= $totalPages ; $i++ ) {
                                                echo '<a href="itemsForSale.php?page=' . $i . '">' . $i . '</a> | ';
                                            }
                                            echo '<a href="/itemsForSale.php?page=' . $next . '">Next</a>';
                                            echo '</div>';
                                            
                                        }
                                    
                                    mysql_close($con);
                                ?>
 
 

Re: Hi everyone!

Posted: Sat Nov 07, 2009 2:06 pm
by John Cartwright
Please use titles that describe the thread, use the proper tags when using php code, and read all the forum descriptions before posting.

Moved to Code Critique, added php tags, changed title.

Re: Image gallery

Posted: Sat Nov 07, 2009 2:09 pm
by ckm2k1
So sorry, that was a real brain fart on my part... I'll make sure to look at the forum headers next time :)

Re: Image gallery

Posted: Thu Jan 21, 2010 5:08 am
by codwrex
but why use can extra query to find the no. of pages?