PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I've created a gallery page that displays images stored in a database dynamically using PHP. That all works fine but now as the site gets bigger I want to try split the images across several pages so as anyone on the site does'nt spend their entire time scrolling. Ideally I'd like 15 or so images per page but the code I'm using won't integrate too well with what I've already done as I'm new to this and not entirely sure what I'm doing.
<?php
include('includes/title.inc.php');
// include MySQL connector function
if (! @include('includes/connection.inc.php')) {
echo 'Sorry, database unavailable';
exit;
}
// create a connection to MySQL
$conn = dbConnect('query');
// prepare SQL to retrieve image details
$sql = 'SELECT * FROM images';
// suvmit the query
$result = mysql_query($sql) or die (mysql_error());
// extract the first record as an array
?>
The links for the pagination appear at the bottom of the page but don't do anything in regards to navigating through anything. They just refresh the gallery. Any help would be fantastic!!
Where are you actually implementing the pagination? You have to use the LIMIT syntax in your SQL query...right now it seems like all you're doing is passing a page # through a URL query, and then not doing anything with it.
You'd need to add it to the end of your query. LIMIT allows two parameters, the starting row, and the rows to display. So, if you're on page 1, and you want 15 images per page, your query would be
i replaced my code with yours but it brought up this error -
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-15, 15' at line 1"
Thats great! its breaks all of the pages up exactly the way i need it to!! The only problem that I have now is that the navigation at the bottom of the page doesnt work. iv tried renaming them and editing the code a little but cant get it to work. changing the page numbers in the address bar works fine though.
the total amount of pages based on the amount of images in the database. its meant to then create the page navigation so as there arent blank pages. im guessing what iv done is very wrong
It's kind of hard to follow what you did for the pagination. From what you posted, it looks like you have the right code to change the page in the URL query, but you just weren't defining $total anywhere.
im kinda glad you said that. it took me ages to get as far as i have! i thought id have to start all over again. were do i need to define it then? can it just go in replace of what i have?