The following code works as intended, but could it be shortened? Everything is placed into a variable named $page
and sent to another function to be printed. I know just because something works, doesn’t mean it is efficient.
Please don’t be too hateful to me should you see something terribly bad.
$year = !isset($_POST['year']) ? isset($_GET['year']) ? $_GET['year'] : '2011' : $_POST['year'];
$state = !isset($_POST['state']) ? isset($_GET['state']) ? $_GET['state'] : 'KS' : $_POST['state'];
$sport = !isset($_POST['sport']) ? isset($_GET['sport']) ? $_GET['sport'] : 1 : $_POST['sport'];
// A form that shows three dropdown boxes across the top of the screen for the user to select from
$page = <<<END
<form action="admin.php?do=teams" method="post">
<table>
<tr>
<td>Year:
END;
// Shows SELECTED year to user in dropdown box
$page .= "<select name=\\"year\\">\
<option selected value=\\"".$year."\\">".$year."</option>\
";
// Searches table xx_teams for all years posted. If there is only one record and it was
// made in 2011, then 2011 would be the only year it would find. More records with the same year
// would be omitted because of the DISTINCT in the query.
$query = "SELECT DISTINCT " .
"year " .
"FROM {{table}} " .
"ORDER BY year ASC";
$result = doquery($query, "teams");
// Dropdown box with all the years available for selection
while ($row = mysql_fetch_array($result)) { $page .= "<option value=\\"".$row['year']."\\">".$row['year']."</option>\
"; }
$page .= <<<END
</select>
</td>
<td>State:
END;
// Shows SELECTED state to user in dropdown box
$page .= "<select name=\\"state\\">\
<option selected value=\\"".$state."\\">".$state."</option>\
";
// Searches table xx_teams for all states posted just as the above code did for years.
// States are not saved as an id number. I chose to save them as two letter abbreviations.
$query = "SELECT DISTINCT " .
"state " .
"FROM {{table}} " .
"ORDER BY state ASC";
$result = doquery($query, "teams");
// Dropdown box with all the states ( abbreviations ) available for selection
while ($row = mysql_fetch_array($result)) { $page .= "<option value=\\"".$row['state']."\\">".$row['state']."</option>\
"; }
$page .= <<<END
</select>
</td>
<td>Sport:
END;
// This just puts the sport id into SELECTED numerical value
$page .= "<select name=\\"sport\\">\
<option selected value=\\"".$sport."\\">";
// This searches table xx_sport for the sport associated with the value above.
// I did it this way because sport is a number in each record that has little meaning to user
// It takes the number, matches it with the id and gets the sport
$query = "SELECT DISTINCT " .
"ws_sport.sport, ws_sport.id " .
"FROM {{table}} " .
"WHERE ws_sport.id='$sport' LIMIT 1";
$result = doquery($query, "sport");
$row = mysql_fetch_array($result);
// Show selected sport
$page .= $row['sport']."</option>\
";
// Search table xx_sport to fill dropdown box with all available sports
$query = "SELECT DISTINCT " .
"ws_sport.sport, ws_sport.id " .
"FROM {{table}} " .
"ORDER BY ws_sport.id ASC";
$result = doquery($query, "sport");
// Dropdown box with all sports listed
while ($row = mysql_fetch_array($result)) { $page .= "<option value=\\"".$row['id']."\\">".$row['sport']."</option>\
"; }
$page .= <<<END
</select>
</td>
<td class="data"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
END;