Im looking to create a form that contains a few htnl select boxes using data from a table in MYSQL.
So far I have managed to create a single drop down box and could set up another two but I think im making the process hard work.
So please feel free to show me the smart way (and take the piss out of my long winded messy code - im still learning but take it all on the chin!) to do this, rather than creating 3 seperate queries and 3 sep arrays!
In the code below I have queried the ward and party table but only created th select for the ward as I was getting annoyed at how long this seemed to take!
<?php // This is the hone page for the site.
// set the title for the html header
$page_title = 'Header';
// require the config file
require_once ('../includes/config.inc.php');
// include the header filde
// add header file once created
// require the database settings file using information from config.inc.php
require_once (MYSQL);
// Select the values for the ward drop down menu
$q="SELECT ward, ward_id FROM ward";
// OREDER BY id DESC is order result by descending - look at ordering by abc
// For debugging return any errors or run the query
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
// check the query returnbed any results of not we need to tell the user so they understand why the page is blank
if (mysqli_affected_rows($dbc) == 0) {
// if no new topics have been created tell the user
echo '<p><h1>No wards available on the system</h1></p>';
}// close the statement
// Select the values for the political party drop down menu
$q2="SELECT party.party, party.party FROM party";
// OREDER BY id DESC is order result by descending - lok at ordering by abc
// For debugging return any errors or run the query
$r2 = mysqli_query ($dbc, $q2) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
// check the query returnbed any results of not we need to tell the user so they understand why the page is blank
if (mysqli_affected_rows($dbc) == 0) {
// if no new topics have been created tell the user
echo '<p><h1>No political parties available on the system</h1></p>';
} // close the statement
// close php and open HTML select tag
?>
<!-- Start HTML select tag for drop down-->
<select>
<?php // open php to create the array
// Start looping data ready for drop downs
while ($rows=mysqli_fetch_array($r, MYSQLI_ASSOC)){
// close php and open HTML loop the array in the open select tag
?>
<!-- Start HTML select tag and echo array into select tag-->
<option value="<?php echo $rows['ward_id']; ?>"><?php echo $rows['ward']; ?></option>
<?php // open php to close looping and database connection
// exit looping
}
// close php tag and open HTML to close select tag
?>
<!-- Close HTML select tag-->
</select>
<?php // Standard end of scripts include footr and close database connection for security
// close connection
mysqli_close($dbc);
// include the footer
// add footer file once created
// close the PHP
?>
Thanks in advance!