Okay, I dont have any code for this I am just not sure at all on how to do what I want. I have a website which displays pictures. There are albums and when you search the album it brings up all the pictures of that album. Now lets say my website only displays 12 images on my wesbite, but the album contains 20 pictures. How can I get the extra 8 images to be placed on a second result page so when they click page 2 of 2 it displays the other 8 images?

Recommended Answers

All 7 Replies

Dear 'garyjohnson', since you posted this topic in the webdesign html css section, I will concider that you are not using the dynamic programming language PHP.
Using html css you just have to make another HTML page let's say the album is in file
named: albumName.html
Copy and paste this page again and name it albumName2.html
and make a link to this page by adding
<a href="albumName2.html">Next Page</a>
in the first page at any place you choose.
Now change the source of the images so they link to the 8 remaining.
If you are using PHP & MySQL let me know because here you will be using the pagination with the LIMIT option in MySQL.
Hope this is clear, for any other inforamtion feel free to ask.

aggreed. if you are just using html, then you have to hardcode the links to your images in the pages, very easy, but useless if you want users to be able to upload their own albums/images.

php is an option indeed, another way to go is jsp/jsp with servlets, ajax, gwt, ...
the possibilities are endless, but we will need to know what exactly it is you are trying to do, and what experience you have, or which language you prefer.

Actually I am using PHP, I didnt know this was possible with PHP. I have a PHP function that pulls image locations out of the mysql database. These images are then displayed onto the webpage but only 12 can fit, so how can i put the other 20 onto another page? My experience is not great, and I have had a lot of help from this website.

how can i put the other 20 onto another page

You mean onto two more pages, right? So Page 1 has 12, Page 2 has 12, and Page 3 has 8.
The way you do this is by passing parameters to the next page indicating what items you wish to retrieve:

<?php
// Going to assume you have a working DB resource var called $conn

// Determine what page you're on, which decides which items you need to retrieve
$page = (!empty($_GET['page'])) ? (int) $_GET['page'] : 1; // if page value was not passed, we'll start at Page #1
$page = ($page < 1) ? 1 : $page; // make sure you can't go lower than first page
$start_at = ($page - 1) * 12; // First page starts at item 0, second at item 12, third at 24, etc...

// Your query. LIMIT will select 12 items starting from row 0 (the first row)
$queryItems = "SELECT * FROM mytable LIMIT $start_at, 12";
/***
 Code that retrieves query results and displays it goes here
 ***/

// Next, create links for pages
$queryNumItems = "SELECT COUNT(*) FROM mytable"; // query counts how many rows in the table
$result = $conn->query($queryNumRows); // our result is a single row...
$row = $result->fetch_row(); // containing the number of rows in the table
$numItems = $row[0]; // this is the number of rows in your table

// Now do some quick math to figure out how many pages you need. 
// Since each page has max 12 results...
$numPages = ceil($numItems / 12)

// Finally, print links to each page:
echo '<ul>';
for($i=1; $i <= $numPages; $i++)
  echo '<li><a href="mypage.php?page='.$i.'">Page '.$i.'</a></li>';
echo '</ul>';
?>

I haven't tested this code, wrote it quickly, it's mostly to get the idea across.

I am understanding this and I appreciate you writing it out like that but i'm still having difficulty. My situation is more complicated. The information I am trying to display is found using a series of array and getting information from the database. This is what it is

<?php
function images(){
//my funtion
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$i=0;
mysql_select_db("characters", $con);
$query="SELECT album FROM albumname ORDER BY RAND() LIMIT 9";
$result= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    $album[] = $row[0];
    $sql_array[] = "SELECT Location FROM {$row[0]} WHERE Pic_id=1"; 
    $sql_array2[] ="SELECT name FROM {$row[0]} WHERE Pic_id=1";
    $i++;
}



$sql = implode(" UNION ALL ", $sql_array);
mysql_select_db("photos", $con);
//The problem occurs here.
$data= mysql_query($sql) or die(mysql_error());
while( $info = mysql_fetch_array( $data )){
   $info2[]= $info[0];  
}  

$sql2 = implode(" UNION ALL ", $sql_array2);
mysql_select_db("photos", $con);
//The problem occurs here.
$data2= mysql_query($sql2) or die(mysql_error());
while( $name = mysql_fetch_array( $data2 )){
   $name2[]= $name[0];  
}  
    $x=0;
    $q=0;
        while($q < $i)
        {
            while($x==0 or $x==5 or $x==10){
echo '<div class="col';
echo $x;
echo '">';
$x++;
}
   echo '<div class="row1">';
   echo '<a href="galleryindividual.php?selection=';
   echo $album[$q];
   echo '"> <img src="uploadedphotos/';
   echo $info2[$q]; 
   echo '" alt="some_text" width="144" height="96"></a></br><div class="text" style="font-size: 12px;" >';
   echo $name2[$q];
   echo '</div>';
   echo '</div>';
   while($x==3 or $x==8 or $x==13){
    echo '</div>'; 
    $x++;
}
   $q++;
   $x++;

        } //END FIRST WHILE LOOP

        }

    ?>

