【问题标题】:Filter results by Product Type and Sort List by Price (PHP/SQL)按产品类型过滤结果并按价格排序列表 (PHP/SQL)
【发布时间】:2010-09-21 20:30:26
【问题描述】:

我正在学习如何创建电子商务网站。我试图弄清楚如何按价格订购产品结果。下面的代码是一个菜单列表,用于过滤特定产品类别或查看所有产品的选项——该代码有效。

我不确定如何将代码添加到“按价格排序”(从低到高,从高到低)。 我尝试创建一个名为priceList 的函数并在函数categoryList 内部调用(它查询数据库的产品类型或查看所有产品),但不起作用。

    function priceList() {
      $con = getConnection();

     if($pUserCat == "lowToHigh") {
        $sqlQuery = "SELECT Price from Products
         ORDER BY Price ASC";
     } elseif($pUserCat == "highToLow") {
        $sqlQuery = "SELECT Price from Products
         ORDER BY Price DESC";

     // Execute Query -----------------------------           
            $result = mysqli_query($con, $sqlQuery);
      if(!$result) {
       echo "Cannot do query" . "<br/>";
       exit;
      }

      $row = mysqli_fetch_row($result);
      $count = $row[0];

      if ($count > 0) {
       echo "Query works" . "<br/>";
      } else {
       echo "Query doesn't work" ."<br/>";
      }

              // Display Results -----------------------------

      $num_results = mysqli_num_rows($result);

      for ($i=0; $i<$num_results; $i++) {
       $row = mysqli_fetch_assoc ($result);
         // print_r($row);
        echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
        echo "Price: " . stripslashes($row['Price']);
      }

      // Close connection
      closeConnection($con);
}

表格

    <!--category and price form ------------------------- -->
    <form action="register_script.php" name="frm" method="post">
        <select name="category" id="category">
            <option value="viewall">View All</option>
            <option value="dress">Dress</option>
            <option value="athletic">Athletic</option>
            <option value="sandals">Sandals</option>
       </select>

      <input type="submit" value="Go" />

  </form>


   <form action="register_script.php" name="frm" method="post">

        <select name="price" id="price">
            <option value="lowToHigh">Low to High</option>
            <option value="highToLow">High to Low</option>
       </select>

       <input type="submit" name="orderPrice" value="orderPrice" />

    </form>
    </div>

PHP

<?php
    function categoryList($pUserCat=false) {
    echo "I am in category list" . "<br/>";

    $con = getConnection(); 

     $sqlQuery = "SELECT * from Products";

    if($pUserCat == "athletic") {
       $sqlQuery = "SELECT * from Products
                    WHERE ProductType='athletic'";
    } elseif ($pUserCat == "dress") {
        $sqlQuery = "SELECT * from Products
                    WHERE ProductType='dress'";
    } elseif ($pUserCat == "sandals") { 
            $sqlQuery = "SELECT * from Products
                    WHERE ProductType='sandals'";
    } elseif ($pUserCat == "viewall") {                 
       $sqlQuery = "SELECT * from Products";
    }

    // Execute Query -----------------------------           
        $result = mysqli_query($con, $sqlQuery);
        if(!$result) {
            echo "Cannot do query" . "<br/>";
            exit;
        }

        $row = mysqli_fetch_row($result);
        $count = $row[0];

        if ($count > 0) {
            echo "Query works" . "<br/>";
        } else {
            echo "Query doesn't work" ."<br/>";
        }

          // Display Results -----------------------------

        $num_results = mysqli_num_rows($result);

        for ($i=0; $i<$num_results; $i++) {
            $row = mysqli_fetch_assoc ($result);
           // print_r($row);
          echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
          echo "Price: " . stripslashes($row['Price']);
        }


         // priceList();
        $priceOrder = getPriceBtn(); //$priceOrder holds value of option selected in         
     //price order menu
    priceList($priceOrder);
        // Close connection
        closeConnection($con);

}


function getPriceBtn() {    
    //echo "<br/>"."I am calling getPriceBtn function" . "<br/>";
    $priceBtn = $_POST["orderPrice"];           // price button
    return $price;
    //echo $priceBtn;
}

?>

【问题讨论】:

  • 不知道是不是拼写错误但是在执行高/低查询之前忘记关闭 if/else
  • priceList() 似乎没有接受任何参数。 $pUserCat 是全局变量吗?
  • @Phill Pafford 感谢您发现这一点,是的,我忘记了结束大括号
  • @dionyziz $pUserCat 是一个局部变量,只存在于函数 categoryList 中。我明白了,我应该在 priceList 中传递 $pUserCat ...

标签: php sql


【解决方案1】:

我认为您需要将 $pUserCat 传递给函数 priceList($pUserCat);

提示,您应该有一个从高到低的默认查询,如果选择了其他选项,则运行该查询:

$sqlQuery = "SELECT Price FROM Products ORDER BY Price DESC";

if($pUserCat == "lowToHigh") {
   $sqlQuery = "SELECT Price FROM Products ORDER BY Price ASC";
}

你也可以重写这个:

$sqlQuery = "SELECT * from Products"; // 这个需要吗?

if($pUserCat == "athletic") {
   $sqlQuery = "SELECT * from Products
                WHERE ProductType='athletic'";
} elseif ($pUserCat == "dress") {
    $sqlQuery = "SELECT * from Products
                WHERE ProductType='dress'";
} elseif ($pUserCat == "sandals") { 
        $sqlQuery = "SELECT * from Products
                WHERE ProductType='sandals'";
} elseif ($pUserCat == "viewall") {                 
   $sqlQuery = "SELECT * from Products";
}

喜欢这个categoryList($pUserCat):

if($pUserCat != 'viewall') {
   $sqlQuery = "SELECT * FROM Products WHERE ProductType='%s'";
   $sqlQuery = sprintf($sqlQuery, $pUserCat);
} else {
   $sqlQuery = "SELECT * from Products";
}

【讨论】:

  • 我很感激这个提示,我会看看我是否可以用你给我的东西让我的代码更干净
  • 当时认为在 priceList 中传递 $pUserCat 会起作用,但 $pUserCat 保存用户在“产品菜单”而不是“按价格排序”菜单中选择的值。我更新了上面的代码并包含了一个名为 getPriceBtn 的函数。现在它不起作用,但我认为逻辑是正确的方向......
  • 然后在 priceList 中传递 $priceOrder >> priceList($priceOrder);
【解决方案2】:

局部函数变量不共享。如果在函数中定义了函数变量,则从该函数中调用的函数无法访问父级的局部变量,这正是它们被称为“局部”的原因。您需要将变量作为参数传递:

定义时:

function priceList( $pUserCat ) {
    ...
}

调用时:

priceList( $pUserCat );

【讨论】:

  • 当时认为它会起作用。我知道我需要将一个变量传递给 priceList,但另一个变量不是 $pUserCat。 $pUserCat 保存用户在“产品菜单”中选择的值(查看全部,或查看运动鞋、连衣裙或凉鞋)。我需要传入一个变量,该变量包含用户在“按价格排序”菜单中选择的值(从低到高,从高到低)
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 2016-06-04
  • 2016-04-01
  • 2019-02-28
  • 2021-01-10
  • 2020-08-17
  • 2021-12-04
  • 1970-01-01
相关资源
最近更新 更多