doujuxin7392 2014-10-27 15:26
浏览 46
已采纳

试图在PHP中进行复杂的搜索

Scenario

I currently have a database hooked up to my PHP pages running from localhost, one of those PHP pages is a shop that's items are populated from the database.

I've allowed the user to search for particular items, for instance:

"List every item in the shop that is under < $5.00"

And I have achieved that. I currently have four simple searches to search for items in the shop, these searches are by: "Category","Name","Availability" and "Price"

So if the user entered a Category of "Gift" then all of the "Gift"s should show up, and that already works.


And although these single-Criteria searches give results? I'd like the user to be able to select from ALL four, or three or two criteria options at the same time.

My attempt at constructing the PHP looks a little like this, and this doesn't work.

    <?php 
    //check to make sure the form has been submitted and retrieve the contents of 
    //both things and make variables.
    if(isset($_POST['search'])){
    $get_price=$_POST['price'];
    $get_function=$_POST['Operation'];
    $get_name=$_POST['UserEnteredText'];

$get_Availability=$_POST['AvailabilityOperation'];
$get_Category=$_POST['CategoryOperation'];
//create the query to extract the data from the table and store
//the SQL query in a variable called $query
/*$query="SELECT * FROM friends WHERE name = '$get_name'";*/

if ($get_function == 'Higher')
{
$queryPrice="SELECT * FROM product WHERE price < $get_price";
}
else
{
$queryPrice="SELECT * FROM product WHERE price > $get_price";
}

if ($get_name = $get_name)
{
$queryName="SELECT * FROM `product` WHERE `Product Name` like '%$get_name%'";
}
else
{
//doesn't work  
}

if ($get_Availability == 'Now')
{
$queryAvailability="SELECT * FROM product WHERE availability LIKE '%Now%'";
}
elseif ($get_Availability == 'Out Of Stock')
{
$queryAvailability="SELECT * FROM product WHERE availability LIKE '%Out%'";
}
else
{
$queryAvailability="SELECT * FROM product WHERE availability";
}

if ($get_Category == 'Garden')
{
$query="SELECT * FROM product WHERE category LIKE '%Garden%'";
}
elseif ($get_Category == 'Gift')
{
$query="SELECT * FROM product WHERE category LIKE '%Gift%'";
}
elseif ($get_Category == 'Test')
{
$query="SELECT * FROM product WHERE category LIKE '%Test%'";
}

//the result set of the mysql_query function is then stored in
//an array called $result
$result=mysqli_query($connection, $queryPrice, $queryName, $queryAvailability, $get_Category);
//create while loop and loop through result set, reading each one 
//and displaying it through the use of an echo command

while ($row = mysqli_fetch_array($result)) {

    $itemname=$row['Product Name'];
    $itemPrice=$row['Price'];
     $itemavailability=$row['Availability'];
?>
    <div class="item">
        <h3><?php echo $itemname. " " ?></h3>
        <a href="<?php echo $row["Product Image Path"] ?>" data-lightbox="Perfecto">
            <img src="<?php echo $row["Product Image Path"] ?>" width="70" height="70" alt="Sculpture1"> </a>
        <br />
        Price: £<?php echo $itemPrice. " " ?>
        <form action="" method="post">
            <input type="submit" value="Buy" />
        </form>
    </div>


<?php
}

}
?>

Question

Wondering if any experienced PHP programmers could help me understand how to construct an algorithm that takes in all of these values and produces he same results as my individual searches do.

If you're wondering how I got to this conclusion I tried to combine all four of my other searches (That all work) into a more complex search.

Would really appreciate it, thanks.

  • 写回答

2条回答 默认 最新

  • dsepcxw181184853 2014-10-27 15:44
    关注

    Rather than using an IF/ELSE to return a specific query you could try building up your query programatically based on params passed in. eg.

    $sql = "select * from `product` where `product name` like '%$get_name%'";
    $sql .= (isset($get_price) ? " and `price` < $get_price" : "");
    $sql .= (isset($get_Category) ? " and `category` LIKE '$GET_Category'" : "");
    

    You will end up writing far less code that way and it will be far easier to add new criteria in as your search application grows.

    Cheers Ash

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab