Manipulate HTML table data with multiple form field submission in PHP -
i've got table of data user can filter using variety of options.
i can manipulate data form submission fine form accepts both $_post
variables , in cases want user use one.
mp | conservative | year elected
for example:
case #1 - user searches 'conservative` party doesn't care year elected.
case #2 - user searches '2005' year of election doesn't care political party.
case #3 - user searches 'conversative' party , in '2005'.
and here's code:
global variables
if(empty($_post['year_elec'])) { $year = "show_all"; } else { $year = $_post['year_elec']; } if(empty($_post['party'])) { $party = "show_all"; } else { $party = $_post['party']; }
the main table generation:
if($year == "show_all" && $party == "show_all") { $query = "select * mps"; } else if(!empty($year) && $party ="show_all"){ $query = "select * mps year_elec = $year"; } else if($year == "show_all" && !empty($party)) { $query = "select * mps party = $party"; } else { $query = "select * mps year_elec = $year , party = $party"; } $res = mysqli_query($conn, $query); while ($a = mysqli_fetch_assoc($res)) { $name = $a['name']; $party = $a['party']; $consti = $a['cont']; $year_elec = $a['year_elec']; echo "<tr>"; echo "<td>" .$name. "</td>"; echo "<td>" .$party. "</td>"; echo "<td>" .$consti. "</td>"; echo "<td>" .$year_elec. "</td>"; echo "</tr>"; }
the form
<form method="post" action="mps.php"> <span class="css-select-moz"> <select name="year_elec" id="selectyear"> <option <?php if (isset($year) && $year=="2010") echo "selected";?> name="2010" value="2010">first elected 2010</option> <option <?php if (isset($year) && $year=="2005") echo "selected";?> name="2005" value="2005">first elected 2005</option> <option <?php if (isset($year) && $year=="2001") echo "selected";?> name="2001" value="2001">first elected 2001</option> </select> </span> <br /> <span class="css-select-moz"> <select name="party" id="selectparty"> <?php $query = "select distinct party mps"; $res = mysqli_query($conn, $query); while($a = mysqli_fetch_assoc($res)) { $party = $a['party']; echo "<option name='$party' value='$party'>$party</option>"; } ?> </select> </span> <input class="submit" type="submit" value="load" /> </form>
i dont see problem in code, apart single =
in 1st else if
.
however can neaten things ternary operator assign values $year
, $party
. , can use not equal operator instead of not empty:
$year = $_post['year_elec']?:'show_all'; $party = $_post['party']?:'show_all'; if($year == "show_all" && $party == "show_all") { $query = "select * mps"; } else if($year != "show_all" && $party == "show_all"){ $query = "select * mps year_elec = $year"; } else if($year == "show_all" && $party != "show_all") { $query = "select * mps party = $party"; } else { $query = "select * mps year_elec = $year , party = $party"; }
also note open sql injection attacks, ideally use prepared statements , whitelist of values use can select (an array of political parties, , array of dates)
Comments
Post a Comment