/    Sign up×
Community /Pin to ProfileBookmark

Calculating in php?

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>
[/CODE]

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.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@speghettiCodeApr 04.2012 — There are 2 sides to this:

1. The user interface

2. The database/back-end

PHP is a back-end/server-side language. In simple terms, it means that the PHP code is run at the server and the server sends the rendered XHTML code to the user's web browser where it is displayed (I'm sure the techies can pull that apart...but, for this example, that's the general idea).

So...

Option 1) If you would like to limit the user's selection in the form 'live' (as in you don't want for the user to submit the form), you'll need to look at Javascript -- a client-side language that can perform calculations while the data is still on the user's screen.

Option 2) If you don't mind waiting for the user to submit the form before calculating the values, you can do your calcuations in PHP (using arrays is probably easiest). Something like the following (incomplete code):

[CODE]
$toys=array(
[0]=>array('name'=>'Toy 0', 'value'=>12),
[1]=>array('name'=>'Toy 1', 'value'=>34),
[2]=>array('name'=>'Toy 2', 'value'=>56),
[3]=>array('name'=>'Toy 3', 'value'=>78)
);
$totalValue=0;
foreach($toys as $k=>$toy){
$totalValue+=($toy['value']*$formData[('toy'.$k)]['qty']);
}
echo ($totalValue>100)?"Over Budget":"Within Budget";
[/CODE]


It's the same sort of algorithm for the Javascript. But this is the forum for PHP. so... yeah.

Personally, I'd use javascript because it saves the user from going through all the work of completing and submitting the form only to find out they're over budget.

Of course, there's Option 3 -- a combo of the two previous options. In this case, you could use AJAX to send to PHP and when the server processes the calculation, it could return the appropriate value. But for something as simple as what you're looking for is probably best handled in Javascript.

If you need more help on the PHP side, let us know. Otherwise, you may have better luck bumping this to the Javascript side.
Copy linkTweet thisAlerts:
@BoobooauthorApr 05.2012 — Thanks mate, I'll give that a shot tonight. I still have a lot to learn at the moment in php, so I don't think I've really played with arrays too much. I'll get back to you later tonight.
Copy linkTweet thisAlerts:
@BoobooauthorApr 05.2012 — BooBoos back!

Thanks for the code first of all. I'm continuing to have a problem. I think this stems from the fact that I do not understand the code, and therefore do not exactly know where to place it into the page...

Can you give me a quick overview of what you've wrote please, thanks speghettiCode
×

Success!

Help @Booboo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...