Trying to build one page to display rows in a database one at a time controlled

I am in the process of building a website for my wife’s business selling handbags and have created a static site that has a details page for each product she sells. Obviously this is a headache when I need to make a change! I want to get to 1 page that can display the next bag in the database dynamically as a user clicks on a next button. I have got Kevin Yanks teach your self php and got as far as being able to pull a single piece of data out of the database using a mysql select statement inside a php command.

The code I have modified from the book is this:

$counter =1;
$result = mysqli_query($link, “SELECT txtBagName FROM tblBag where lngBagID = $counter”);

while ($row =mysqli_fetch_array($result))
{
$names=$row[‘txtBagName’];
}

include ‘details.html.php’;

what I need to be able to do is somehow automate the increment of the counter to be triggered by the user clicking the next button on my included 'details.html.php template which is the presentation layer with the HTML

Am I going about this the correct way or is there some way that is more appropriate to do this?

I should probably add that I also want a previous button that would display the previous bags details when a user presses it.
my static site is http://www.florenandgilder.com/gallery.html which might help to give a better explanation of what I am trying to automate.

many thanks in advance to anyone who is able to help me!

Hayden

You would need to something like this:


$counter = 1;
if (isset($_GET['counter']) && ctype_digit($_GET['counter'])) {
   $counter = mysqli_real_escape_string($link, $_GET['counter']);
}

$result = mysqli_query($link, "SELECT txtBagName FROM tblBag where lngBagID = $counter");


while ($row = mysqli_fetch_array($result))
{
  $names[]=$row['txtBagName'];
}

include 'details.html.php'; 

Now you can use index.php?counter=1, index.php?counter=2, etc to show the different bags.
To create the next link, just link it to “index.php?counter=”.$counter+1

It is good practice to take a look in the database if the bag actually exists before you link to it, but i’ll let you figure that one out by yourself for now.
Just come back if you have questions :slight_smile:

thanks for the quick reply!! will have a play with this and let you know how I get on. cheers

So I have tried adding the code as follows:

$counter = 1;
if (isset($_GET[‘counter’]) && ctype_digit($_GET[‘counter’])) {
$counter = mysqli_real_escape_string($link, $_GET[‘counter’]);
}

$result = mysqli_query($link, “SELECT txtBagName FROM tblBag where lngBagID = $counter”);

while ($row =mysqli_fetch_array($result))
{
$names=$row[‘txtBagName’];
}

include ‘details.html.php’;

And in the details.html.php file i have created
<a href=“details.php”?counter=“.$counter+1”>Next </a> (where details.php is the equivalent of index.php and contains the code above)

but when I click the next link nothing is happening to either the data or the URL. I was expecting the bag data to update to the next row in the database and the URL to include?counter=x

can you spot anything obvious that I have done wrong?

many thanks
Hayden

It should be


<a href="details.php?counter=<?php echo $counter+1; ?>">Next </a>

:slight_smile:

thanks - that worked a treat. I actually thought that it might have something to do with using echo based on another thing I did the other day so I must be learning!! no I just have to figure out how to make the counter work so that if I have deleted a record in the database it will jump to the next ID rather than throwing an error…

is there a php or mySQL command that moves you to the next record in a database that can be used this? so for example if I have a query like
Select * from table;

to return all of the rows in a table into an array and then use a command on the clicking of the next button to control the return of the next row?

There sure is


SELECT
  * 
FROM
  tblBag
WHERE
  lngBagID > $counter
ORDER BY
  lngBagID ASC
LIMIT 1

That gives you the next available row (or nothing if there is no next row).

Similarly


SELECT
  * 
FROM
  tblBag
WHERE
  lngBagID < $counter
ORDER BY
  lngBagID DESC
LIMIT 1

gives you the previous row (or nothing if there is now previous row).