From what I've gathered browsing SO, I'm the worst nightmare of good people like you: someone who never formally learned any programming, decided he'd like to enhance his hobby website, and bought a fast-track PHP book off the shelf and went at it.
Browsing SO has taught me a great deal of things, highlights of which include:
- PHP is terrible for beginners (of which I am)
- PHP is a terrible first language (it's my first)
- lots and lots of PHP code is awful (mine probably is)
- most people have no clue how to template in PHP (my book recommends Smarty
<grin>
) - functions are awesome, if you have a clue (I don't)
In light of reading pages and pages of veteran coder ramblings, I have become perhaps the world's most paranoid and self-conscious beginner.
Which is why I now prostrate myself before you, offering up my firstborn child... err, chunk of working code, and humbly beg that you tear & criticize it to pieces, that I may more expeditiously learn the error of my ways...
The goals of the following code:
1) Grab a list of units from table unit_basic_data
based on their nationality, returning their unitid
for query purposes and their unitname
for display to user.
2) Build a form with checkboxes for each unit in this format
`<tr><td><input type="checkbox" name="Americanunit[]" value="$unitid" />$unitname</td></tr>`
You'll note that "American" is currently manually set to $nation
and then appended to unit[]
-- this is to allow me to add a dynamic country select later.
3) For each checked box, submit an update query to table scenario_needunits
composed of the scenario id scenid
and unit id unitid
, which taken as a pair are a primary key.
4) Dump out a rough list of unitids updated to the user and a confirmation of completion message.
What the code does now: Everything! :-) Except in a rather ugly fashion.
Obvious problems: no sanitization (btw, do checkboxes require sanitizing?), no error reporting if user tries to submit a scenario|unit pair that's already in the table
Further questions to the experts:
- Is there a cleaner way to build the checkbox strings besides my approach that concatenates SIX variables?
- Is this a templating abomination?
- Can it be split into multiple files? (if yes, how/where?)
- Am I already hopeless?
Aside from the above, I am 100% open to any other comments & criticism. I thank ALL of you most kindly for your time, and your patience. And I promise the next bit of code I post to SO will look a lot nicer!
Code follows:
<?php
// designed to dynamically load a country's unit list from the database
// and allow you to submit the country's units needed for a scenario
error_reporting(E_ALL);
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$nation = "American"; // desired nation
echo "$nation is selected.<br />";
$sql = "SELECT unitid,unitname FROM `unit_basic_data` WHERE `forcename`='$nation'"; // grab all current units for $nation
$result = mysql_query($sql);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
echo "Total rows returned is $rows rows."; // display total number of units
$formOpen = '<form method="post" action="scenario-needunits2.php"><input type="hidden" name="submittedCheck" value="yes" /><br />';
$scenario = '<tr><td>Scenario Name: <input type="text" name="scenid" /></td></tr>'; // input target scenario for new data
$checkPre = '<input type="checkbox" name="'; // checkbox string part 1 of 5
$checkName = $nation; // checkbox string part 2 of 5
$checkMid = 'unit[]" value="'; // checkbox string part 3 of 5
$checkPost = '" />'; // checkbox string part 5 of 6
$row = ''; // checkbox string parts (4 of 6) & (6 of 6)
$checkSubmit = '<br /><input type="submit" /></form>'; // submit button & close form
echo "<table><tr><th>Units</th></tr>";
echo "$formOpen";
echo "$scenario";
for ($j = 0 ; $j < $rows ; ++$j) // for statement that returns result row by row
{
$row = mysql_fetch_row($result);
echo "<tr>";
echo "<td>$checkPre$checkName$checkMid$row[0]$checkPost$row[1]</td>"; //brutal, but effective
echo "</tr>";
}
echo "</table>";
echo "$checkSubmit<br />"; // form closes here
if (isset($_POST['submittedCheck'])) //begin table update code
{
$scenid = $_POST['scenid']; // grab scenid
$americanunit = $_POST['Americanunit']; // grab unit array
foreach($americanunit as $unitid) // run through array
{
$sql = "INSERT INTO scenario_needunits VALUES ('$scenid' , '$unitid');"; // create query string
$result = mysql_query($sql); // add one new row
echo "Inserted a $unitid for scenario $scenid<br />"; // report addition
}
echo "Complete."; // report update has been successful
}
?>