Pagination

Hi folks, really hope someone can help me out here. I’m basically displaying results from database using mysql and i would love to introduce some pagination. Currently i use a loop to show only 12 lines but i’m really not sure how to add pagination to show 12 lines per page. I’ll post my code so far and maybe somebody can point me in the right direction.

[code]

<?php include 'config.php'; ?> <?php // Connect to the database $query="SELECT * FROM vehicles"; $result=mysql_query($query); $num=mysql_numrows($result); $i=1; while ($i<=12) { $f1=mysql_result($result,$i,"imagerefs"); $f2=mysql_result($result,$i,"make"); $f3=mysql_result($result,$i,"price"); $f4=mysql_result($result,$i,"varianttext"); $f5=mysql_result($result,$i,"mileage"); $f6=mysql_result($result,$i,"year"); $f7=mysql_result($result,$i,"vehicle_id"); $f8=mysql_result($result,$i,"engine"); $f9=mysql_result($result,$i,"title"); $f10=mysql_result($result,$i,"model"); $f11=mysql_result($result,$i,"reg"); $f12=mysql_result($result,$i,"bodystyle"); $f13=mysql_result($result,$i,"transmission"); $f14=mysql_result($result,$i,"fueltype"); $f15=mysql_result($result,$i,"colour"); $f16=mysql_result($result,$i,"cap_features"); $f17=mysql_result($result,$i,"seller_email"); ?>
				<article>
					<a href="one-products.php?vehicle_id=<?php echo $f7; ?>"class="single-image picture">
						<img src="<?php $parts = explode(',', $f1); echo $parts[0]; ?>">
					</a>

					<div class="detailed">
						
						<h6>
						     
						        <a href="one-products.php?vehicle_id=<?php echo $f7; ?>"class="title-item"><?php echo $f9; ?></a>
							
						</h6>
						
						<span class="price">£<?php echo $f3; ?></span>
						
						<div class="clear"></div>
						
						<ul class="list-entry">
							<li><b>Engine:</b><span><?php echo $f8; ?></span></li>
							<li><b>Mileage:</b><span><?php echo $f5; ?></span></li>	
							<li><b>Year:</b><span><?php echo $f6; ?></span></li>
							<li><b>Location:</b><span><?php echo strstr($f17, '@', true); ?></span></li>	
							<li><b>Reference:</b><span><?php echo $f7; ?></span></li>		
							</ul><!--/ .list-entry-->

						<label class="compare"><input type="checkbox" />Compare</label>
						<a href="one-products.php?vehicle_id=<?php echo $f7; ?>"class="button orange">Details &raquo;</a>							
					</div><!--/ .detailed-->
					
				</article>
<?php $i++; } ?>[/code]

Apologies for the mashup of code, but this is as far as i have got.

Thanks again for any help !

I’ll help you with pagination but first, please read this page and try to update your code to use mysql_fetch_assoc()

http://www.php.net/mysql_fetch_assoc

m@tt … thank you very much for getting back to me. I have taken on board what you said and have read and followed the instructions to the best of my ability. So what i have done works, in the sense that the page displays all the lines in my database (whereas before i had controlled the amount of times to run the loop) but i assume the pagination then will control that instead. So baring in mind that i am a novice here is my amended code.

[code]<?php
$sql=“SELECT * FROM vehicles”;
$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)) { ?>

<article>
					<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>

					<div class="detailed">
						
						<h6>
						     
						        <a href="one-products.php?vehicle_id=<?php echo $row["vehicle_id"]; ?>"class="title-item"><?php echo $row["title"]; ?></a>
							
						</h6>
						
						<span class="price">£<?php echo $row["price"]; ?></span>
						
						<div class="clear"></div>
						
						<ul class="list-entry">
							<li><b>Engine:</b><span><?php echo $row["engine"] ?></span></li>
							<li><b>Mileage:</b><span><?php echo $row["mileage"]; ?></span></li>	
							<li><b>Year:</b><span><?php echo $row["year"]; ?></span></li>
							<li><b>Location:</b><span><?php echo strstr($row["seller_email"], '@', true); ?></span></li>	
							<li><b>Reference:</b><span><?php echo $row["vehicle_id"]; ?></span></li>		
							</ul><!--/ .list-entry-->

						<!-- <label class="compare"><input type="checkbox" />Compare</label> -->
						<a href="one-products.php?vehicle_id=<?php echo $row["vehicle_id"]; ?>"class="button orange">Details &raquo;</a>							
					</div><!--/ .detailed-->
					
				</article>
<?php } mysql_free_result($result); ?>[/code]

