Pagination

No problem. Hopefully you learned something and anyone that reads this thread may learn something. That’s why we’re here :wink:

And i’m back lol … I would be really greatfull if you could take a look and see where i am going wrong with this one … I’m passing “make” over to this results page like so

<li><a href="make.php?make=Abarth">Abarth</a></li>

It seems to work, in that it displays results and pagination correctly. However similarly to a previous error i had, it doesn’t actually pass the make to the second page … I have included the variable in the URL for each page (i think) but it’s not passing it over correctly and displays

No rows found, nothing to print so am exiting

I can see when i hover over the link for pagination that it shows the variable as blank … like so

http://mydomain.com/make.php?make=&page=2

So for some reason the make variable isn’t being declared properly, probably because i have made a very silly error, but i can’t for the life of me see where.

Code is below, if any body can spot it.

[php]<?php
// Don’t forget to include your pagination class first
include ‘pagination.php’;

// Setup pagination
$pag = new Pagination();

// Query the total number of results
$sql = "SELECT COUNT() FROM vehicles WHERE make=’".$_GET[‘make’]."’";
$result = mysql_query($sql)or die(mysql_error()); // query
$row = mysql_fetch_row($result); // fetch row
$total_results = $row[0]; // results of COUNT(
)

// note that the ‘page_var’ is defaulted to ‘p’ this can be changed to anything
$pag->setPageVar(‘page’); // now it will look for ?page=X

// set total count in pagination class
$pag->setResults($total_results);

// this defines the number of page links to display at a time
// e.g. if set to 5, you would have an array like << 1 2 3 4 5 >> or << 3 4 5 6 7 >>
$pag->setPageLinks(5);

// set the number of results to display per page
$pag->setPageResults(12);

// build $pages array see pagesInfo() function or print_r($pages)
$pages = $pag->pagesInfo();

// now we can query using offset and limit
$sql=“SELECT * FROM vehicles WHERE make=’”.$_GET[‘make’]."’ LIMIT " . $pages[‘Db_Offset’] . ", " . $pages[‘Page_Results’];

// the following code is unchanged

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo “No rows found, nothing to print so am exiting”;
exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you’re expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you’ll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
?>

<a href=“one-products.php?vehicle_id=<?php echo $row["vehicle_id"]; ?>“class=“single-image picture”>
<img src=”<?php $parts = explode(',', $row["imagerefs"]); echo $parts[0]; ?>”>



<a href="one-products.php?vehicle_id=<?php echo $row["vehicle_id"]; ?>"class=“title-item”><?php echo $row["title"]; ?>

£<?php echo $row["price"]; ?>


  • Engine:<?php echo $row["engine"] ?>

  • Mileage:<?php echo $row["mileage"]; ?>

  • Year:<?php echo $row["year"]; ?>

  • Location:<?php echo strstr($row["seller_email"], '@', true); ?>

  • Reference:<?php echo $row["vehicle_id"]; ?>



<a href="one-products.php?vehicle_id=<?php echo $row["vehicle_id"]; ?>"class=“button orange”>Details »

<?php } mysql_free_result($result); ?>
			</section><!--/ #change-items-->				

			<div class="wp-pagenavi clearfix">
<?php foreach($pages['Page_Numbers'] as $page_num) { echo '' . $page_num . ' '; } ?> [/php]

Thanks again !!

In your queries you are using $_GET[‘make’] in your links you are using $make (which is undefined)

Well that was rather straight forward lol … i knew it was something straight forward but i just couldn’t see it. Thank you very much for your help !!

While i’m here may i ask, did you build the pagination script yourself ?

I was wondering if there is any reference material available for it. I kinda wanted to improve the display of the controls. At the moment i have just numbers displaying, without arrows etc … I know you told me the functionality exists already, but i don’t really understand how to show arrows and highlite current page etc.

Just wondering if there is any docs available that i could learn more from ?

Thanks again for your outstanding support as always !!

Well you can really design it however you want…

[php]
foreach($pages[‘Page_Numbers’] as $page_num) {
if ($pages[‘Current_Page’] == $page_num) {
// this is the current page
echo ‘’ . $page_num . ’ ';
}
else {
// this is every other page
echo ‘’ . $page_num . ’ ';
}
}
[/php]

I’ll post another example later using some better code with CSS

Thanks for your reply m@tt, that got me around the current page option. Is it possible to show also something like this

Page 1of 6 << 1 2 3 4 5 6 >>

Obviously with the arrows for next or previous and maintaining the current page ?

Thanks again !

Yes: http://www.yournetpal.com/AdBrowse.php

At the top of the page I collect the parameters, I also preserve some of the URL parameters, whilst discarding others and store them in $strURLparameter, this is so that you can search and filter the page and return to it after you’ve gone into a details page, perhaps to edit a record where a form has been submitted and you would otherwise lose the filter parameters. This allows me to store $strURLparameter in a single hidden field, rather than collecting all the URL parameters individually and storing them separate hidden fields.

[php]if (isset($_GET[“RF”])) {$intRecFirst=funcStripText($_GET[“RF”]);} else{$intRecFirst=0;}//First Row
if (isset($_GET[“RT”])) {$intRecTotal=funcStripText($_GET[“RT”]);} else{$intRecTotal=0;}//Total Records
//------------------- Preserve the Filter Parameters ------------------------
$strDiscardParameters = “&D=&RF=&RT=&RPage=&Submit=&BID=”;
$strUrlParameter = “”;
if (isset($_GET)){
foreach($_GET as $Parameter => $Value){
if (strrpos($strDiscardParameters, “&” . $Parameter . “=”)===false){ $strUrlParameter = $strUrlParameter . “&” . $Parameter . “=” . $Value;}
}
}
//------------------- End Preserve the Filter Parameters ------------------------[/php]

