Jump to content

[SOLVED] Simple searching with pagination


mmarif4u

Recommended Posts

Hi guys.

I have a little problem in pagination of searching script.

The following are the form code:

<form method='get' action=''>
<input type='text' name='search'>
<input type='submit' name='submit' value='Search'>
</form>

The following are the php code for searching:

<?php
if($_GET['submit']){
$rowsPerPage= 10;
$pageNum = 1; 
if(isset($_GET['page'])) { $pageNum = $_GET['page']; }
$offset = ($pageNum - 1) * $rowsPerPage;	  
  echo "Results";
  echo '<br>';
$sql2="select * from courses_add where courses_add like '%$search%' LIMIT $offset, $rowsPerPage ";
$select2=mysql_query($sql2);
$numrows2=mysql_num_rows($select2);	
echo "Your search keyword return ".$numrows2." results";
echo '<br>';echo '<br>';
$query_count = "SELECT COUNT(*) AS page_count_rows FROM courses_add where courses_add like '%$search%'";
$result_count = mysql_query($query_count) or die(mysql_error());
$row_count = mysql_fetch_array($result_count, MYSQL_ASSOC);
$page_count_rows = $row_count['page_count_rows'];

$maxPage = ceil($page_count_rows/$rowsPerPage);
if($maxPage>200){
$maxPage = 200;
}
$self = $_SERVER['PHP_SELF']; $nav_page = ''; 		
while($row2=mysql_fetch_object($select2)){
$term1=$row2->courses_add;
$c_id=$row2->c_id; 	
echo $term1;
echo '<br>';
}
}
?>
<br>
            <?php
            if ($page_count_rows>10){
            ?>
            <table align="center" border="0" cellpadding="0" cellspacing="0" width="95%">
           <tr>
            <td><hr noshade color="#6699FF" align="left" width="100%" size="1"> </td>
          </tr>
          <tr>
            <td align="center">
            <?php             
              include('paging.php');
            ?>
           </td>
          </tr>
          </table><br>
           <?php } ?>

 

Now the problem is if click the next link suppose it have to go to 2nd page, it works but i did not see any

data there.

I work out on this last night but no luck.

Can any one give some helpful hints.

Thanks in advance.

Link to comment
Share on other sites

Yes sure:

 

<?php
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

$first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
$prev  = '<font size="2" face="verdana"> [Prev] </font>';       // we're on page one, don't enable 'previous' link
$first = '<font size="2" face="verdana"> [First Page] </font>'; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";

$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
$next = '<font size="2" face="verdana"> [Next] </font>';      // we're on the last page, don't enable 'next' link
$last = '<font size="2" face="verdana"> [Last Page] </font>'; // nor 'last page' link
}

// print the page navigation link
//echo $first . $prev . $nav . $next . $last;
echo $first . $prev . "<font size='2' face='verdana'> Showing page </font><strong><font color='blue'>$pageNum</font></strong><font size='2' face='verdana'> of </font><strong><font color='blue'>$maxPage</font></strong><font size='2' face='verdana'> pages </font></strong></font>" . $next . $last;
?>

Link to comment
Share on other sites

on 2nd page you don't have $_GET['submit'] change paging.php to

 
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page&submit=Search&search=$search\">[Next]</a> ";

$last = " <a href=\"$self?page=$maxPage&submit=Search&search=$search\">[Last Page]</a> ";

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.