I hope i have understood correctly !

As I understand your question you have multiple results from your query, and you want to be able to display 12 results at a time. I had the same issue, but I wanted to display 30 results. Here is how I solved the riddle. If anyone has a better way, let me know.

[php]

$numpages = 30; //sets the number of entries per page

if (isset($_GET[‘continue’])) //determines if we have reached the end of the entries
{
if ($_GET[‘continue’] = “NEXT”) //if we have’t get the next result set
{
$offset = $_GET[‘offset’];
$pagenum = $_GET[‘pagenum’];
}
ELSE //if end of results have been reached reach reset variables
{
$pagenum = 1;
$offset = ($pagenum -1)*$numpages;
}

// Connect to Database

$db = db_connect();

//set up query
$query = “SELECT classmateid, fname, lname
FROM classmate
ORDER BY lname
LIMIT “.$offset.”, “.$numpages.””; //this limits the

//send query to database
$result = $db->query($query);
$num_class = $result->num_rows;

// Check for database error
if(!$result)
{
echo “oops”;
}

//check to see if any results were found
if($num_class == 0)
{
echo "oops";;
}


//convert result set to an array
$res_array = array(); 

//  Next we save each row returned from the database
//  as its own array
for ($count=0; $row = $result->fetch_assoc(); $count++)
{
$res_array[$count] = $row;
}

//if no result display error message
if (!is_array($res_array))
{
 	echo "<p>No classmates currently available</p>";
 	return;
 }

\this next part displays the results in three columns

    $tc = 1;
    $count = 0;
    echo "<table border='0' cellspacing='0' cellpadding='5' style='width:100%; margin:auto' >
	<tr>";
	foreach ($res_array as $row)
	{
	              $res_array[$count] = $row;
		echo "<td style = 'text-align:center; valign='top';>";
	                     $url = "show_classmates.php?classmateid=".$row['classmateid'];
			$name = $row['lname'].", ".$row['fname'];
		      echo "<a href= ".$url.">".$name."</a><br /> 
                                       </td>";
					  
		 if ($tc < 3)
		{
			 echo "</td>";
			 $tc = $tc+1;
		}
		 else
		{
			 echo "</td>";
			 echo "</tr>";
			 $tc = 1;
		}
				  }
?>
            </tr>
//this section displays navigation buttons at the bottom of the page			 
<tr>
     <td style='text-align:center'>
          <form method="get" action="classmates.php">
                          <input type="hidden" name="offset" value = "<?php echo $offset - $numpages ?>" />
                          <input type="hidden" name="pagenum" value="<?php echo $pagenum - 1 ?>" />
                          <input type="submit" name="continue" value="PREV" />
                    </form>
                </td>
                
                <td style='text-align:center'>
	<form method="get" action="classmates.php">
                              <input type="hidden" name="offset" value = "0" />
                              <input type="hidden" name="pagenum" value="1" />
                             <input type="submit" name="continue" value="START" />
                         </form>
                </td>
                
                <td style='text-align:center'>
          <form method="get" action="classmates.php">
                        <input type="hidden" name="offset" value = "<?php echo $offset + $numpages ?>" />
                        <input type="hidden" name="pagenum" value="<?php echo $pagenum + 1 ?>" />
                        <input type="submit" name="continue" value="NEXT" />
                    </form>
                </td>
            </tr>
       </table>

[/php]

That’s much better! :slight_smile:

Now there’s no sense reinventing the wheel so I’ll just give you a pagination class that I’ve used before.

[php]
class Pagination {
var $page_number;
var $page_var;
var $pages_results;
var $pages_links;
var $total_pages;
var $results;

function __construct() {
	$this->page_var 		= "p";
	$this->pages_results 	= 20;
	$this->pages_links 		= 99999;
	$this->page_number 		= null;
	$this->total_pages		= 1;
	$this->results			= array();
}

function pagesInfo()	{
	$this->total_pages	= $this->get_total_pages();
	$this->page_number	= ($this->page_number === null ? $this->get_page_number() : $this->page_number);
	
	$pagesArray	= array(
		"Previous_Page"	=> $this->get_prev_page(),
		"Next_Page"		=> $this->get_next_page(),
		"Current_Page"	=> $this->page_number,
		"Total_Pages"	=> $this->total_pages,
		"Page_Numbers"	=> $this->get_page_numbers(),
		"Db_Offset"		=> $this->get_db_offset(),
		"Start_Offset"	=> $this->get_start_offset(),
		"End_Offset"	=> $this->get_end_offset(),
		"Page_Results"	=> $this->pages_results,
		"Total_Results"	=> $this->results,
		"Page_Variable"	=> $this->page_var
	);
	return $pagesArray;
}

function setResults($argResults) {
	$this->results = $argResults;
}

function setPageLinks($value) {
	$this->pages_links = $value;
}

function setPageResults($value) {
	$this->pages_results = $value;
}

function setPageVar($var) {
	$this->page_var = $var;
}

function get_page_number($p = null) {
	$total_pages = $this->get_total_pages();
	if ($p !== null) {
		return ($p > $total_pages ? 1 : $p);
	} else {
		$value = (isset($_REQUEST[$this->page_var]) && !empty($_REQUEST[$this->page_var]) && is_numeric($_REQUEST[$this->page_var]) ? $_REQUEST[$this->page_var] : 1);
		return ($value > $total_pages ? 1 : $value);
	}
	return 1;
}

function get_total_pages() {
	$result = ($this->results != 0 && $this->pages_results != 0 ? ceil($this->results / $this->pages_results) : 1);
	return (isset($result) && $result == 0 ? 1 : $result);
}

function get_start_offset()	{
	return ($this->results > 0 ? ($this->pages_results * ($this->page_number - 1)) + 1 : 0);
}

function get_db_offset() {
	return ($this->pages_results * ($this->page_number - 1));
}

function get_end_offset() {
	return ($this->get_start_offset() > ($this->results - $this->pages_results) ? $this->results : ($this->get_start_offset() + ($this->pages_results - 1)));
}

function get_prev_page() {
	return ($this->page_number > 1 ? ($this->page_number - 1) : 0);
}

function get_next_page() {
	return ($this->page_number < $this->total_pages ? ($this->page_number + 1) : 0);
}

function get_start_number() {
	$half = ceil($this->pages_links / 2);
	if ($this->page_number <= $half || $this->total_pages <= $this->pages_links) {
		return 1;
	} else if ($this->page_number >= ($this->total_pages - $half)) {
		return ($this->total_pages - $this->pages_links) + 1;
	} else {
		return $this->page_number - $half;
	}
}

function get_end_number() {
	if ($this->total_pages < $this->pages_links) {
		return $this->total_pages;
	} else {
		return ($this->get_start_number() + $this->pages_links) - 1;
	}
}

function get_page_numbers() {
	$numbers = array();
	for($p = $this->get_start_number(); $p <= $this->get_end_number(); $p++) {
		$numbers[] = $p;
	}
	return $numbers;
}

}
[/php]

Just copy this to a file and include it wherever pagination is needed.

Here is how you can implement this into your code.

[php]
// Don’t forget to include your pagination class first

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

// Query the total number of results
$sql = "SELECT COUNT() FROM vehicles";
$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 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); [/php] The only thing left to do would be building the page links. You can do this by simply using the $pages['Page_Numbers'] array. e.g. [php] foreach($pages['Page_Numbers'] as $page_num) { echo '' . $page_num . ' - '; } [/php]

