Page 1 of 1

produt gallery HELP

Posted: Mon May 03, 2010 9:41 am
by gpl
Hello,

I am trying to make a page that connects to a DB that has a field with an image directory and its price, and i need to make pagination to show all products in that table but one at each time.
How can i make a previous and next button to go trough the DB table and display all content, showing one product each time and having a previous and next button?

thank you

Re: produt gallery HELP

Posted: Mon May 03, 2010 9:55 am
by rnoack
this is how i would do it :

At the top of your page check if $_GET['currentProduct'] is set, and if so set that as the current product.

When you read the current product from the database, also read the next and previous product to variables.
When the 'next' or 'previous' button is clicked, send those as like GET/POST ie:

Code: Select all

<a href="index.php?currentProduct=<?php echo $previous ?>">Previous</a>
( replace index.php by whatever the current page is called )

then the page will be re-loaded with the correct product as the new current product, and the previous and next values will be re-loaded from the database.


you might want to add some code to detect the there is no next/previous product as well. that is just some if statements.

Re: produt gallery HELP

Posted: Mon May 03, 2010 10:11 am
by gpl
And what would i change in the sql code ?

i got something like this now

Code: Select all

<?php
        $BD=mysql_select_db($database_ligacaobd,$ligacaobd);
        $query= "Select * from galeria" ;
        $resultado = mysql_query($query);
        if ($resultado) {
                        while ($registo = mysql_fetch_array ($resultado)){
                                $imagem = $registo ["imagem"];
                                $imagem2= "backoffice/" . $imagem;              
                                print("<img height=\"350px\" width=\"250px\" src=\"$imagem2\" />");
                        }
        }
        mysql_free_result ($resultado);
?>
but its showing just the first product, i cant get a way of going to the next product on the DB

Re: produt gallery HELP

Posted: Tue May 04, 2010 4:03 pm
by thinsoldier
That query is pulling every record at the same time and showing them all on one page.

Lets call the page you want for showing 1 at a time "item.php"

Code: Select all

[ item.php ]
<?
// database connections, utility functions, other stuff.
include 'common_code.php';

$itemToShow = $_GET['item']; 
// $_GET is an array containing everything after the question mark in the address bar
// you want to have the database id number of the item you want to see in the address of the page
// example:  http://www.galeria.com/item.php?item=64

// this prevents your database from getting hacked
$itemToShow = mysql_real_escape_string($_GET['item']); 

// get the 1 item with the matching id number
$query = "Select * from galeria WHERE galeria_id = '$itemToShow' LIMIT 1 " ;
$resultado = mysql_query($query);
if ($resultado) 
{
   while ($registo = mysql_fetch_array ($resultado)) 
   {  // you might not need to while-loop anymore since you are only dealing with 1 record.
      $imagem = $registo ["imagem"];
      $imagem2= "backoffice/" . $imagem;              
      print("<img height=\"350px\" width=\"250px\" src=\"$imagem2\" />");
   }
}

// After showing the details of the 1 item, you want to have links to the previous and next item

// Get the first record with an id that is less than the current record

$query = "Select * from galeria WHERE galeria_id < '$itemToShow' LIMIT 1 " ;
$prev_resultado = mysql_query($query);
$prev = mysql_fetch_array ($prev_resultado);
?>
<a href="item.php?item=<?php echo $prev['galeria_id'] ?>">Previous</a>

<?
// Get the first record with an id that is greater than the current record

$query = "Select * from galeria WHERE galeria_id > '$itemToShow' LIMIT 1 " ;
$next_resultado = mysql_query($query);
$next = mysql_fetch_array ($next_resultado);
?>
<a href="item.php?item=<?php echo $next['galeria_id'] ?>">Next</a>