![]() |
PHP and MySQL Pagination
I've been searching the web for basic php/mysql pagination tutorials to learn from since my book didn't cover this and having some problems.
Here's one that I'd like to learn from but it doesn't explain how to display the results on each page. After reading other pagination tutorials, I added a while loop and another query. Here's the link with source code, however I had to make some changes to make it work with mysqli: http://www.tyleringram.com/blog/basi...ation-tutorial Here's the while loop I added before the pages get echoed: Code:
$max = 'limit ' .($page - 1) * $LIMIT .',' .$LIMIT; The url output appears like this: http://www.domain.com/2 http://www.domain.com/3 http://www.domain.com/4 etc..... |
need to let it know which page you're on:
Quote:
|
Quote:
but he must use mod_rewrite to get it... better idea is something like site.com/page/3 or site.com/page-3 both must include mor_rewrite rule if need more help hit me up http://www.awmzone.com/services |
Quote:
Code:
echo "<a href=\"".$_SERVER['PHP_SELF']."/?page={$i}\">{$i}</a>"; |
your echo is wrong:
Code:
echo "<a href=\"".$_SERVER['PHP_SELF']."/?page={$i}\">{$i}</a>"; you don't want the / in there :) try: Code:
echo "<a href=\"".$_SERVER['PHP_SELF']."?page={$i}\">{$i}</a>"; |
Quote:
Right now when I clicked on page 2 or 3 I see the url link in the address bar but the results don't change.. still displays the query results from page 1. |
Code:
if (!$_GET['page'] && !ctype_digit($_GET['page'])) { |
That's basically what I have but it's still not working. Here's my complete code:
Code:
<?php |
|
Code:
if ($page <= 0) { |
i didn't read through the rest of the code past this part:
Quote:
Quote:
|
Quote:
|
Quote:
|
Quote:
|
Sorry one more question about this....
I'm trying to display to results by RAND() and have added the following to the top of the code: Code:
session_start(); How can I have it so when you refresh the page or revisit it randomizes the results but doesn't again when you click on page 2, page 3, etc? I tried adding in the following in a few places but doesn't seem to do anything: Code:
if(isset($_SESSION['rand'])) Code:
$data_p = "SELECT * FROM model WHERE status = 'Active' ORDER BY RAND($rand) $max"; |
Quote:
Code:
$page = (isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1); |
That didn't seem to work Tempest. Anymore ideas?
|
By the way, I'm using php includes and it may be the problem.
File1 has the php session and include codes. File2 has the pagination code. File3 is a template file. |
|
www.stackoverflow.com is your best shot
|
Quote:
So the only thing I can think of is that your session isn't getting set properly. Make sure there is NO output before the session is opened, variables saved etc. Check the source of the page and see if there's any blank lines at the top for example. Turn on error reporting to see if any errors are getting thrown. Put this right at the very start of the script. Code:
ini_set('display_errors', 1); |
All these people offering advice and nobody points out to you that you have a giant gaping massive security hole... never, Never, NEVER use GET or POST variables right in a fucking SQL statement...
|
Quote:
|
Quote:
You mean something like this ? PHP Code:
|
Quote:
'; DROP DATABASE 'XXXXX Byebye data... SQL injection FTL. Check out http://us.php.net/manual/en/mysqli.r...ape-string.php |
Quote:
Code:
$page = mysql_escape_string($_GET['page']); |
See anything wrong with this code?
Code:
<?php echo "rand = ". $_SESSION['rand']; and each page has a different $rand value so it's not being saved... is there something wrong with the way I include files? I didn't find any blank lines in the files or output. |
Quote:
|
Quote:
PHP Code:
and it doesn't work no matter what. Only what i noticed is how this causing query not to execute. |
Quote:
Wait wait wait... Are you saying you don't think SQL injections are possible with uncleaned GET/POST values? |
Quote:
|
Quote:
|
Don't close the session if you want access to the $_SESSION vars... You actually don't ever need to close the session for any normal logic, it happens automatically. The only time you really need to close it manually is if you need to have multiple requests from the same user modify the session data...
Let's say you're doing a large file upload and you want to track progress. Whatever PHP process is being held open by the file upload, you would want to close out the session in that request so that additional requests (checking the progress) would have access to the updated session values... |
In regards to SQL injection...
PHP now (as a safety measure) will not run multiple queries in the same SQL request. That doesn't mean you can't modify a single query to do other things though... |
I just changed $strqry and $data_p and now when I click on page 2 it's showing 0 results.
old queries: Code:
$strqry = "SELECT id from model WHERE status = 'Active' "; Code:
$strqry = "SELECT DISTINCT model.id from model Post #8 has the complete code: https://gfy.com/showpost.php?p=18519612&postcount=8 |
pagination
|
Any ideas?
|
stackoverflow.com , give it a try
|
Quote:
|
All times are GMT -7. The time now is 07:08 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123