我正在使用PHP制作我的第一个数据库(神奇宝贝数据库),我认为如果你能用'选项形式'对所有神奇宝贝进行排序会很好。 现在,我尝试使用多个选项表单,因此您可以组合不同的选项来获取某个精选小册子列表。
例如,如果您选择'type = water'和'按名称排序',您将获得所有水类型的神奇宝贝,按名称排序。我试图这样做,但是当我在其他表单中选择一个新选项时,它会在一种形式中忘记变量的值。
这是我的代码(非常草率,对不起)
HTML:
<form method="post" action="SQLpokemondb.php">
<select name="sort" id="sort" onchange="this.form.submit();">
<option value="ID" <?php if ($option == 'ID'){echo 'selected="selected"';};?>>ID</option>
<option value="Naam" <?php if ($option == 'Naam'){echo 'selected="selected"';};?>>Naam</option>
<option value="Soort" <?php if ($option == 'Soort'){echo 'selected="selected"';};?>>Soort</option>
</select>
</form>
</span>
<form method="post" action="SQLpokemondb.php">
<select name="gen" id="gen" onchange="this.form.submit();">
<option value="0" <?php if ($where == '0'){echo 'selected="selected"';};?>>All gens</option>
<option value="1" <?php if ($where == '1'){echo 'selected="selected"';};?>>Gen 1</option>
<option value="2" <?php if ($where == '2'){echo 'selected="selected"';};?>>Gen 2</option>
<option value="3" <?php if ($where == '3'){echo 'selected="selected"';};?>>Gen 3</option>
<option value="4" <?php if ($where == '4'){echo 'selected="selected"';};?>>Gen 4</option>
<option value="5" <?php if ($where == '5'){echo 'selected="selected"';};?>>Gen 5</option>
<option value="6" <?php if ($where == '6'){echo 'selected="selected"';};?>>Gen 6</option>
<option value="7" <?php if ($where == '7'){echo 'selected="selected"';};?>>Gen 7</option>
</select>
</form>
</span>
如果首次访问该页面,我添加了下一个PHP代码,为没有值的变量赋值:
if (empty( $_POST['sort']))
{
$option = 'id';
}
else
{
$option = $_POST['sort'];
}
if (empty( $_POST['gen']))
{
$where = 'ID=ID';
}
else
{
$where = $_POST['gen'];
}
if (empty( $_POST['type']))
{
$where2 = 'ID=ID';
}
else
{
$where2 = $_POST['type'];
}
选择选择时,它会自动提交表单。 我接下来要做的是确定使用PHP选择了哪个选项:
if (empty( $_POST['sort']))
{
$sort = 'id';
}
elseif ( $_POST['sort'] == 'ID' )
{
$sort = 'id';
}
elseif ( $_POST['sort'] == 'Naam' )
{
$sort = 'naam, ID';
}
elseif ( $_POST['sort'] == 'Soort' )
{
$sort = 'type, ID';
}
if (empty( $_POST['gen']))
{
$where = 'ID=ID';
}
elseif ( $_POST['gen'] == '0' )
{
$where = 'ID=ID';
}
elseif ( $_POST['gen'] == '1' )
{
$where = 'generation = 1';
}
elseif ( $_POST['gen'] == '2' )
{
$where = 'generation = 2';
}
elseif ( $_POST['gen'] == '3' )
{
$where = 'generation = 3';
}
elseif ( $_POST['gen'] == '4' )
{
$where = 'generation = 4';
}
elseif ( $_POST['gen'] == '5' )
{
$where = 'generation = 5';
}
elseif ( $_POST['gen'] == '6' )
{
$where = 'generation = 6';
}
elseif ( $_POST['gen'] == '7' )
{
$where = 'generation = 7';
}
最后,我将变量放入mysql的查询中:
mysql_connect("localhost", "root", "usbw");
mysql_select_db("pokemon");
$result = mysql_query("SELECT * FROM pokemon WHERE ($where2) AND ($where) ORDER BY $sort");
while($data = mysql_fetch_assoc($result)) { ........ }
我无法找到适合自己的方法,所以如果有人能帮助我,我将不胜感激。
答案 0 :(得分:0)
问题在于您使用的是两个FORM标签。如果第一个表单发送了它的值,则第二个表单将被忽略,反之亦然。因此,将输入包装在一个FORM中应该可以使它工作。
<form method="post" action="SQLpokemondb.php">
<select name="sort" id="sort" onchange="this.form.submit();">
<option value="ID" <?php if ($option == 'ID'){echo 'selected="selected"';};?>>ID</option>
<option value="Naam" <?php if ($option == 'Naam'){echo 'selected="selected"';};?>>Naam</option>
<option value="Soort" <?php if ($option == 'Soort'){echo 'selected="selected"';};?>>Soort</option>
</select>
<select name="gen" id="gen" onchange="this.form.submit();">
<option value="0" <?php if ($where == '0'){echo 'selected="selected"';};?>>All gens</option>
<option value="1" <?php if ($where == '1'){echo 'selected="selected"';};?>>Gen 1</option>
<option value="2" <?php if ($where == '2'){echo 'selected="selected"';};?>>Gen 2</option>
<option value="3" <?php if ($where == '3'){echo 'selected="selected"';};?>>Gen 3</option>
<option value="4" <?php if ($where == '4'){echo 'selected="selected"';};?>>Gen 4</option>
<option value="5" <?php if ($where == '5'){echo 'selected="selected"';};?>>Gen 5</option>
<option value="6" <?php if ($where == '6'){echo 'selected="selected"';};?>>Gen 6</option>
<option value="7" <?php if ($where == '7'){echo 'selected="selected"';};?>>Gen 7</option>
</select>
</form>
&#13;