how to show multiple mysql records per a row (gallery like)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
irandoct
Forum Newbie
Posts: 5
Joined: Sat Aug 01, 2009 2:10 am

how to show multiple mysql records per a row (gallery like)

Post by irandoct »

Hi all,
i'm creating a galley with php and mysql . how i can show X photo per row?! my php please see my code:

Code: Select all

//gallery class
class gallery{
var $galleylimit = GALLERY_LIST; // number of photo in a page
var $galleycol = GALLERY_COL;    // number of photo in a row
//list all photos
function list_gallery(){
     $db = new Database;
     $ImageHeight = "0";
     $ImageWidth = "75";
     $theme = new theme();
     $default_theme = $theme->default_theme();
     $sitelang = new language();
     $sitelang = $sitelang -> site_lang();
     $imagespath = "themes/$default_theme/images";
     include "header.php";
     $block = new blocks;
     $block = $block->block(EW_PAGE_ID, EW_TABLE_NAME, 3);
     echo $block;
     $Security = new cAdvancedSecurity();
     if (CurrentUserLevel() <> ""){
     if (!$Security->IsAdmin()){
     if(@$_GET["category"] <> ""){
     $galleryfilter = "WHERE approved = 'Y' AND category = '".@$_GET["category"]."' AND (user_group = " . CurrentUserLevel() . " OR user_group is null) AND (language = '$sitelang' OR isnull(language))";
     }else{
     $galleryfilter = "WHERE approved = 'Y' AND (user_group = " . CurrentUserLevel() . " OR user_group is null) AND (language = '$sitelang' OR isnull(language))";
     }
     } else {
     if(@$_GET["category"] <> ""){
     $galleryfilter = "WHERE category = '".@$_GET["category"]."' AND (language = '$sitelang' OR isnull(language))";
     } else {
     $galleryfilter = "WHERE language = '$sitelang' OR isnull(language)";
     }
     }
     } else {
     if(@$_GET["category"] <> ""){
     $galleryfilter = "WHERE category = '".@$_GET["category"]."' AND approved = 'Y' AND (user_group is null) AND (language = '$sitelang' OR isnull(language))";
     } else {
     $galleryfilter = "WHERE approved = 'Y' AND (user_group is null) AND (language = '$sitelang' OR isnull(language))";
     }
     }
            if(!isset($_GET['page'])){
        $page = 1;
        } else {
        $page = ew_AdjustSql($_GET['page']);
        }
        $from = (($page * $this->galleylimit) - $this->galleylimit);
        $total_results = mysql_num_rows(mysql_query("SELECT * FROM gallery $galleryfilter"));
        $query = mysql_query("SELECT * FROM gallery $galleryfilter ORDER by id DESC LIMIT $from, $this->galleylimit");
        $galleryscount = mysql_num_rows($query);
        if ($galleryscount > 0){
// showing photos
       $total_pages = ceil($total_results / $this->galleylimit);
       if ($total_pages > 1){
         echo "<form method=\"post\" action=\"gallery.php\" name=\"pageform\" title=\"pageselection form\">";
         echo "<label>".PAGE.": <select id=\"pageselect\" size=\"1\">";
            for($i = 1; $i <= $total_pages; $i++){
            if($page == $i){
            $sel = "selected=\"selected\"";
            } else {
            $sel = "";
            }
            echo "<option value=\"gallery.php?category=".$_GET["category"]."&page=$i\" $sel>$i</option>\n";
            }
           echo "</select></label>";
           echo "<input onclick=\"gotourl(document.getElementById('pageselect'))\" value=\"".GOTOPAGE."\" type=\"button\" />";
           echo "</form>";
            }
      echo "<script language=\"javascript\" type=\"text/javascript\">
      function gotourl( mySelect ) {
      myIndex = mySelect.selectedIndex;
      myValue = mySelect.options[myIndex].value;
      window.location.href = myValue;
      }
       </script>";
}else{
echo "<br/><br/><font color=\"red\">".NOPHOTO."</font>";
echo "<br/><br/><a href=\"javascript&#058;history.go(-1)\" title=\"".GOBACK."\"><img src=\"images/back.jpg\" width=\"49\" height=\"17\" border=\"0\" alt=\"".GOBACK."\" /></a>";
}
     $block = new blocks;
     $block = $block->block(EW_PAGE_ID, EW_TABLE_NAME, 4);
     echo $block;
include "footer.php";
}
}
please advise!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: how to show multiple mysql records per a row (gallery like)

Post by aceconcepts »

If you're looking to display a gallery which displays something like 3 rows by 3 columns you can use this:

Assuming you have already setup your query.

Code: Select all

 
$output = '';
    
# display results
$output .= "<table width=\"100%\">\n\n";
$howmany = mysql_num_rows($query);
$rowmax = 3;
for($x = 0; $row = mysql_fetch_array($query); $x++)
{               
if($x % $rowmax == 0)
$output .= "<tr>\n";
                                    
$output .= '<td valign="top" align="center" style="padding-bottom:20px;"><a href="" title=""><p>'.$row['strSubCategory'].'</p></a></td>';
                    
if($x % $rowmax == $rowmax - 1)
$output .= "\r</tr>\n\n";
}
            
if($left = (($howmany + $rowmax - 1) % $rowmax))
$output .= '<td colspan="' . $left . '">' . "</td>\n</tr>\n\n";
            
$output .= "</table>\n\n";
            
$output .= "</DT></P>";
 
echo $output;
 
irandoct
Forum Newbie
Posts: 5
Joined: Sat Aug 01, 2009 2:10 am

Re: how to show multiple mysql records per a row (gallery like)

Post by irandoct »

Dear aceconcepts,
Thank you for your reply!
Regards
Mansour
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: how to show multiple mysql records per a row (gallery like)

Post by insight »

On a similar topic, is there a way to get images to automatically continue on the next line if the screen is to small. For example, on larger screens you can fit maybe 6-7 images per row (say on a 1920x1080 or 1680x1050) but on smaller screens it may only be able to fit 3-4 images. Is there a way to get images to drop to the next line in order if the screen is too small?

Much the same way as text drops to the next line when you decrease the size of the browser window.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: how to show multiple mysql records per a row (gallery like)

Post by jackpf »

You'd have to use javascript. If you do something like

Code: Select all

var imagesPerRow = window.innerWidth / imageWidth;
you could get how many images per row the window will take.

Then you'd have to do some rather fancy rearranging...... I might have a go a bit later if I have nothing better to do :P
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: how to show multiple mysql records per a row (gallery like)

Post by Eran »

The simplest way to create a dynamic layout for this purpose is using CSS instead of tables to display images (I'd recommend that regardless). Float the images and assign them a fixed width, they will arrange themselves according to the space available on each row.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: how to show multiple mysql records per a row (gallery like)

Post by insight »

pytrin wrote:The simplest way to create a dynamic layout for this purpose is using CSS instead of tables to display images (I'd recommend that regardless). Float the images and assign them a fixed width, they will arrange themselves according to the space available on each row.
Yes indeed, would it be better to put the images in say a div tag and then float them?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: how to show multiple mysql records per a row (gallery like)

Post by Eran »

A div, or an unordered list, both do a nice job
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: how to show multiple mysql records per a row (gallery like)

Post by aceconcepts »

For those of you interested in a directory-based gallery script take a look at this: http://www.dynamicdrive.com/dynamicinde ... oalbum.htm

It basically loads images from a specified directory into an array and displays a specified quantity of images per viewing/page e.g. 3 across x 3 down etc...

As it uses an array and is based on JavaScript and PHP there is no need to reload the entire page.

Enjoy :D
Post Reply