我怎样才能正确地继续我的if语句在我的代码


How can I continue correctly my if statements in my code?

脚本应该这样做:

检查产品是否有$startprice和$endprice。这些变量将根据$price(产品的价格)进行检查。

用户可以同时添加($startprice和$endprice),也可以只添加其中一个,也可以不添加。

  • 如果他只添加了$startprice,则$price必须为
  • 如果他只添加了$endprice,则$price必须小一些比这。
  • 如果两者都添加,则$price必须介于两者之间。
  • 如果他没有添加任何东西,脚本都会显示出来结果。

如果数据满足语句,它将显示产品列表。在所有情况下,产品的列表代码是相同的。

*我所做的直到现在是这样的,但我不能继续正确

产品的价格命名为$price,我可以得到每个产品的正确价格。

   <?php
    $startprice = $_GET['startprice'];
    $endprice = $_GET['endprice'];
    // this is the between code and it's the only I know how to do...
    if (($startprice <= $price) && ($endprice >= $price)) { ?>
    <h2> <a href="<?php the_permalink(); ?>"> <strong>  <?php the_title(); ?>  </strong></a></h2>
 <? } ?>

你的问题不清楚。但就我所理解的来看,不妨试试……

<?php
    $startprice = isset($_GET['startprice']) ? $_GET['startprice'] : 0;
    $endprice = isset($_GET['endprice']) ? $_GET['endprice'] : PHP_INT_MAX;
    // this is the between code and it's the only I know how to do...
    if (($startprice <= $price) && ($endprice >= $price)) { ?>
    <h2> <a href="<?php the_permalink(); ?>"> <strong>  <?php the_title(); ?>  </strong></a></h2>
 <? } ?>

你需要包含更多的上下文。

例如,很明显有一些产品,它们的价格与用户指定的价格相比较。这些产品的格式是什么,它们是多维数组吗?

例如,价格是否可以这样定义:

$PRODUCTS[$prod_id]['price'] = 9.99

或者在"price"数组中,按产品id索引?

$PRICE[$prod_id] = 9.99

或者在一个对象中?

$product->price

我们需要很多更多的上下文来帮助。

这里只是猜测,因为您没有向我们展示太多代码:

if(!isset($startprice)) $startprice = 0;
if(!isset($endprice)) $endprice = PHP_INT_MAX;
foreach($theproducts as $product) {
    if($product['price'] >= $startprice && $product['price'] <= $endprice) {
        echo $product['name'].'<br/>';
    }
}