Jump to content

Help With Page Navigation


armjoker

Recommended Posts

Hi guys, im noob to php and started learning it now.

 

Im building a test webpage and run on a problem which i cant find a way out.

 

I want to add page navigation. (after i receive the query result from MySQL, I want to sort it 10 results per page. and if the number of rows is > the 10 to crate a second page and so on.... ) expl: prev page. 1. 2. 3....25. next page. last page. here is my code

<?php session_start();
include('cms/db.php');
error_reporting(E_ALL ^ E_NOTICE);
$make = $_POST['make'];
$model = $_POST['model'];
$year = $_POST['year'];
$min_price = $_POST['minprice'];
$max_price = $_POST['maxprice'];


$search = $_GET['search'];


echo"
<table width='100%' align='left'>
<tr>
<td width='' collspan='3'>Make:</td>
<td collspan='3'>
<form name='form2' method='post' action='index.php?page_id=2&page_type=search&search=search'>
<select name='make'>
<option value=''>Select</option>
<option value='Audi'>Audi</option>
<option value='BMW'>BMW</option>
<option value='Ford'>Ford</option>
<option value='Honda'>Honda</option>
<option value='Mitsubishi'>Mitsubishi</option>
<option value='Opel'>Opel</option>
<option value='Porsche'>Porsche</option>
</select>(Required)
</td>
</tr>

<tr>
<td width='13%' collspan='4'>Model:</td>
<td collspan='2'><select name='model'>
<option value=''>Select</option>
<option value='Audi'>Audi</option>
<option value='BMW'>BMW</option>
<option value='Ford'>Ford</option>
<option value='Honda'>Honda</option>
<option value='Mitsubishi'>Mitsubishi</option>
<option value='Opel'>Opel</option>
<option value='Porsche'>Porsche</option>
</select>
</td>
</tr>
<tr>
<td colspan='0'>Year:</td>
<td><input type='text' name='year' size='5' style='text-align: center;' maxlength='4'></td>
</tr>
<tr>
<td width='70'>Price min: </td>
<td><input type='text' name='minprice' size='5'> max:  <input type='text' name='maxprice' size='5'></td>
</tr>
<tr>
<td>Optional:</td>
<td>
<input type='checkbox' value='Sport_seats' name='sport_seats'>Sport Seats
<input type='checkbox' value='AC' name='AC'>AC
<input type='checkbox' value='Cruise_control' name='Cruise_control'>Cruise control
<input type='checkbox' value='central_locking' name='central_locking'>Central locking
</td>
</tr>
<tr>
<td colspan=''><td colspan='2' align='center'> <input type='submit' name='Search' value='Search'></td>
</tr>
</table>
</form>
";
if($make){$make="make='$make'";}else $search='';
if($model){$model="and model='$model'";}
if($year){$year="and year='$year'";}
if($search){
$qry=mysql_query("SELECT * FROM cars WHERE $make $model $year", $dbcnx);
if(!$qry){
 echo "Please select Make";
 }
$num = @mysql_num_rows($qry);
echo "<table border='1' width='100%' align='left'>
<tr>
<th width='80'>Make</th>
<th width='80'>Model</th>
<th width='80'>Year</th>
<th width='80'>Price</th>
<th>Description</th>
</tr>";

if(!$num){
 echo "No results!";
}
while($row = @mysql_fetch_array($qry)){


$make = $row['make'];
$model = $row['model'];
$year = $row['year'];
$price = $row['price'];
$description = $row['info'];
echo "<tr>
<td align='center'>".$make."</td>
<td align='center'>".$model."</td>
<td align='center'>".$year."</td>
<td align='center'>".$price."</td>
<td align='center'>".$description."</td>";
}
echo "</table>";

}
?>

 

I have found a ready mage php code for page navigation, and was trying to put them together but its somehow doesnt work. I have to change my SQL query?

 

here is the code I got from the web for the pagenav.

<?php

class pageNavigation {

var $noOfPages;
var $currPage;
var $previousStartItem;
var $nextStartItem;

var $pages;

// constructor
function pageNavigation($noOfItems, $itemsPerPage, $startItem) {

if ($itemsPerPage <= 1) $itemsPerPage = 2;
elseif ($itemsPerPage > $noOfItems && $noOfItems != 0) $itemsPerPage = $noOfItems;

if ($startItem < 0) $startItem = 0;

$this->noOfPages = ceil($noOfItems / $itemsPerPage);

$this->currPage = ceil( ($startItem+1) / $itemsPerPage);

if($this->currPage > $this->noOfPages) { $this->currPage = $this->noOfPages; }

$this->previousStartItem = ($this->currPage-2)*$itemsPerPage;
if($this->previousStartItem < 0) $this->previousStartItem = -1;

$this->nextStartItem = $this->currPage*$itemsPerPage;
if($this->nextStartItem > $noOfItems-1) $this->nextStartItem = -1;

// create pages
$tempArray = array();
// first part
for($i = 1; $i <= 3; $i++) {
if($i >= 1 && $i <= $this->noOfPages) { $tempArray[] = $i; }
}
// middle part
for($i = $this->currPage-6; $i <= $this->currPage+6; $i++) {
if($i >= 1 && $i <= $this->noOfPages) { $tempArray[] = $i; }
}
// last part
for($i = $this->noOfPages-2; $i <= $this->noOfPages; $i++) {
if($i >= 1 && $i <= $this->noOfPages) { $tempArray[] = $i; }
}
$tempArray = array_unique($tempArray); //cut off duplicate entries
sort($tempArray);

// and create the array containing the pages
$this->pages = array();
$tempLast = -1;
foreach($tempArray as $key => $value) {
if($key != 0 && $value > ($tempLast + 1)) { // put an empty interval if there's a jump
$this->pages[] = array("pageno" => -1, "startitem" => -1);
}
$this->pages[] = array("pageno" => $value, "startitem" => (($value-1)*$itemsPerPage));
$tempLast = $value;
}


}

}

?>




<?php

$totNoOfItems = 500;
$itemsPerPage = 20;
$startItem = $_GET["startitem"];

$myPageNavigation = new pageNavigation($totNoOfItems, $itemsPerPage, $startItem);

echo "Listing ".$totNoOfItems." items<br /><br />";

if ($myPageNavigation->noOfPages > 1) {

if ($myPageNavigation->previousStartItem != -1) {
echo " <a href=\"pagenav.php?startitem=".$myPageNavigation->previousStartItem."\">Prev</a> ";
}

foreach ( $myPageNavigation->pages as $page ) {
if ($page["pageno"] == -1) {
echo " <b>.</b> ";
}
elseif ($page["pageno"] == $myPageNavigation->currPage) {
echo " <b>".$page["pageno"]."</b> ";
}
else {
echo " <a href=\"pagenav.php?startitem=".$page["startitem"]."\">".$page["pageno"]."</a> ";
}
}

if ($myPageNavigation->nextStartItem != -1) {
echo " <a href=\"pagenav.php?startitem=".$myPageNavigation->nextStartItem."\">Next</a> ";
}
}

?>

 

Any help is very welcome.

Thanks a lot in advance.

Link to comment
Share on other sites

its not navigation..it is pagination that you are looking for..

try with this tutorial..

 

http://datatables.ne...pagination.html

 

Thank you for help.

Didnt get wat was exactly on the tutorial you gave, but found a good info on the forum about pagination (since now i learned how its called:) )

here is the link if someone else like me will need it in future...

 

http://www.phpfreaks.com/tutorial/basic-pagination

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.