Jump to content

Recommended Posts

Guys and Gals...I need help.....my pagination with search box feature works except for the fact that when I click next it just refreshes the page and it doesnt show the next set of results.....please help...

 

Code Follows:

 

if(!isset($_GET['q']))
die("Search Query not found"); 

$var = $_GET['q'];
$trimmed = trim($var); 

$limit=10;


if ($trimmed == ""){
echo "<p>Please enter a search…</p>";
exit;
}


if (!isset($var)){
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}


mysql_connect("host","dbusername","dbpassword");


mysql_select_db("inventorymanage") or die("Unable to select database");

$query = "select * from invsearch where item_name like \"%$trimmed%\" order by item_name";




$numresults= mysql_query($query);
$numrows= mysql_num_rows($numresults);


if (empty($s)) {
$s=0;
}


$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");


echo "<p align='center'>You searched for: ". $var ." </p>";


echo "<p align='center'>Results: </p>";
$count = 1 + $s ;


echo "<table border='2' cellpadding='0' align='center'>";
while ($row = mysql_fetch_array($result)) {
  echo "<tr>";
echo "<td width='500px'>";
echo	"<p style=margin:0px><font color='black' size='5px'>" . $row['item_name'] . "</font></p>";
echo   "<p style=margin:0px><font color='black' size='3px'>" . $row['system'] . "</font></p>";
echo   "<p style=margin:0px><font color='black' size='2px'>Item Number: " . $row['Item_num'] . "</font></p>";
echo "</td>";
echo "<td><font color='red' size='3px'>" . $row['price'] . "</font></p";
echo "</td>";
echo "</tr>";
}
echo "</table>";


echo "<p align='center'>";

$currPage = (($s/$limit) + 1);


echo "<br />";


if ($s>=1) {


$prevs=($s-$limit);
echo " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<Prev 10</a>";
}



$pages=intval($numrows/$limit);



if ($numrows%$limit) {

$pages++;
}


if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {


$news=$s+$limit;

echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";


$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
}
echo "<p align='center'>Showing results $b to $a of $numrows</p>";

echo "</P>";

 

 

This is the codde im using....any ideas or thoughts??

Link to comment
https://forums.phpfreaks.com/topic/246250-pagination-code-help/
Share on other sites

Thanks for the reply.....

 

If i understood you correct... all i need to do is change this : <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>

 

to this: <a href=\"page.php?s=$news&q=$var\">Next 10 >></a>

 

I did that and still same result.....

 

GRRRRRRRR..........my brain is hurting.... sorry im just frustrated.... working on a project and this the last things i need to get working.....php is definitely not my strong area.....

Your code is apparently (it's actually difficult to follow what your code is doing) trying to pass the record number through the links, not a page number. That would be called record-ination not pagination.

 

I recommend that you read this tutorial - http://www.phpfreaks.com/tutorial/basic-pagination

Okay so ive scrapped the previous pagination or recordination code.......cant get it to work......now ive implemented the one in the php freaks tutorial....now my question is how do I add a search feature to this?

 

code follows:

 

<?php

// database connection info


$conn = mysql_connect('dbhost, dbusername, dbpassword') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('inventorymanage',$conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM invsearch";
$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 = 10;

// 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 Item_name, system, item_num, price FROM invsearch ORDER BY item_name  LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
echo "<table border='2' cellpadding='10' align='center'>";
while ($list = mysql_fetch_assoc($result)) {  
  // echo data   
  echo "<tr>";
echo "<td width='500px'>";
echo	"<p style=margin:0px><font color='black' size='5px'>" . $list['Item_name'] . "</font></p>";
echo   "<p style=margin:0px><font color='black' size='3px'>" . $list['system'] . "</font></p>";
echo   "<p style=margin:0px><font color='black' size='2px'>Item Number: " . $list['item_num'] . "</font></p>";
echo "</td>";
echo "<td><font color='red' size='3px'>" . $list['price'] . "</font></p";
echo "</td>";
echo "</tr>";

}
echo "</table>";

echo "<p align='center'>";
  
  // end while/******  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'>First<<</a> ";   
  
  // get previous page num   
  $prevpage = $currentpage - 1;   
  
  // show < link to go back to 1 page   
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Prev<</a> ";} 
  // end if
  
  // loop to show links to range of pages around current page
  
  for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {   
  
  // if it's a valid page number...   
  
  if (($x > 0) && ($x <= $totalpages)) {      
  
  // if we're on current page...      
  
  if ($var == $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'>$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 = $var + $currentpage + 1;    
  
  // echo forward link for next page    
  
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next></a> ";   
  
  // echo forward link for lastpage   
  
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last>></a> ";
  
  } 
  echo "</P>";
  // end if/****** end build pagination links ******/
  
  ?>

 

To be specific I want to have a search box point to this and search the database and paginate the results.....

 

 

Try this:

<?php

// database connection info


$conn = mysql_connect('dbhost, dbusername, dbpassword') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('inventorymanage',$conn) or trigger_error("SQL", E_USER_ERROR);

if(isset($_POST['search'])) {
$item = mysql_real_escape_string($_POST['item']);
}
elseif(isset($_GET['item'])) {
$item = mysql_real_escape_string($_GET['item');
}
else {
$item = 'a';
}

// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM invsearch WHERE item_name LIKE '%$item%'";
$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 = 10;

// 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 Item_name, system, item_num, price FROM invsearch WHERE item_name LIKE '%$item%' ORDER BY item_name  LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
echo "<div>\n<form action=\"\" method=\"post\">\n<label for=\"item\">Search</label>\n
                <input type=\"text\" name=\"item\" id=\"item\" /> <input type=\"submit name=\"submit\" value=\"Search\" />\n
			</form>\n<table border='2' cellpadding='10' align='center'>";
while ($list = mysql_fetch_assoc($result)) {  
  // echo data   
  echo "<tr>";
echo "<td width='500px'>";
echo	"<p style=margin:0px><font color='black' size='5px'>" . $list['Item_name'] . "</font></p>";
echo   "<p style=margin:0px><font color='black' size='3px'>" . $list['system'] . "</font></p>";
echo   "<p style=margin:0px><font color='black' size='2px'>Item Number: " . $list['item_num'] . "</font></p>";
echo "</td>";
echo "<td><font color='red' size='3px'>" . $list['price'] . "</font></p";
echo "</td>";
echo "</tr>";

}
echo "</table>";

echo "<p align='center'>";
  
  // end while/******  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&item=$item'>First<<</a> ";   
  
  // get previous page num   
  $prevpage = $currentpage - 1;   
  
  // show < link to go back to 1 page   
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&item=$item'>Prev<</a> ";} 
  // end if
  
  // loop to show links to range of pages around current page
  
  for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {   
  
  // if it's a valid page number...   
  
  if (($x > 0) && ($x <= $totalpages)) {      
  
  // if we're on current page...      
  
  if ($var == $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&item=$item'>$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 = $var + $currentpage + 1;    
  
  // echo forward link for next page    
  
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&item=$item'>Next></a> ";   
  
  // echo forward link for lastpage   
  
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&item=$item'>Last>></a> ";
  
  } 
  echo "</P>";
  // end if/****** end build pagination links ******/
  
  ?>

Your my hero for today...

 

I had to change this:

 

 if(isset($_POST['search'])) {	
                              $item = mysql_real_escape_string($_POST['item']);} 

 

to this:

 

 if(isset($_GET['search'])) {	
                            $item = mysql_real_escape_string($_GET['item']);} 

 

but aside from that it works perfect... Thank You very Much

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.