Need some help with this one guys… so I’m going to give you a scenario similar to what I’m doing first of all.
Lets say you have $100 to spend on 4 toys. You have a list of toys plus their individual prices. What this means is I have a table within my database named toys, where it holds two fields name and value.
What I’m requiring is a form/option layout where you can select 4 toys, which display the value for each total, but the total can not exceed $100. What I have at the moment is a website page that extracts the data, and places it into a form, with a drop down list so you can select the toy you want and it combines both name and value into one list on the drop down.
I need some assistance to get this to calculate, and produce a final outcome that I can then publish into a database…
[CODE] <?php
if (array_key_exists(‘insert’, $_POST)) {
include(‘conn_mysql.inc’);
include(‘corefuncs.php’);
nukeMagicQuotes();
$expected = array(‘login’, ‘toy_Choice1’, ‘toy_Choice2’, ‘toy_Choice3’, ‘toy_Choice4’);
$conn = dbConnect(‘admin’);
foreach ($_POST as $key => $value) {
if (in_array($key, $expected)) {
${$key} = mysql_real_escape_string($value);
}
}
$sql = “INSERT INTO preferences (login, toy_Choice1, toy_Choice2, toy_Choice3, toy_Choice4)
VALUES(‘$login’, ‘$toy_Choice1’, ‘$toy_Choice2’, ‘$toy_Choice1’, ‘$toy_Choice2’)”;
$result = mysql_query($sql) or die(mysql_error());
if ($result) {
header(‘Location: member-index.php’);
exit;
}
}
?>
<?php
include(‘conn_mysql.inc’);
include(‘corefuncs.php’);
$conn = dbConnect(‘admin’);
$sql = “SELECT * FROM toys”;
$result2 = mysql_query($sql) or die(mysql_error());
$result3 = mysql_query($sql) or die(mysql_error());
?>
<table bordercolor=”#000000″ border=”0″>
<tr align=”left”>
<td colspan=”2″><h3>Insert Race Results</h3></td>
</tr>
<tr align=”left”>
<form id=”form1″ name=”form1″ method=”post” action=””>
</tr>
<tr align=”left”>
<td><label for=”login”>login: </label></td>
<td><input name=”login” type=”text” class=”widebox” id=”login” /></td>
</tr>
<tr align=”left”>
<td><label for=”toy_Choice1″>toyChoice No 1: </label></td>
<td>
<select name=”toy_Choice1″>
<?php
while($row = mysql_fetch_assoc($result2)) {
?>
<option value=”
<?php echo $row[‘toy_Name’] . ‘ ‘ . $row[‘toy_Value’]; ?>”>
<?php echo $row[‘toy_Name’] . ‘ ‘ . $row[‘toy_Value’]; ?>
</option>
<?php } ?>
</select>
</td>
</tr>
<tr align=”left”>
<td><label for=”toy_Choice2″>toyChoice No 2: </label></td>
<td>
<select name=”toy_Choice2″>
<?php
While($row = mysql_fetch_assoc($result3)) {
?>
<option value=”
<?php echo $row[‘toy_Name’] . ‘ ‘ . $row[‘toy_Value’]; ?>”>
<?php echo $row[‘toy_Name’] . ‘ ‘ . $row[‘toy_Value’]; ?>
</option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td><br /><input type=”submit” name=”insert” value=”Insert Item” /></form></td>
<td><form action=”choicemade.php”>
<br /><input type=”submit” value=”Cancel”/>
</form>
</td>
</tr>
</table>
As you can see from the code above, I’ve only done 2 out of the 4 so far, for options to drop down, but I don’t wish to move further forward until I’ve got something that can calculate the total value and make sure it is under 100.