Thanks m@tt … let’s see if i understand correctly. So i copied your pagination class to pagination.php and then replaced my code with your amended version and added a simple <?php include 'pagination.php'; ?>

Is that correct ?

I haven’t built the page links as yet, but so far i have a 500 Server error, just by copying the code and including the pagination.php … have i missed something perhaps ?

Make sure that you have opening/closing PHP tags in the correct places.

[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`"; $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 <> or <> $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` 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)) { ?>
<article>
	<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>
	<div class="detailed">
		<h6>
			<a href="one-products.php?vehicle_id=<?php echo $row["vehicle_id"]; ?>"class="title-item"><?php echo $row["title"]; ?></a>
		</h6>
		<span class="price">£<?php echo $row["price"]; ?></span>
		<div class="clear"></div>
		<ul class="list-entry">
			<li><b>Engine:</b><span><?php echo $row["engine"] ?></span></li>
			<li><b>Mileage:</b><span><?php echo $row["mileage"]; ?></span></li>
			<li><b>Year:</b><span><?php echo $row["year"]; ?></span></li>
			<li><b>Location:</b><span><?php echo strstr($row["seller_email"], '@', true); ?></span></li>
			<li><b>Reference:</b><span><?php echo $row["vehicle_id"]; ?></span></li>
		</ul><!--/ .list-entry-->
		<!-- <label class="compare"><input type="checkbox" />Compare</label> -->
		<a href="one-products.php?vehicle_id=<?php echo $row["vehicle_id"]; ?>"class="button orange">Details &raquo;</a>
	</div><!--/ .detailed-->
</article>
<?php } mysql_free_result($result); ?>

