Jump to content

Remembering Search Queries


mick_otoole

Recommended Posts

Hey all, this is my first post here so be gentle!

I'm working with a database that has a list of companies in it. When the user authenticates he/she is given a list of all companies in the database. There is a search box whereby a user can search for specific companies. The problem that I'm having is that if there are more than 20 matches for a search string a <Next> button appears on the bottom of the page. When the user clicks on the next button the page that is displayed is not related to the original search query. I was wondering if anyone can help me out on how to display only relevant results on the <Next> page.

Here's my code:

[code]/* $Id: PageName.php,v 1.46 2006/08/03 12:40:41 Mick Exp $
<?php
session_start() ;

require_once 'DB.php';
require_once 'functions.inc';
require_once 'HTMLGenerator.php';
require_once 'Client.inc';
require_once 'Order.inc';
require_once 'Constants.inc';
require_once $path_required_scripts.'Bizunit.inc';


$_SESSION['menu_to_display'] = 'clients';

pull_from_get('deleteclient','clientid','start','created');
pull_from_post('filter','form_clientname','form_filterbizunit');
pull_from_session('os_session_user_id');
if (!isset($os_session_user_id) || $os_session_user_id == NULL || $os_session_user_id == -1)
        header('Location: index.php');


foreach(array("form_clientname","filter","form_filterbizunit") as $variable)
        if (!isset($$variable))
                $$variable = "";

$params = array();
$strFeedback = "";

if (!isset($start)) $start = 0;

$display = $listdisplay;
$prev_value = $start - $display;
$next_value = $start + $display;
$recordCount = $display+1;
$showPrevNext = 'false';


//if "company" logged in, display all clients, else only clients for this reseller
if ($os_session_user_id == 1000)
        $reseller = "";
else
        $reseller = " and u.reseller_id = ".$os_session_user_id;

$strSelect = 'Select u.* from user u, client_information c where c.user_id = u.user_id and c.client_account_activated = 1 '.$r
eseller .' limit '. $start . ',' . $display;

if (isset($filter) && $filter == 'Search')
{

                //$strSelect = "select * from client_information where client_company_name like ? or client_last_name like ?
or client_first_name like ?";

                $strTmp = "'%" . $form_clientname . "%'";
                $bizunitsql="";
                if((isset($form_filterbizunit)) && (!($form_filterbizunit==-1))) { $bizunitsql=" and c.bizunit=$form_filterbiz
unit";  };

                $strSelect = "select c.* from client_information c, user u where c.user_id=u.user_id and c.client_account_act
ivated = 1 and (u.username like ".$strTmp . " or c.client_company_name like " . $strTmp . " or c.client_last_name like  " . $s
trTmp . "  or c.client_first_name like  " . $strTmp . ")".$reseller . $bizunitsql . " limit ". $start . "," . $display;

                $res1 =& getRecordSetMessengerDB("select count(*) from client_information c, user u where c.user_id = u.user_i
d and (c.client_company_name like " . $strTmp . " or c.client_last_name like  " . $strTmp . "  or c.client_first_name like  "
. $strTmp.") ".$reseller, $params);

                $row =& $res1->fetchRow();

                $recordCount = $row[0];

                //echo $strSelect;

                if ($recordCount > $display) $showPrevNext = 'true';

                //$params = array($form_filterorderid);
                //$params =  array("%" . $form_clientname . "%","%" . $form_clientname . "%","%" . $form_clientname . "%","%"
. $form_clientname . "%");
}


if (isset($deleteclient) && $deleteclient == 'true')
{
        $client = new Client();
        $client->deleteClient($clientid);
}

if (isset($created) && $created == 'new')
{
        $strFeedback = "Client and Client Account have been created successfully";
}

PrintHeader("OrderSystem -- Client Overview");
PrintLayoutTop("Client List", "Click on a Client Name to view the details", $os_session_user_id);

if ($strFeedback != "")
        echo '<div align=center><span class = "blue11">'.$strFeedback.'</span></div><br>';


?>



<form method="post" action="PageName.php">
<br>
<table border = 1 rules = none width = 50% align = center>
<tr>
        <td class=bold_text>Client Search</td>
        <td><input name="form_clientname" size = 13 value="<?php echo $form_clientname; ?>"></td>
</tr>
      <tr>
                <th class="bold_text" align=left>Biz Unit</th>
                <th class="bold_text" align=left>
                                                <select name=form_filterbizunit><option value='-1'></option>
                                                <?php
                                                        $bizunit = new Bizunit();
                                                        $bizunits = $bizunit->getBizunitIDList();

                                                        for ($i=0; $i < count($bizunits); $i++)
                                                        {
                                                                        $strSelected = "";
                                                                if ($bizunits[$i]==$form_filterbizunit) { $strSelected=" SELEC
TED "; };

                                                                $bizunit = new Bizunit($bizunits[$i]);
                                                                echo '<option value=' . $bizunits[$i] . ' ' . $strSelected.'>'
. $bizunit->getBizunitName() . '</option>';
                                                        }
                                                ?>
                                        </select>

</th>
                <th class="bold_text" align=left><input type="submit" name="filter" value="Search"></th>
        </tr>

</table>
</form>

<br>
<?php


        $res =& getRecordSetMessengerDB($strSelect, $params);
        ?>

<table width= 100% border="1" rules = rows class="text" align="left">
        <tr>
                <th class=greybg align="left">Username</th>
                <th class=greybg align="left">Company</th>
                <th class=greybg ></th>
                <th class=greybg ></th>
        </tr>

        <?php
        $i = 0;
        while ($row =& $res->fetchRow())
        {

                $strBackground = "";

                $user = new Client($row[0]);

                ?>
                <tr <?php echo $strBackground; ?> >
                        <td>
                          <a title = "Edit Client" href= "EditClient.php?<?php echo "clientid=" . $row[0] ?>"> <?php echo $use
r->getClientUsername(); ?> </a>
                        </td>
                        <td>
                          <?php echo $user->getClientCompanyName(); ?>
                        </td>
                        <td>
                          <a title="display client's orders" href = "OrderOverview.php?<?php echo "clientid=" . $row[0] ?>">ge
t orders</a>
                        </td>
                        <td>
                          <a title="Delete Client" href= "PageName.php?<?php echo "deleteclient=true&clientid=" . $row[0
] ?>" >x</a>
                        </td>
                </tr>


        <?php
        }

        ?>

        </table>
        <div style = "color:white">a</div>
        <table width=100%>

        <tr>

                <td width=90%></td>
                <td width=10%>

                        <?php
                        if (($recordCount > $display) || ($showPrevNext == 'true'))
                        {
                                if ($start > 0)
                                {
                                ?>
                                        <a href = "PageName.php?start=<?php echo $prev_value;?>"> prev </a>
                                <?php
                                }
                                ?>

                </td>
                <td>

                                        <a href = "PageName.php?start=<?php echo $next_value;?>"> Next </a>
                        <?php
                                }
                                ?>

                </td>
        </tr>

        </table>

<?php
PrintLayoutBottom();
PrintFooter();
?>
[/code]

I hope this explains properly what I'm trying to do.

Thanks in advance for any help that you can give me.

Mick
Link to comment
Share on other sites

I can see from the code

[code]if (!isset($start)) $start = 0;
$display = $listdisplay;
$prev_value = $start - $display;
$next_value = $start + $display;
$recordCount = $display+1;
$showPrevNext = 'false';
[/code]

That the next button will just take the start and add on the $listdisplay basically abandoning the search query completely. I'm still trying to figure out how to pass the query through the next button.

Anyone ??? Please? I'm going frigging nuts trying to figure this out!
Link to comment
Share on other sites

have your query search in the address bar using the GET method

also have the page number

[code=php:0]
if(!issit($_GET["page"])){
$page = 1;
}else{
$page = $_GET["page"];
}

$max_results = 10; //Change to what you wish

$from = (($page * $max_results) - $max_results);

//Add this to the end of the query
//LIMIT ".$from.", ".$max_results;

//Now the page numbering

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM table WHERE" //Make same as your query, except the limit and the beginning is COUNT(*) as Num


$total_pages = ceil($total_results / $max_results);

if($total_pages > 1){
echo "Select a Page
\n";

if($page > 1){
$prev = ($page - 1);
echo "<a href='".$_SERVER['PHP_SELF']."?page=".$prev."&search=".$search_string."'>&alt;&alt; Prev[/url] \n";
}

for($i = 1l $i <= $total_pages; $i++){
if(($page) == $i){
echo $i." ";
}else{
echo "<a href='".$_SERVER['PHP_SELF']."?page=".$i."&search=".$search_string."'>".$i."[/url] \n";
}

}

if($page < $total_pages){
$next = ($page + 1);
echo "<a href='".$_SERVER['PHP_SELF']."?page=".$next."&search=".$searchString."'>Next &gt;&gt;[/url]\n";
}


}
[/code]
Link to comment
Share on other sites

I am working on this myself today.  I am creating something my self, It's called pagination, the reason it's displaying another search is you have to limit the search with LIMIT 50
on the first one, then record 51 in a variable, then you can passit back to the same query and dynamically change those to
LIMIT 50, 51
then record 101 in a variable and refeed it
LIMIT 50, 101 then just keep going like that, it'll update the query each tiem, the minor technical issues I am ironing out on mine as well to keep updated on hte progress you'll see a recent post with me talking to myself awhile getting help on something and talking to myself again.
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.