Jump to content

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


Go to solution Solved by Ch0cu3r,

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;

?>
  • Solution

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

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.