I'm making my first database (pokémon database) for school using PHP and I thought it would be nice if you could sort all pokémon using an 'option form'. Now, I tried to use multiple option forms, so you could combine different selections to get a certain list of pokemon.
For example, would you select 'type=water' and 'sort by name', you would get all water type pokémon, sorted by name. I've tried to do this, but it keeps forgetting the value of the variable in one form, when I select an new option in my other forms.
this is my code (it's very sloppy, sorry for that)
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>
In case of first visiting the page, I added the next PHP code to give values to variables without a value:
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'];
}
It will automatically submit the form when you choose a selection. Next thing I do is determine which option was picked using 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';
}
Last, I put the variables into the query for 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)) { ........ }
I was unable to find a fix for myself, so I would be grateful if someone could help me.