Denethor Posted November 19, 2008 Share Posted November 19, 2008 Hi all I've been teaching myself PHP for a few weeks and am currently working on a database driven website for a friend of mine. Basically it grabs the info out of a database and outputs it like so: Each order box is a form but at the moment when it calculates the cost based on the options selected it prints the price in all the product boxes. This doesn't come as much of a surprise as each form element etc is being generated with the same name. I know the solution lies somewhere in using hidden fields to make each form treated as a seperate calculation... but I'm not sure where to begin so I'm hoping someone could give me some help, or point me in the right direction. Here's my code so far, I've commented some of the elements etc: <?PHP // login info $user_name = "root"; $password = ""; $database = "lemboss"; $server = "localhost"; // Connect To Database $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); // connection test script /* if ($db_found) { print "Database Found "; mysql_close($db_handle); } else { print "Database NOT Found" . $db_handle; } print "Connection to the Server opened"; */ $size = $_POST['size']; $material = $_POST['material']; $quantity = $_POST['quantity']; $total = $size * $material * $quantity; $price = number_format($total, 2); $result = mysql_query("SELECT * FROM health_and_safety"); $columns = 2; $num_rows = mysql_num_rows($result); //Start Content echo "<table width=\"710\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"; //Table Loop for($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); if ($i % $columns ==0) { echo "<tr>"; } echo "<td align=\"center\">"; //Start Order Box echo "<form method=\"post\" action=\"health.php\" name=\"" . $row['id'] . "\">"; echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#666666\">"; echo "<tr align=\"center\" valign=\"middle\">"; echo "<td width=\"10\" height=\"270\" rowspan=\"16\"> </td>"; echo "<td width=\"150\" height=\"20\" colspan=\"2\"><div align=\"center\" class=\"tablehead\">" . $row['id'] . "</div></td>"; echo "<td width=\"30\" height=\"270\" rowspan=\"16\"> </td>"; echo "<td width=\"150\" height=\"20\" colspan=\"3\"><div align=\"center\" class=\"tablehead\">MATERIALS</div></td>"; echo "<td width=\"10\" height=\"270\" rowspan=\"16\"> </td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"150\" colspan=\"2\" rowspan=\"10\" align=\"center\"><a href=\"img/signs/" . $row['picture'] . ".jpg\" target=\"_blank\"><img src=\"img/signs/thumbs/" . $row['picture'] . "_thumb.jpg\" width=\"150\" height=\"150\"></a></td>"; echo "<td width=\"130\" height=\"20\" colspan=\"2\" class=\"tabletext\">" . $row['material_1'] . "</td>"; //Radio Button 1 echo "<td width=\"20\" height=\"20\"><input name=\"material\" type=\"radio\" value=\"" . $row['vinyl'] . "\" checked></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"10\" colspan=\"3\"><img src=\"img/spacer.gif\" width=\"1\" height=\"1\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"130\" height=\"20\" colspan=\"2\" class=\"tabletext\">" . $row['material_2'] . "</td>"; echo "<td width=\"20\" height=\"20\">"; //Radio Button 2 echo "<input type=\"radio\" name=\"material\" value=\"" . $row['3mm_foam'] . "\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"10\" colspan=\"3\"><img src=\"img/spacer.gif\" width=\"1\" height=\"1\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"130\" height=\"20\" colspan=\"2\" class=\"tabletext\">" . $row['material_3'] . "</td>"; //Radio Button 3 echo "<td width=\"20\" height=\"20\"><input type=\"radio\" name=\"material\" value=\"" . $row['5mm_foam'] . "\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"10\" colspan=\"3\"><img src=\"img/spacer.gif\" width=\"1\" height=\"1\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"130\" height=\"20\" colspan=\"2\" class=\"tabletext\">" . $row['material_4'] . "</td>"; //Radio Button 4 echo "<td width=\"20\" height=\"20\"><input type=\"radio\" name=\"material\" value=\"" . $row['3mm_alu_panel'] . "\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"10\" colspan=\"3\"><img src=\"img/spacer.gif\" width=\"1\" height=\"1\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"130\" height=\"20\" colspan=\"2\" class=\"tabletext\">" . $row['material_5'] . "</td>"; //Radio Button 5 echo "<td width=\"20\" height=\"20\"><input type=\"radio\" name=\"material\" value=\"" . $row['3mm_solid_alu'] . "\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"10\" colspan=\"3\"><img src=\"img/spacer.gif\" width=\"1\" height=\"1\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"20\" colspan=\"2\"><div align=\"center\" class=\"tablehead\">AVAILABLE SIZES</div></td>"; echo "<td width=\"130\" height=\"20\" colspan=\"2\" class=\"tabletext\">" . $row['material_6'] . "</td>"; //Radio Button 6 echo "<td width=\"20\" height=\"20\"><input type=\"radio\" name=\"material\" value=\"" . $row['pvc_banner'] . "\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"130\" height=\"20\"><div align=\"left\" class=\"tabletext\">" . $row['size_1_dim'] . "</div></td>"; //Radio Button A echo "<td width=\"20\" height=\"20\"><input name=\"size\" type=\"radio\" value=\"" . $row['size_1'] . "\" checked></td>"; echo "<td width=\"75\" height=\"40\" rowspan=\"2\"><div align=\"left\" class=\"tablehead\">QUANTITY</div></td>"; //Quantity text field echo "<td width=\"75\" height=\"40\" colspan=\"2\" rowspan=\"2\" align=\"center\"><input name=\"quantity\" type=\"text\" id=\"quantity\" value=\"0\" size=\"5\" maxlength=\"10\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"130\" height=\"20\"><div align=\"left\" class=\"tabletext\">" . $row['size_2_dim'] . "</div></td>"; //Radio Button B echo "<td width=\"20\" height=\"20\"><input type=\"radio\" name=\"size\" value=\"" . $row['size_2'] . "\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"130\" height=\"20\"><div align=\"left\" class=\"tabletext\">" . $row['size_3_dim'] . "</div></td>"; //Radio Button C echo "<td width=\"20\" height=\"20\"><input type=\"radio\" name=\"size\" value=\"" . $row['size_3'] . "\"></td>"; echo "<td width=\"75\" height=\"20\" bgcolor=\"#000000\"><div align=\"center\" class=\"tabletext\">£"; //Print out of the total cost echo $price; echo "</div></td>"; echo "<td width=\"75\" height=\"20\" colspan=\"2\"><input type=\"image\" name=\"submit\" src=\"img/add_button.gif\" width=\"75\" height=\"20\" value=\"send\"></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"150\" height=\"20\" colspan=\"2\"> </td>"; echo "<td width=\"150\" height=\"20\" colspan=\"3\"> </td>"; echo "</tr>"; echo "<tr>"; echo "<td width=\"10\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"10\" height=\"1\"></td>"; echo "<td width=\"130\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"130\" height=\"1\"></td>"; echo "<td width=\"20\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"20\" height=\"1\"></td>"; echo "<td width=\"30\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"30\" height=\"1\"></td>"; echo "<td width=\"75\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"75\" height=\"1\"></td>"; echo "<td width=\"55\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"55\" height=\"1\"></td>"; echo "<td width=\"20\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"20\" height=\"1\"></td>"; echo "<td width=\"10\" height=\"1\" valign=\"top\"><img src=\"transparent.gif\" alt=\"\" width=\"10\" height=\"1\"></td>"; echo "</tr>"; echo "</table>"; echo "</form>"; //End Order Box if (($i % $columns) == (columns - 1) || ($i + 1) == $num_rows) { echo "</tr>"; } } echo "</table>"; ?> I know this is probably a stupid question, but like I say I'm new so forgive me if I mess up. Also, is there a way for the form to update so that the user sees the total price without having to click anything? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/133327-stuck-up-form-creek-without-a-paddle/ Share on other sites More sharing options...
nevesgodnroc Posted November 19, 2008 Share Posted November 19, 2008 if your wanting the page do add up your total without a calculate total button you can write a javascript calculation to cat on almost any action including time if you wish. look into some javascript tutorials for how to get the information from your textfields or drop down menus. and how to set the value of a text field with javascript. and then you can get it to fire that function say on a onFocus event for every textfield or every so many seconds your code will need to have something like getElementbyID("field1") in order to make each text field act like a variable. but yes it can be done. it may be more work or be less efficient then just haveing them click a calculate new total button. Link to comment https://forums.phpfreaks.com/topic/133327-stuck-up-form-creek-without-a-paddle/#findComment-693481 Share on other sites More sharing options...
Denethor Posted November 21, 2008 Author Share Posted November 21, 2008 if your wanting the page do add up your total without a calculate total button you can write a javascript calculation to cat on almost any action including time if you wish. look into some javascript tutorials for how to get the information from your textfields or drop down menus. and how to set the value of a text field with javascript. and then you can get it to fire that function say on a onFocus event for every textfield or every so many seconds your code will need to have something like getElementbyID("field1") in order to make each text field act like a variable. but yes it can be done. it may be more work or be less efficient then just haveing them click a calculate new total button. Thanks for the reply, I'll go through some beginner stuff here then;http://homepage.ntlworld.com/kayseycarvey/ But maybe I'll just go with a calculate/get total button if it's too much unnecessary work. I'm more worried about fixing the other problem at the minute so I'll keep the calc button for now either way. Link to comment https://forums.phpfreaks.com/topic/133327-stuck-up-form-creek-without-a-paddle/#findComment-695254 Share on other sites More sharing options...
Denethor Posted November 24, 2008 Author Share Posted November 24, 2008 Sorry, I've gotta bump this because I'm still stuck... Link to comment https://forums.phpfreaks.com/topic/133327-stuck-up-form-creek-without-a-paddle/#findComment-697529 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.