Here’s the paging code which allows you to specify the page name, the records per page and how many pages you have in set of links. So :

$intPageSet=10;

So if you were on page 5 it would give you this:

Prev 10 1 2 3 4 5 6 7 8 9 10 Next 10

If your on page 1, the Prev 10 link wouldn’t be there, similarly if there are less than 10 pages, the links will stop at the relevant page and Next 10 link will be omitted.

$intPageSet=5;

Would give you this:

Prev 5 1 2 3 4 5 Next 5

etc.

You can see that better here on an ASP site, where there are more records: http://www.starbrite.co.uk/ProductView.asp?Count=200&Total=388

At the beginning, I’ve created a query just to count the records:

[php]if ($intRecTotal == 0){
$pdo = $connBike->prepare(“SELECT BikeID FROM” . $strSQLFROM . $strSQLWHERE);
$pdo->execute();
$intRecTotal = $pdo->rowCount();
$pdo->closeCursor();
}[/php]

This only happens when you first visit the page, thereafter I have the $intRecTotal parameter in the URL parameters.

I’ve done this so that I can then use LIMIT with the query that retrieves the data for display on the page:

[php]
$pdo = $connBike->prepare(“SELECT tbl_bikes.BikeID,tbl_bikes.TypeID,tbl_bikes.BikeName,tbl_bikes.BikeEra … FROM” . $strSQLFROM . $strSQLWHERE . " LIMIT " . $intRecFirst . “,” . $intRecPage . “;”);
$pdo->execute();
$arrView = $pdo->fetchAll();
$pdo->closeCursor();
[/php]

Here’s the bit that calculates the previous, next and last pages and makes sure we don’t try to go beyond the last record or show links to pages that don’t exist.

[php]
//------------------- Start Paging -------------------
if ($intRecTotal == 0){
$pdo = $connBike->prepare(“SELECT BikeID FROM” . $strSQLFROM . $strSQLWHERE);
$pdo->execute();
$intRecTotal = $pdo->rowCount();
$pdo->closeCursor();
}

$strPageName = “AdBrowse.php”; //The page name
$intRecPage=10; //Records per page
$intPageSet=10; //Pages per set
$intFirstPage = 0;
$intPrevPage = $intRecFirst - $intRecPage;
if ($intPrevPage <0){$intPrevPage =0;}
$intNextPage = $intRecFirst + $intRecPage;
$intLastPage = (intval($intRecTotal / $intRecPage) * $intRecPage);
if ($intLastPage==$intRecTotal) {
$intLastPage = $intLastPage-$intRecPage;
}
$intThisPage = $intRecFirst / $intRecPage + 1;
$intTotalPages = ($intRecTotal + 1) / $intRecPage;

$intLastPageRec = $intRecFirst + $intRecPage;
if ($intLastPageRec > $intRecTotal){$intLastPageRec = $intRecTotal;}

if (ceil($intTotalPages) < $intTotalPages){
$intTotalPages = (ceil($intTotalPages) + 1);
}
else{
$intTotalPages = ceil($intTotalPages);
}

if ($intRecFirst <= ($intRecPage * $intPageSet) - $intRecPage){
$intStartPage = 1;
$intPrevSet = 0;
$intEndPage = $intPageSet;
}
else{
$intStartPage = (((ceil($intRecFirst/$intPageSet)) - ((ceil($intRecFirst/$intPageSet)) % $intRecPage)) * ($intPageSet/$intRecPage))+1;
$intEndPage = $intStartPage + ($intPageSet - 1);
$intPrevSet = (($intStartPage - 1) * $intRecPage) - ($intRecPage * $intPageSet);
}
if ($intEndPage > $intTotalPages) {$intEndPage = $intTotalPages;}
$intEndPageCount = $intEndPage;
if ($intRecTotal % $intRecPage == 0) {
$intEndPageCount = $intEndPage - 1;
}
$strLinkParameter = “&RPage=” . $strPageName . “&RF=” . $intRecFirst . “&RT=” . $intRecTotal . $strUrlParameter;
//------------------- End Paging -------------------
[/php]

This is my navigation bar/buttons in an include:

[php]

<?php echo "Record: " . ($intRecFirst+1) . " to " . $intLastPageRec . " of " . $intRecTotal ?> <?php echo '   Page: '; if ($intStartPage > 1){echo ' Previous ' . $intPageSet . '  -  ';} for ($intPageCount = $intStartPage; $intPageCount <= $intEndPageCount; $intPageCount++){ if ($intPageCount == $intThisPage){ echo '' . $intPageCount . ' '; } else{ echo '' . $intPageCount . ' '; } } if ($intEndPage < $intTotalPages) { echo ' -  Next ' . $intPageSet . ' ';} echo '   '; ?>
<?php if (($intRecFirst-$intRecPage)>=0){ ?>First <?php }?> <?php if (($intRecFirst-$intRecPage)>=0){ ?>Previous <?php } ?> <?php if (($intRecFirst+$intRecPage)<($intRecTotal)){ ?>Next <?php }else{ ?> <?php }?> <?php if (($intRecFirst+$intRecPage)<($intRecTotal)){ ?>Last<?php }else{ ?><?php }?>
[/php]

Thanks for that, but i already have my pagination in place and working. m@tt kindly helped me out with a pagination script a couple of weeks ago and it already has everything in place to display what is required, i just haven’t got my head around displaying the actual navigation options outside of page numbers, but it is all working !

Thanks again for your help !

Sponsor our Newsletter | Privacy Policy | Terms of Service