drn34916 2011-08-10 20:30
浏览 48
已采纳

PHP订单搜索 - 添加项目以订购刷新页面,如何应用旧的搜索查询?

I am creating a simple search feature that queries a user input string for a matches in a products catalog that is held within a table in mysql.

This is my code for this search form:

<form name="search" method="post" action="'.$_SERVER['PHP_SELF'].'">
    <div>
    Search For Product: <input type="text" name="find" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;" />
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </div>
</form>

Here is the code that processes this string:

//This is only displayed if they have submitted the form 
if ($searching == "yes") 
{ 
$pageContent = '
<h2>Place your order</h2>
<p>Your order contains '.count($_SESSION['order']).' items. (<a href="?orders">View your order</a>)</p>
<form name="search" method="post" action="'.$_SERVER['PHP_SELF'].'">
    <div>
    Search For Product: <input type="text" name="find" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;" />
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </div>
</form>
<br />
';


//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
$pageContent .= '<p>You forgot to enter a search term</p>';
echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $footer . $pageScripts; 
exit;
} 

// Otherwise we connect to our Database 
$bccConn   = mysql_connect($bccHost, $bccUser, $bccPass) or exit(mysql_error());
             mysql_select_db($bccDB, $bccConn) or exit(mysql_error());

// We preform a bit of filtering 

$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 
$keywords_array = explode(' ', $find);

//Now we search for our search term, in the field the user specified 
$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE '%" 
   . implode("%' AND upper(`desc`) LIKE '%", $keywords_array) 
   . "%'";

$data = mysql_query($dataQuery) or die(mysql_error());

//And we remind them what they searched for
$pageContent .= '
<p><b>Searched For:</b>  ' . $find . '</p>
';

$tempVar = 0;
//And we display the results 
while ($result = mysql_fetch_array($data)) {
$prId = $result['id'];
$prRefCode = $result['refCode'];
$prDesc = $result['desc'];
$prPack = $result['pack'];
$prMeasure = $result['measure'];
$prQuantity = $result['quantity'];
$prDeptCode = $result['deptCode'];
$prTaxable = $result['taxable'];
$prPrice1 = $result['price1'];
$prPrice2 = $result['price2'];
$prCrdCode = $result['crdCode'];
$prCost1 = $result['cost1'];
$prCost2 = $result['cost2'];

if ($tempVar == 0) {
$pageContent .= '
<p>All prices are inclusive of VAT</p>
<table border="1">
<thead>
    <tr>
        <th>Stock Code</th>
        <th>Description</th>
        <th>Packsize</th>
        <th>Price</th>
        <th>In-Stock?</th>
        <th>Quantity</th>
        <th>Submit</th>
    </tr>
</thead>
<tbody>
';
}
if ($tempVar == mysql_num_rows($data)) {
    $pageContent .= '
</tbody>
</table>
';
}

    $pageContent .= '
<tr>
    <td>' . $prId . '</td>
    <td>' . $prDesc . '</td>
    <td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td>
    <td>R' . $prPrice1 . '</td>
';
    if (empty($prQuantity)) {
        $pageContent .= '
<td>No</td>
';
    } else {
        $pageContent .= '
<td>Yes</td>
';
    }
    $pageContent .= '
    <form action="" method="post">
    <td>            
            <div>
                <input type="text" name="quantity" size ="2" value ="1" style="background: #F4F4F4; font-family: "Lucida Console", Monaco, monospace;"/>
            </div>            
    </td>
    <td>
            <div>
                <input type="hidden" name="id" value="' . $prId . '" />
                <input type="submit" name="action" value="Order" />
            </div>            
    </td>
    </form>           
 </tr>
 ';
    $tempVar ++;
 }
 $pageContent .= '
 </tbody>
</table>
';

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches = mysql_num_rows($data);
if ($anymatches == 0) {
    $pageContent .= '
<p>Sorry, but we can not find an entry to match your query</p>
';
}
}

Here is the code that deals with the event of a user clicking the "Order" button:

if (!isset($_SESSION['order']))
{
$_SESSION['order'] = array();
}

if (!isset($_SESSION['quantity']))
{
$_SESSION['quantity'] = array();
}

if (isset($_POST['action']) and $_POST['action'] == 'Order')
{
// Add item to the end of the $_SESSION['order'] array
$_SESSION['order'][$_POST['id']] = $_POST['id'];
$_SESSION['quantity'][$_POST['id']] = $_POST['quantity'];
header('Location: .');
exit();
}

What I would like to happen, is when the user clicks on order, after the header redirect, the page should not be directed to the folder index, instead, it should be refreshed with the current search queries results intact.

At the moment, when the user clicks on order, they are redirected to the index page, and in this process, lose whatever search results they had previously called.

I understand that I would need to add the users search string to a session, then recall this information after the page has been refreshed.

What I am struggling with though, is the process involved in this.

If anyone has any ideas on how i would go about this, I would really appreciate the advice!

Thanks!

  • 写回答

1条回答 默认 最新

  • dpii73380 2011-08-10 20:37
    关注

    My first thought is simply to stick either the $find or the $keywords_array into $_SESSION when you conduct the search. You'd need to update the code to check that $_SESSION variable as part of the process before issuing the "Enter a search term" error.

    Since you're already using $_SESSION it is entirely possible that you've already headed down this road though. If that's the case, what went wrong?

    Edit to include code -- Basically, I would change the second code segment as follows:

    if (($searching == "yes") || (isset($_SESSION['find'])))

    ...[your code]... until if ($find == "") which is replaced with:

    if (isset($_SESSION['find']) && ($find == "") 
       $find = $_SESSION['find'];
    
    if ($find == "")
    

    ...[your code]... until $keywords_array = explode(' ', $find); which gets one line added before it:

    $_SESSION['find'] = $find;

    ...[your code as before]...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记