Jump to content

[SOLVED] RePOSTing Search info for Paginated Results


kingnutter

Recommended Posts

The first page of search results works perfectly in the code below, however further pages are blank.

 

I think this is because the information is not being rePOSTed for each page but:

 

a) I'm not quite sure how to do this. Is it just a hidden form with existing string values as defaults?

 

and

 

b) Is it better achieved with SESSIONs or some other solution?

 

Thanks in advance,

KN

 

<form name="search" method="post" action="search_results.php">
Search for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="track_artist">Track Artist</option>
<Option VALUE="track_title">Track Title</option>
<Option VALUE="all">All</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?php

// includes
include('conf.php');
include('functions.php');

$field = $_POST['field'] ;
$find = $_POST['find'] ;
$searching = $_POST['searching'] ;


//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
// open database connection
$connection = mysql_connect($host, $user, $pass)
or die ('unable to connect!');

//select database
mysql_select_db($db) or die ('unable to select database!');


$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);


// Query database to find out how many rows in total result before pagination. N.B Could experiment with COUNT here.

if ($field=='all')

{
$query = "SELECT t.moj_id, t.track_artist, t.track_title, m.moj_title

FROM tracks t
JOIN mojocd m ON t.moj_id = m.moj_id

WHERE upper(t.track_artist) LIKE '%$find%'
   OR upper(t.track_title) LIKE '%$find%'";
   
$result = mysql_query($query, $connection);
}
else
//Or we search for our single search term, in the field the user specified
{
$query = "SELECT track_artist, track_title FROM tracks WHERE upper($field) LIKE'%$find%'";
$result = mysql_query($query);
}

$result = mysql_query($query, $connection);

$numrows = mysql_num_rows($result);

// 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;


if ($field=='all')

{
$query = "SELECT t.moj_id, t.track_artist, t.track_title, m.moj_title

FROM tracks t
JOIN mojocd m ON t.moj_id = m.moj_id

WHERE upper(t.track_artist) LIKE '%$find%'
   OR upper(t.track_title) LIKE '%$find%' LIMIT $offset, $rowsperpage";
}
else
//Or we search for our single search term, in the field the user specified
{
$query = "SELECT track_artist, track_title FROM tracks WHERE upper($field) LIKE'%$find%' LIMIT $offset, $rowsperpage";
}

$result = mysql_query($query);

// while there are rows to be fetched...

?><table><?php
while($list = mysql_fetch_assoc( $result ))
{

$counter++;
	$background_color = ( $counter % 2 == 0 ) ? ('white') : ('#E0E0E0');


echo '<tr><td style="background-color:'.$background_color.'">' ?>
<b><?php echo $list['track_title']; ?></b> by 
<b><?php echo $list['track_artist']; ?></b> on
<b><?php echo $list['moj_title']; ?></b></td></tr>
<br>
<?php
}
?>
</table>
<?php
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}


/******  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'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</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 ($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'>$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'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>

Link to comment
Share on other sites

a) yes, you can use hidden forms if there is a form being submitted to get to the next page... or information in the URL if you have a link taking you to the next page...

 

<input type='hidden' name='something' value='".$something."'>

or

<a href='mypage.php?something=".$something.">Another Page</a>

 

for example

 

b) yes, sessions are good if you want to access a variable across many pages, more secure too

 

--------------

 

edit: the only information you are wanting on the next page is for pagination? just use $_GET information from the URL rather than POST if that is the case....

 

<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a>

 

 

can you give an example of what the URL looks like for the page 1 that is working? because if that has something like mypage.php?topic=5 for example, you'll need to add that as well

Link to comment
Share on other sites

Use a db table to store the search result along with the user's session id (to retrieve these rows). There are many tutorials that teach you how to paginate db table data, use these. Don't forget to clean this table up use an additional created_at column to delete them when they expire, also delete them as soon as the user initiates another search.

Link to comment
Share on other sites

okay, all you should have to do is basically add the values you want in the next page like this... for example if you wanted to pass $searching you could use

 

 

<a href='{$_SERVER['PHP_SELF']}?searching=$searching&currentpage=$x'>$x</a>

 

and then get it on the next page with $_REQUEST['searching'] instead of $_POST... $_REQUEST will work with both GET and POST methods

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.