Jump to content

[SOLVED] Another Pagination Problem


DarkPrince2005

Recommended Posts

The pagination is loading  the first page, but as soon as the links are clicked to navigate it doesn't show anything.

 

$max = 4; //amount of articles per page. change to what to want
																		$p = $_GET['p'];
																		if(empty($p))
																		{
																		$p = 1;
																		}
																		$limits = ($p - 1) * $max; 
																		//view the news article!
																		if(isset($_GET['act']) && $_GET['act'] == "view")
																		{
																		$id = $_POST['selection'];
																		$sql = mysql_query("SELECT * 
FROM tbl_product where product_manufacturer like '$id' and category_id like 'Notebooks'");
																		while($row = mysql_fetch_array($sql))
																		{
																		echo "<div><p>$row[product_name]</p></div>";
																		}

																		}else{
																		$id = $_POST['selection'];

																		//view all the news articles in rows
																		$sql = mysql_query("SELECT * FROM tbl_product where product_manufacturer like '$id' and category_id like 'Notebooks' LIMIT ".$limits.",$max") or die(mysql_error());
																		//the total rows in the table
																		$totalres = mysql_result(mysql_query("SELECT COUNT(product_id) AS tot FROM tbl_product where product_manufacturer like '$id' and category_id like 'Notebooks'"),0);	
																		//the total number of pages (calculated result), math stuff...
																		$totalpages = ceil($totalres / $max); 
																		//the table
																		echo "<table>";
																		while($row = mysql_fetch_array($sql))
																		{
																		echo "<tr><td  valign='center' align='center' width='300' height='40'><form method='post'><input type='hidden' name='product_id' value='$row[product_id]'><img src='$row[product_image]' border='0' alt='$row[product_name]' width='30%'></td>
			<td> </td>
			<td valign='top'><input type='hidden' name='product_name' value='$row[product_name]'>$row[product_name]<br><small><input type='hidden' name='product_price' value='$row[product_price]'>R $row[product_price]</small></td>
			<td> </td>
			<td valign='center'><input type='image' src='images/details.gif' value='View Details' alt='View Details' onclick=\"this.form.action='product_details.php';\"><br>
								<input type='image' src='images/placeorder.gif' value='Order' alt='Order' onclick=\"this.form.action='2.php';\"><br>
								<input type='hidden' name='qty' value='1'>
								<input type='image' src='images/addtocart.gif' value='Add To Cart' alt='Add To Cart' onclick=\"this.form.action='modcart.php?action=add';\"></form></td>
		</tr>";
																		}
																		//close up the table
																		echo "</table></td></tr>
																											<tr>
																												<td colspan='5'> </td>
																											</tr>
																											<tr>
																												<td colspan='5' align='center' valign='bottom'>";
																		for($i = 1; $i <= $totalpages; $i++){ 
																		//this is the pagination link
																		echo "  <a href='pagination.php?p=$i'>$i</a>  |";
																		}
																		}

Link to comment
Share on other sites

code repost

 

			$max = 4; //amount of articles per page. change to what to want
		$p = $_GET['p'];
		if(empty($p))
		{
		$p = 1;
		}
		$limits = ($p - 1) * $max; 
		//view the news article!
		if(isset($_GET['act']) && $_GET['act'] == "view")
		{
		$id = $_POST['selection'];
		$sql = mysql_query("SELECT * FROM tbl_product where product_manufacturer like '$id' and category_id like 'Notebooks'");
		while($row = mysql_fetch_array($sql))
		{
		echo "<div><p>$row[product_name]</p></div>";
		}

		}else{
		$id = $_POST['selection'];

		//view all the news articles in rows
		$sql = mysql_query("SELECT * FROM tbl_product where product_manufacturer like '$id' and category_id like 'Notebooks' LIMIT ".$limits.",$max") or die(mysql_error());
		//the total rows in the table
		$totalres = mysql_result(mysql_query("SELECT COUNT(product_id) AS tot FROM tbl_product where product_manufacturer like '$id' and category_id like 'Notebooks'"),0);	
		//the total number of pages (calculated result), math stuff...
		$totalpages = ceil($totalres / $max); 
		//the table
		echo "<table>";
		while($row = mysql_fetch_array($sql))
		{
		echo "<tr><td  valign='center' align='center' width='300' height='40'><form method='post'><input type='hidden' name='product_id' value='$row[product_id]'><img src='$row[product_image]' border='0' alt='$row[product_name]' width='30%'></td>
		<td> </td>
		<td valign='top'><input type='hidden' name='product_name' value='$row[product_name]'>$row[product_name]<br><small><input type='hidden' name='product_price' value='$row[product_price]'>R $row[product_price]</small></td>
		<td> </td>
		<td valign='center'><input type='image' src='images/details.gif' value='View Details' alt='View Details' onclick=\"this.form.action='product_details.php';\"><br>
		<input type='image' src='images/placeorder.gif' value='Order' alt='Order' onclick=\"this.form.action='2.php';\"><br>
		<input type='hidden' name='qty' value='1'>
		<input type='image' src='images/addtocart.gif' value='Add To Cart' alt='Add To Cart' onclick=\"this.form.action='modcart.php?action=add';\"></form></td>
		</tr>";
		}
			//close up the table
			echo "</table></td></tr>
				<tr>
				<td colspan='5'> </td>
				</tr>
				<tr>
				<td colspan='5' align='center' valign='bottom'>";
		for($i = 1; $i <= $totalpages; $i++){ 
		//this is the pagination link
		echo "  <a href='pagination.php?p=$i'>$i</a>  |";
		}
		}

Link to comment
Share on other sites

I found another script that also works, but as soon as I add a where clause to the queries it returns an error saying that 'selection' is an undefined index. Does anybody have any idea what i'm doing wrong.

 

<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate'); 
header('Cache-Control: post-check=0, pre-check=0', FALSE); 
header('Pragma: no-cache');

ini_set('display_errors','On');
error_reporting(E_ALL);
ini_set('session.cache_limiter','');
session_start();
$id=session_id();

mysql_connect("localhost","root","");
mysql_select_db("pc_lan_it");





// If current page number, use it 
// if not, set one! 

if(!isset($_GET['page'])){ 
    $page = 1; 
} else { 
    $page = $_GET['page']; 
} 

// Define the number of results per page 
$max_results =3; 

// Figure out the limit for the query based 
// on the current page number. 
$from = (($page * $max_results) - $max_results);  

// Perform MySQL query on only the current page number's results 

$sql = mysql_query("SELECT * FROM tbl_product where product_manufacturer like '$_POST[selection]' LIMIT $from, $max_results"); 



// Figure out the total number of results in DB: 
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tbl_product where product_manufacturer like '$_POST[selection]'"),0); 

// Figure out the total number of pages. Always round up using ceil() 
$total_pages = ceil($total_results / $max_results); 

// Build Page Number Hyperlinks 

// Build Previous Link 
if($page > 1){ 
    $prev = ($page - 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> "; 
} 

for($i = 1; $i <= $total_pages; $i++){ 
    if(($page) == $i){ 
        echo "$i "; 
        } else { 
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
    } 
} 

// Build Next Link 
if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"> Next>></a>"; 
}

echo "<table>";
while($row = mysql_fetch_array($sql)){ 
echo "<tr><td  valign='center' align='center' width='300' height='40'><form method='post'><input type='hidden' name='product_id' value='$row[product_id]'><img src='$row[product_image]' border='0' alt='$row[product_name]' width='30%'></td>
			<td> </td>
			<td valign='top'><input type='hidden' name='product_name' value='$row[product_name]'>$row[product_name]<br><small><input type='hidden' name='product_price' value='$row[product_price]'>R $row[product_price]</small></td>
			<td> </td>
			<td valign='center'><input type='image' src='images/details.gif' value='View Details' alt='View Details' onclick=\"this.form.action='product_details.php';\"><br>
								<input type='image' src='images/placeorder.gif' value='Order' alt='Order' onclick=\"this.form.action='2.php';\"><br>
								<input type='hidden' name='qty' value='1'>
								<input type='image' src='images/addtocart.gif' value='Add To Cart' alt='Add To Cart' onclick=\"this.form.action='modcart.php?action=add';\"></form></td>
		</tr>";
		}
		echo "</table>";
	?>

Link to comment
Share on other sites

sorry, i should have looked closer.  $_POST values won't propagate from page to page unless you do something to get them to.  that usually requires putting them into the URL as a $_GET parameter.  assuming you will always have a selection, you could adjust the links to look like this:

 

    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&selection={$_GET['selection']}\"><<Previous</a> "; 

 

this requires that you use $_GET['selection'] rather than $_POST['selection'] in the query, and that you assign it as soon as you've arrived from the form:

 

// set the selection in the GET parameters if it isn't already there
$_GET['selection'] = (isset($_POST['selection'])) ? $_POST['selection'] : $_GET['selection'];

 

note that this is a crude fix; it doesn't account for when the page is accessed neither from the form nor the results (in which case neither $_POST['selection'] or $_GET['selection'] are set).

Link to comment
Share on other sites

Thanx got it fixed

 

<?php
// database connection info
$conn = mysql_connect('localhost','root','') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('pc_lan_it',$conn) or trigger_error("SQL", E_USER_ERROR);
$_GET['selection'] = (isset($_POST['selection'])) ? $_POST['selection'] : $_GET['selection'];
// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM tbl_product where product_manufacturer like '$_GET[selection]'";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];


// number of rows to show per page
$rowsperpage = 3;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT * FROM tbl_product where product_manufacturer like '$_GET[selection]' LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
echo "<table>";
// while there are rows to be fetched...
while ($row = mysql_fetch_assoc($result)) {
   // echo data
   echo "<tr><td  valign='center' align='center' width='300' height='40'><form method='post'><input type='hidden' name='product_id' value='$row[product_id]'><img src='$row[product_image]' border='0' alt='$row[product_name]' width='30%'></td>
			<td> </td>
			<td valign='top'><input type='hidden' name='product_name' value='$row[product_name]'>$row[product_name]<br><small><input type='hidden' name='product_price' value='$row[product_price]'>R $row[product_price]</small></td>
			<td> </td>
			<td valign='center'><input type='image' src='images/details.gif' value='View Details' alt='View Details' onclick=\"this.form.action='product_details.php';\"><br>
								<input type='image' src='images/placeorder.gif' value='Order' alt='Order' onclick=\"this.form.action='2.php';\"><br>
								<input type='hidden' name='qty' value='1'>
								<input type='image' src='images/addtocart.gif' value='Add To Cart' alt='Add To Cart' onclick=\"this.form.action='modcart.php?action=add';\"></form></td>
		</tr>";
} // end while
echo "</table>";
/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&selection={$_GET['selection']}'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&selection={$_GET['selection']}'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&selection={$_GET['selection']}'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links	
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&selection={$_GET['selection']}'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&selection={$_GET['selection']}'>>></a> ";
} // end if
/****** end build pagination links ******/
?>

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.