Page 1 of 1

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

Posted: Sat Aug 01, 2009 2:18 am
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!

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

Posted: Sat Aug 01, 2009 5:41 am
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;
 

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

Posted: Sat Aug 01, 2009 6:41 am
by irandoct
Dear aceconcepts,
Thank you for your reply!
Regards
Mansour

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

Posted: Sat Aug 01, 2009 10:24 am
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.

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

Posted: Sat Aug 01, 2009 10:42 am
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

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

Posted: Sat Aug 01, 2009 10:54 am
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.

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

Posted: Sun Aug 02, 2009 2:10 am
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?

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

Posted: Sun Aug 02, 2009 2:28 am
by Eran
A div, or an unordered list, both do a nice job

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

Posted: Wed Aug 05, 2009 2:34 pm
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