[/php]

Haha … so somebody (namely me) forgot the closing tag in pagination.php … so i think i am making progress, i’m just going to experiment here with the page links and i’ll follow up to let you know how i get on !!

Thanks you very very much !!

So i have got it all working perfectly and it is fantastic !! Thank you again !

I don’t mean to push my luck, but i was wondering how i would go about styling the page numbers, i’m trying to see which part is current and which part is all other pages.

I would love to achieve something like this

<span class="current">1</span> <a class="page">2</a>

Never mind if it’s complicated, you have been more than helpful already !

Check the $pages array. You do have $pages[‘Current_Page’]

So you could simply do

[php]
if ($page_num == $pages[‘Current_Page’]) {
// current page
}
else {
// not current page
}
[/php]

Gotcha !! … I think it’s too late in the night for my brain to function (Ireland Time) …
I didn’t even think of looking there, and on looking i now realise there are more option for next & previous images aswell … awesome … You have been a fantastic help … Thanks again !

M@tt … i’m hoping you can take a look at this one, it’s killing me as it is a little more complicated. It’s basically a search results page where the criteria id passed by adding the string to the URL from the search to the search results … So the SELECT is a little more complicated … i think it is the COUNT that is killing me, maybe you could take a look at this and tell me where i have gone wrong.

[php]//var_dump($_REQUEST);
$resultsFoundFlag = 0;

$make;
$model = “”;
$minPrice;
$maxPrice;
if(isset($_REQUEST[‘make’])){
$make= $_REQUEST[‘make’];
$resultsFoundFlag = 1;
}

if(isset($_REQUEST[‘model’]) AND $resultsFoundFlag){
$model= $_REQUEST[‘model’];

}

if(isset($_REQUEST[‘maxPrice’]) AND $resultsFoundFlag){
$maxPrice= $_REQUEST[‘maxPrice’];

}
else{
$resultsFoundFlag = 0;
}

if(isset($_REQUEST[‘minPrice’]) AND $resultsFoundFlag){
$minPrice= $_REQUEST[‘minPrice’];
}
else{
$resultsFoundFlag = 0;
}
if($resultsFoundFlag){
if($minPrice > $maxPrice){
$resultsFoundFlag =0;
}
}

if($resultsFoundFlag){
$subQuery1 = "price>=$minPrice ";
if($maxPrice){
$subQuery1 = "price>=$minPrice AND price<=$maxPrice ";
}
$subQuery2 = “”;
if($model){
$subQuery2 = "model= ‘$model’ AND ";
}

// 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 = ‘$make’ AND “.$subQuery2.$subQuery1.”;
$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
$query = "SELECT * FROM vehicles WHERE make = ‘$make’ AND “.$subQuery2.$subQuery1.” LIMIT " . $pages[‘Db_Offset’] . “, " . $pages[‘Page_Results’]”;
}
$result=mysql_query($query);

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