This function actually displays 9 images on each page. I was using 12 as an example. But it displays 9 images onto a page, but there are more than 12 images in the database.

I have been trying to get this code to work with what you gave me, but I am having no luck at all.

Google search pagination in php
There are plenty of standard ways to do this, using mysql queries and manipulating the limit option to pull sets of images at a time from the database.

Thanks everyone for all the help. I have solved the problem and the code I used is this,

<?php
function pagination(){
//my funtion
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$i=0;
mysql_select_db("characters", $con);
$query="SELECT album FROM albumname ORDER BY album DESC ";
$result= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    $album[] = $row[0];
    $sql_array[] = "SELECT Location FROM {$row[0]} WHERE Pic_id=1"; 
    $sql_array2[] ="SELECT name FROM {$row[0]} WHERE Pic_id=1";
    $i++;
}



$sql = implode(" UNION ALL ", $sql_array);
mysql_select_db("photos", $con);
//The problem occurs here.
$data= mysql_query($sql) or die(mysql_error());
while( $info = mysql_fetch_array( $data )){
   $info2[]= $info[0];  
}  

$sql2 = implode(" UNION ALL ", $sql_array2);
mysql_select_db("photos", $con);
//The problem occurs here.
$data2= mysql_query($sql2) or die(mysql_error());
while( $name = mysql_fetch_array( $data2 )){
   $name2[]= $name[0];  
}  





    $adjacents = 1;

    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */




    /* Setup vars for query. */
    $targetpage = "gallery.php";    //your file name  (the name of this file)
    $limit = 9; 
    if(isset($_GET['page'])){                               //how many items to show per page
    $page = $_GET['page'];
    }
    else{$page = 1;}


    $total_pages = ceil($i/$limit);
    if($page!=1) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0



    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages);     //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */

    if($lastpage > 1)
    {   
        echo "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            echo "<a href=\"$targetpage?page=$prev\">previous</a>";
        else
            echo "<span class=\"disabled\">previous</span>";  

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))    //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    echo "<span class=\"current\">$counter</span>";
                else
                    echo "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))     
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        echo "<span class=\"current\">$counter</span>";
                    else
                        echo "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                echo "...";
                echo "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                echo "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";      
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                echo "<a href=\"$targetpage?page=1\">1</a>";
                echo "<a href=\"$targetpage?page=2\">2</a>";
                echo "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        echo "<span class=\"current\">$counter</span>";
                    else
                        echo "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                echo "...";
                echo "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                echo "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";      
            }
            //close to end; only hide early pages
            else
            {
                echo "<a href=\"$targetpage?page=1\">1</a>";
                echo "<a href=\"$targetpage?page=2\">2</a>";
                echo "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        echo "<span class=\"current\">$counter</span>";
                    else
                        echo "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            echo "<a href=\"$targetpage?page=$next\">next </a>";
        else
            echo "<span class=\"disabled\">next </span>";
        echo "</div>\n";     
    }
}
    function images(){
        //my funtion
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$i=0;
mysql_select_db("characters", $con);
$query="SELECT album FROM albumname ORDER BY album DESC ";
$result= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
    $album[] = $row[0];
    $sql_array[] = "SELECT Location FROM {$row[0]} WHERE Pic_id=1"; 
    $sql_array2[] ="SELECT name FROM {$row[0]} WHERE Pic_id=1";
    $i++;
}



$sql = implode(" UNION ALL ", $sql_array);
mysql_select_db("photos", $con);
//The problem occurs here.
$data= mysql_query($sql) or die(mysql_error());
while( $info = mysql_fetch_array( $data )){
   $info2[]= $info[0];  
}  

$sql2 = implode(" UNION ALL ", $sql_array2);
mysql_select_db("photos", $con);
//The problem occurs here.
$data2= mysql_query($sql2) or die(mysql_error());
while( $name = mysql_fetch_array( $data2 )){
   $name2[]= $name[0];  
}



        $limit = 9; 
    if(isset($_GET['page'])){                               //how many items to show per page
    $page = $_GET['page'];
    }
    else{$page = 1;}
    $total_pages = ceil($i/$limit);
    if($page!=1) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else{
        $start = 0; 
    }


        $w=0;
        $x=0;
        $q=0;
        if($start==0){
            $z=9;
        }
        else{
        $f= ($i - $start);
        $z= ($start + $f);
        }
        while($start < $z)
        {
            while($x==0 or $x==5 or $x==10){
echo '<div class="col';
echo $x;
echo '">';
$x++;
}
   echo '<div class="row1">';
   echo '<a href="galleryindividual.php?selection=';
   echo $album[$start];
   echo '"> <img src="uploadedphotos/';
   echo $info2[$start]; 
   echo '" alt="some_text" width="144" height="96"></a></br><div class="text" style="font-size: 12px;" >';
   echo $name2[$start];
   echo '</div>';
   echo '</div>';
   while($x==3 or $x==8 or $x==13){
    echo '</div>'; 
    $x++;
}
   $start++;
   $q++;
   $x++;

        } //END FIRST WHILE LOOP
    }
    ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.