Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recalculate Quantities?

Status
Not open for further replies.

bluedollar

Programmer
Joined
Jul 24, 2003
Messages
174
Location
GB
I have some code that allows users to view their shopping cart (which can contain social event tickets, conference tickets and misc items to do with a conference), as shown below:

=======================================================

function displaychk() {

echo'<form action=&quot;checkoutscript.php&quot; METHOD=POST>';
echo'<table border=&quot;0&quot;>';
echo'<tr bgcolor=&quot;#999999&quot;><th colspan=&quot;3&quot; align=&quot;left&quot;>Check Out</th></tr>';
echo'<tr><td><br></td></tr>';
echo'<tr><th width=&quot;50%&quot; bgcolor=&quot;#999999&quot; align=&quot;left&quot;>Description</th><th bgcolor=&quot;#999999&quot; align=&quot;left&quot;>Qty</th><th bgcolor=&quot;#999999&quot; align=&quot;left&quot;>Price</th></tr>';
if ($_SESSION['accep'] != &quot;&quot;) {
$aid = $_SESSION['accep'];
$SQL = &quot;select description from prices where prices_id = '$aid'&quot;;
$description = $this->obconnect->selectcol($SQL,&quot;description&quot;);
$SQL = &quot;select cost from prices where prices_id = '$aid'&quot;;
$itemcost = $this->obconnect->selectcol($SQL,&quot;cost&quot;);
echo'<tr><td>Ticket - '.$description.'</td><td>1</td><td>£'.$itemcost.'</td></tr>';
}
if ($_SESSION['social'] != &quot;&quot;) {
$sid = $_SESSION['social'];
$SQL = &quot;select name from social where social_id = '$sid'&quot;;
$description = $this->obconnect->selectcol($SQL,&quot;name&quot;);
$SQL = &quot;select price from social where social_id = '$sid'&quot;;
$itemcost = $this->obconnect->selectcol($SQL,&quot;price&quot;);
if ($_SESSION['social_qty'] != &quot;&quot;) { $sqty = $_SESSION['social_qty']; }
else { $sqty = 1;}
echo'<tr><td>Social Event - '.$description.'</td><td><input type=&quot;text&quot; size=&quot;3&quot; name=&quot;socqty&quot; value=&quot;'.$sqty.'&quot;></td>';
$totalcost = $itemcost * $sqty;
echo'<td>£'.$totalcost.'</td></tr>';
}
if ($_SESSION['proc'] != &quot;&quot;) {
$sid = $_SESSION['proc'];
$esid = explode(&quot; &quot;, $sid);
if ($_SESSION['proc_qty'] != &quot;&quot;) {
$epqty = explode (&quot; &quot;, $_SESSION['social_qty'] );
}
$counter = 0;
foreach ($esid as $val) {
$SQL = &quot;select name from proceedings where proc_id = '$val'&quot;;
$name = $this->obconnect->selectcol($SQL,&quot;name&quot;);
$SQL = &quot;select price from proceedings where proc_id = '$val'&quot;;
$itemcost = $this->obconnect->selectcol($SQL,&quot;price&quot;);
if ($_SESSION['proc_qty'] != &quot;&quot;) {
$totalcost = $itemcost * $eqty[$counter];
}
else { $totalcost = $itemcost * 1; }
$qname = 'proc';
$qname .= $counter;
echo'<tr><td>Proceeding - '.$name.'</td><td><input type=&quot;text&quot; size=&quot;3&quot; name=&quot;'.$qname.'&quot; value=&quot;1&quot;></td><td>£'.$totalcost.'</td></tr>';
$counter++;
}
}
echo'<tr><td><input type=&quot;submit&quot; name=&quot;recalc&quot; value=&quot;Recalculate Prices&quot;></td><td><input type=&quot;submit&quot; name=&quot;purchase&quot; value=&quot;Purchase&quot;></td></tr>';
echo'</table>';
echo'</form>';

}



==========================================================

On the form there is a recalculate quantities button that calls a script which saves the quantities from the form quantity text boxes into session variables, when the function above is then reloaded the corresponding prices are calculated.

This works fine for social event tickets and coference tickets, however because misc conference items may consist of > 1 item I do not know how to obtain the value of the quantity text boxes on the form, they are named:

proc0
proc1
proc2
procn

however do not know how to obtain these variables from the script that is called from the form.

QUESTION

1) Does anyone have any ideas about solving the above problem?

2) And ways in which the code could generally be improved.

Any help would be greatly appreciated.

Thanks

Dan
 
The proc1...procn problem? Change the name of your form elements.

Don't name them &quot;proc0&quot;, &quot;proc1&quot;, etc. Name them &quot;proc[0]&quot;, &quot;proc[1]&quot;, etc. Then $_POST['proc'] will itself be an array.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top