我有一个取决于商店编号的结果列表,每个商店编号都印有其所有信息,第一个选项“ ALL”显示所有商店编号中的信息,我希望选项“ ALL”仅显示商店编号中的信息1,2和3,不是全部。
这是我的代码:
<?php $store_number=$_REQUEST["store_number"]; ?>
<form name="selecciona" action="despl_probl.php" method="post">
<td> <select name="store_number" id="store_number">
<?php print(($store_number=='0'?'<option value="0"
selected>ALL</option>':'<option value="0">ALL</option>')) ?>
<?php print(($store_number=='1'?'<option value="1" selected>Store
1</option>':'<option value="1">Store 1</option>')) ?>
<?php print(($store_number=='2'?'<option value="2" selected>Store
2</option>':'<option value="2">Store 2</option>')) ?>
<?php print(($store_number=='3'?'<option value="3" selected>Store
3</option>':'<option value="3">Store 3</option>')) ?>
<?php print(($store_number=='4'?'<option value="4" selected>Store
4</option>':'<option value="4">Store 4</option>')) ?>
<?php print(($store_number=='5'?'<option value="5" selected>Store
5</option>':'<option value="5">Store 5</option>')) ?>
先谢谢了。
最佳答案
为了扩大我的评论范围,您可以执行以下操作,以很好的方式显示所需的数据;
<form name="selecciona" action="despl_probl.php" method="post">
<td>
<select name="store_number" id="store_number">
<?php
$store_number=$_REQUEST["store_number"];
// Select the right option for this - will only print the one it finds
switch ($store_number)
{
case "0" : {
print "<option value='0' selected>Store 0</option>";
break;
}
case "1" : {
print "<option value='1' selected>Store 1</option>";
break;
}
case "2" : {
print "<option value='2' selected>Store 2</option>";
break;
}
case "3" : {
print "<option value='3' selected>Store 3</option>";
break;
}
case "4" : {
print "<option value='4' selected>Store 4</option>";
break;
}
case "5" : {
print "<option value='5' selected>Store 5</option>";
break;
}
default : {
// Default to print the right one at the right time
print "<option value='{$store_number}'>Store {$store_number}</option>";
}
}
?>
进行一些更整洁,更好的排序是执行以下操作:
<?php
$store_number=$_REQUEST["store_number"];
$max_option_num = 5;
for ($i = 0; $i <= $max_option_num; $i++)
{
$selected = "";
if ($i == $store_number) $selected = "selected";
print "<option value='{$store_number}' {$selected}>Store {$store_number}</option>";
}
?>
从新的解释;
$store_number=$_REQUEST["store_number"];
if ($store_number == "0")
{
print "<option value='0' selected>All</option>";
}
else
{
// Print the "All" first to have this there ready
print "<option value='0'>All</option>";
// These will be your stored & retrieved values
$numbers_from_database = array(1, 4, 6, 28, 33, 56);
foreach ($numbers_from_database as $store_num)
{
$selected = "";
if (store_num == $store_number) $selected = "selected";
print "<option value='{$store_num}' {$selected}>Store {$store_num}</option>";
}
}
关于php - PHP MYSQL打印,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50952833/