使用下面的 foreach 循环修改/添加到代码中,以仅显示所选价格范围内的书籍



我不知道如何 使用下面的 foreach 循环修改/添加代码以仅显示其中的书籍 所选的价格范围。我希望 20 美元以下的书籍只显示在 20 美元部分下。低于 50 美元和低于 100 美元的情况相同。

<?php
$books = array ();
$books[0] = array();
$books[0]['ISBN'] = "1234567890";
$books[0]['Title'] = "PHP for Dummies";
$books[0]['Price'] = "35.99";
$books[1] = array();
$books[1]['ISBN'] = "2345678901";
$books[1]['Title'] = "SQL for Dummies";
$books[1]['Price'] = "78.49";
$books[2] = array();
$books[2]['ISBN'] = "3456789012";
$books[2]['Title'] = "Economics for Dummies";
$books[2]['Price'] = "44.99";
$books[3] = array();
$books[3]['ISBN'] = "4567890123";
$books[3]['Title'] = "History of Dummies";
$books[3]['Price'] = "62.49";
$books[4] = array();
$books[4]['ISBN'] = "5678901234";
$books[4]['Title'] = "Marketing for Dummies";
$books[4]['Price'] = "52.49";
$books[5] = array();
$books[5]['ISBN'] = "6789012345";
$books[5]['Title'] = "Gardening for Dummies";
$books[5]['Price'] = "19.99";
// function to generate the book list
function listBook($maximum){
global $books; // make $books array available inside the function
echo "<table>n
<caption>Under $maximum</caption>
<tr><th>Title</th><th>ISBN</th><th>Price</th></tr>n
";
//###############################
// Try the script in its current form.  You will find that no matter what price range is chosen, all books are presented all the time.  It's not what we want.
// Modify/Add to the code with the foreach loop below to only  show the books within the price range selected.
//###############################
foreach ($books as $book) {
$title = $book['Title'];
$ISBN = $book['ISBN'];
$price = $book['Price'];

echo "<tr><td>$title</td> <td>$ISBN</td> <td>$price</td></tr>n";
}
echo "</table>";
}

用一个简单的if:

function listBook($maximum) {
global $books; // make $books array available inside the function
echo "<table>n
<caption>Under $maximum</caption>
<tr><th>Title</th><th>ISBN</th><th>Price</th></tr>n
";
//###############################
// Try the script in its current form.  You will find that no matter what price range is chosen, all books are presented all the time.  It's not what we want.
// Modify/Add to the code with the foreach loop below to only  show the books within the price range selected.
//###############################
foreach ($books as $book) {
$title = $book['Title'];
$ISBN  = $book['ISBN'];
$price = $book['Price'];
if ($price <= $maximum) {
echo "<tr><td>$title</td> <td>$ISBN</td> <td>$price</td></tr>n";
}
}
echo "</table>";
}

最简单的方法是将输出 HTML 的 echo 语句包装在 IF 语句中。假设您有一个保存所选价格的变量,那么您可以这样做:

$selectedPrice = 20;
if($price <= $selectedPrice) {
echo "<tr><td>$title</td> <td>$ISBN</td> <td>$price</td></tr>n";
}

或者,价格介于高价和低价之间

$highPrice = 100;
$lowPrice = 50
if($lowPrice <= $price && $price <= $selectedPrice) {
echo "<tr><td>$title</td> <td>$ISBN</td> <td>$price</td></tr>n";
}

但是,您的示例不包括如何收集价格,尽管可能的机制是选择下拉列表或几个输入框。

希望有帮助。

你的 foreach 循环:

foreach ($books as $book) {
$title = $book['Title'];
$ISBN = $book['ISBN'];
$price = $book['Price'];
if ((int) $price > round($maximum, 2))
continue;
echo "<tr><td>$title</td> <td>$ISBN</td> <td>$price</td></tr>n";
}
echo "</table>";
}

最新更新