Jump to content


Photo

multiple pages for a gallery problem


  • Please log in to reply
2 replies to this topic

#1 chuddyuk

chuddyuk
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationEngland

Posted 14 June 2006 - 08:22 PM

I am trying to make a simple gallery. but i want to have pages for it.so that say only ten images show up on a page and then there are links to the next page or previous page. iv looked at some tutorials on the net and cant really get them to work. here is my code:

<?php

include 'connect.php';


$sql = "SELECT *
       FROM  gallery";

$result = mysql_query($sql);

if (!$result) {
   echo "Could not successfully run query ($sql) from DB: " . mysql_error();
   exit;
}

if (mysql_num_rows($result) == 0) {
   echo "No rows found";
   exit;
}

while ($row = mysql_fetch_assoc($result)) {

$id = $row["id"];
$name = $row["name"];
$des = $row["des"];
$alt = $row["alt"];
$src = $row["src"];
$date = $row["date"];
}

mysql_free_result($result);

?> 

anyone able to help me?

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 14 June 2006 - 09:05 PM

There's been an effort made on these boards to minimize repetitive questions/threads. Please do a search on the site for "Pagination". There's even a tutorial on the subject.
PHP Ninja

#3 chuddyuk

chuddyuk
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationEngland

Posted 14 June 2006 - 09:39 PM

Thanks for that, i wasnt sure what it was called. i tried the tutorial and it still isnt working. here is my code:

<?php

include 'connect.php';


    $limit          = 5;               
    $query_count    = "SELECT count(*) FROM gallery";    
    $result_count   = @mysql_query($query_count);    
    $totalrows      = @mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM gallery LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

while ($row = mysql_fetch_assoc($result)) {

$id = $row["id"];
$name = $row["name"];
$des = $row["des"];
$alt = $row["alt"];
$src = $row["src"];
$date = $row["date"];
}


    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");
    
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor.">n<td>");
    echo"$name";
    echo("</td>n<td>");
    echo"<img src=thumbs/$src>";
    echo("</td>n</tr>");
    }

    echo("</table>");

    if($page != 1){ 
        $pageprev = $page--;
        
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
    }else{
        echo("PREV".$limit." ");
    }

    $numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
    }else{
        echo("NEXT".$limit); 
    }
    
    mysql_free_result($result);



?> 
Any ideas :'(




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users