Image gallery

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
ckm2k1
Forum Newbie
Posts: 2
Joined: Sat Nov 07, 2009 1:56 pm

Image gallery

Post 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);
                                ?>
 
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Hi everyone!

Post 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.
ckm2k1
Forum Newbie
Posts: 2
Joined: Sat Nov 07, 2009 1:56 pm

Re: Image gallery

Post 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 :)
codwrex
Forum Newbie
Posts: 5
Joined: Wed Jan 20, 2010 3:38 am

Re: Image gallery

Post by codwrex »

but why use can extra query to find the no. of pages?
Post Reply