Jump to content

Combine an HTML form with a display of search results over several pages


jamesmargolis

Recommended Posts

  In the code below, I am trying to produce a google-like page where the user can type keywords
and pages of search results are displayed.
  The main difficulty I am encountering is that when the browser changes pages, it forgets everything about current data.    Perhaps I should include the display of pages inside the HTML form ? How can I fix this code ?
 

 Here is the contents of my searchpage.php file :

<?php



$keywords='';
$display_search_results='';

if(isset($_POST['search_submitted'])) {
    $keywords=$_POST['keywords'];
    $search_results=array();
    $totalNbrOfItems=20;
    for($k=1;$k<=$totalNbrOfItems;$k++) {
      $search_results[$k]='Your keywords '$keyowrds.' have been found in item number '.$k;
    }  

    $nbrOfItemsParPage = 5;
    $nbrOfPages = ceil($totalNbrOfItems / $nbrOfItemsParPage);
    // Compute current page
    $current_page = (isset($_GET['page']))?intval($_GET['page']):1;
    $display_pages=($nbrOfPages<2)?'<p>Page : ':'<p>Pages : ';
    for ($i = 1 ; $i <= $nbrOfPages ; $i++)
    {
        if ($i == $current_page) //No link to the current page
        {
            $display_pages=$display_pages.$i.' ';
        }
        else
        {
            $display_pages=$display_pages.'<a href="searchpage.php?'.
            'page='.$i.'">'.
            $i . '</a> ';
        }
    }
    $display_pages=$display_pages.'</p>';
    $display_items='';
    $start=$nbrOfItemsPerPage*($current_page-1);
    for($k=1;$k<=$nbrOfItemsParPage;$k++) {
      $display_items=$display_items.($search_results[$start+$k]).'<br>';
    }
    $display_search_results=$display_pages.$display_items;
}


echo '<form method="post" action="searchpage.php">'.
' Type your keywords here : <br><br>'.
'<textarea cols="65" rows="1" '.
'id="keywords_id" name="keywords">'.$keywords.'</textarea>'.
'<input type="submit" name="search_submitted" id="search_submitted_id" value="Search" /> '.
'</fieldset>'.
'</form>';

echo $display_search_results;

?>

When you click the pages links the post data which contains the keywords will not be remembered.

 

You could use session for remembering the keywords here are the changes required for this to work

 

Change these lines

<?php

$keywords='';
$display_search_results='';

if(isset($_POST['search_submitted'])) {
    $keywords=$_POST['keywords'];

To the following

<?php
// start session
session_start();

// add submitted keywords to the keywords session variable
if(isset($_POST['search_submitted'])) {
    $_SESSION['keywords'] = $_POST['keywords'];
}

// retrieve the keywords from the session
$keywords = isset($_SESSION['keywords']) ? $_SESSION['keywords'] : '';

// only display the page links if $keywords is not empty
if(!empty($keywords))
{

No matter what page link is clicked the entered keywords will be remembered. When you submit the form with new keywords the session will be updated with the new values.

Thank you ch0cu3r, your solution works for me. I noticed some posts in thid forum are marked with a green "ANSWERED" (indicating that they have been fully answered I presume :-) ) and I guess this is what I should mark this post now, but I fail to see

an "answered" button anywhere

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.