while ($row = mysql_fetch_assoc($result)) { [/php]

SELECT COUNT * FROM

COUNT is a function

SELECT COUNT(*) FROM

Thanks for getting back to me M@tt, i put that in place but there is still something causing a server error.

I’m getting this :: Parse error: syntax error, unexpected T_STRING in your code on line 79 … when testing the code …

I wish i knew more about this stuff :wink:

Do you use a color coded editor? You can actually see on the forum where your code has an error because the color coding changes. Note where the comments turn from orange to red.

[php]$sql = "SELECT COUNT * FROM vehicles WHERE make = ‘$make’ AND “.$subQuery2.$subQuery1.”;[/php]

You are not ending the string properly. It should either be double quotes or no quotes

[php]$sql = “SELECT COUNT(*) FROM vehicles WHERE make = ‘$make’ AND “.$subQuery2.$subQuery1.””;[/php]

OR

[php]$sql = "SELECT COUNT(*) FROM vehicles WHERE make = ‘$make’ AND ".$subQuery2.$subQuery1;[/php]

Thanks again for your reply and all the help you have given me. With regards to editor, i had been using Notepad and built in code editor in cPanel … I went and tried a few different editors and made (some) progress but i am still having a little problem. As it stands i’m not getting any fatal errors, my code below works to show the first 12 items in my db and creates the pagination numbers. Unfortunately when i go to page 2 for example i get the following error … Could not successfully run query () from DB: Query was empty

Here is my code,

[code]<?php
//var_dump($_REQUEST);
$resultsFoundFlag = 0;

$make;
$model = “”;
$minPrice;
$maxPrice;
if(isset($_REQUEST[‘make’])){
$make= $_REQUEST[‘make’];
$resultsFoundFlag = 1;
}

if(isset($_REQUEST[‘model’]) AND $resultsFoundFlag){
$model= $_REQUEST[‘model’];

}

if(isset($_REQUEST[‘maxPrice’]) AND $resultsFoundFlag){
$maxPrice= $_REQUEST[‘maxPrice’];

}
else{
$resultsFoundFlag = 0;
}

if(isset($_REQUEST[‘minPrice’]) AND $resultsFoundFlag){
$minPrice= $_REQUEST[‘minPrice’];
}
else{
$resultsFoundFlag = 0;
}
if($resultsFoundFlag){
if($minPrice > $maxPrice){
$resultsFoundFlag =0;
}
}

if($resultsFoundFlag){
$subQuery1 = "price>=$minPrice ";
if($maxPrice){
$subQuery1 = "price>=$minPrice AND price<=$maxPrice ";
}
$subQuery2 = “”;
if($model){
$subQuery2 = "model= ‘$model’ AND ";
}
// 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 vehiclesWHERE make = ‘$make’ AND “.$subQuery2.$subQuery1.”";
$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
$query = "SELECT * FROM vehicles WHERE make = ‘$make’ AND “.$subQuery2.$subQuery1.” LIMIT " . $pages[‘Db_Offset’] . ", " . $pages[‘Page_Results’];
}

$result=mysql_query($query);

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

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]; ?>”>

					<div class="detailed">
						
						<h6>
						     
						        <a href="one-products.php?vehicle_id=<?php echo $f7; ?>"class="title-item"><?php echo $row["title"] ?></a>
							
						</h6>
						
						<span class="price">£<?php echo $row["price"] ?></span>
						
						<div class="clear"></div>
						
						<ul class="list-entry">
							<li><b>Engine:</b><span><?php echo $row["engine"] ?></span></li>
							<li><b>Mileage:</b><span><?php echo $row["mileage"] ?></span></li>	
							<li><b>Year:</b><span><?php echo $row["year"] ?></span></li>
							<li><b>Location:</b><span><?php echo strstr($row["seller_email"], '@', true); ?></span></li>	
							<li><b>Reference:</b><span><?php echo $row["vehicle_id"] ?></span></li>		
							</ul><!--/ .list-entry-->

						<!-- <label class="compare"><input type="checkbox" />Compare</label> -->
						<a href="one-products.php?vehicle_id=<?php echo $f7; ?>"class="button orange">Details &raquo;</a>							
					</div><!--/ .detailed-->
					
				</article>
<?php } mysql_free_result($result); ?>
			</section><!--/ #change-items-->				

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

At least it is not breaking pages anymore, but i would love the second page to show results. Any ideas ?

Thanks again ! Really appreciate your help.

I’m guessing that your $resultsFoundFlag is empty (zero)

Change your error line to this and see what it outputs

[php]
echo “Could not successfully run query (” . $resultsFoundFlag . ": " . $query . ") from DB: " . mysql_error();[/php]

It would appear you are correct. The error now says

Could not successfully run query (0: ) from DB: Query was empty

I’m guessing the query is cancelling out on page change … but i have no idea how to address it !

All of your variables have to be passed in the URL for each page e.g.

[php]
echo ‘’ . $page_num . ’ ';[/php]

M@tt … Thank you, thank you, thank you !!!

It now works a treat … Perfect !

I really don’t no how to thank you enough. Maybe someday i can return the favor !!

Sponsor our Newsletter | Privacy Policy | Terms of Service