Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with arrays please
Old 06-29-2005, 10:34 AM Help with arrays please
Novice Talker

Posts: 8
Trades: 0
Hello

I'm new to all this and need some help with arrays......

I've created a wedding gift list form where users can select various gifts from checkboxes and select fields. The code below prints out which gifts have been chosen from the database. It gives the user the subtotal for each gift from the database expressed in the variable $subtotal.

The problem i have now is getting each instance of the variable $subtotal so i can give a total price at the end. As this is in a while loop when I have tried to create an array for $subtotal i only get the last value.

I need a way of getting each value of $subtotal from the while loop.

I hope this makes sense!!

Here is the code......
PHP Code:
include ('connect2.inc');
//check for array
if (is_array ($_POST['select'])) {
// check against database for each item in array
foreach ($_POST['select'] as $gift) {
    
$sql "select * from giftlist where gift_id = $gift"
$get_list_res mysql_query($sql) or die(mysql_error());

while (
$recs mysql_fetch_array($get_list_res)) {
//variables from database

$id $recs['gift_id'];
$title =$recs['title'];
$details =$recs['details'];
$quantity =$recs['quantity'];
$price=$recs['price'];
$total=$recs['total'];

// grabs amount from $id variable 
$amount $_POST[$id];
// I need each variable of $subtotal below so I can create a total price at end
$subtotal$price $amount

print 
"Gift $title Price= $amount X $price = £$subtotal<br>\n";

}    
      
}

} else { 
 print 
'Please select some gifts from the list';


I am getting close to throwing my laptop out the window so please help!

Thanks

Last edited by davedark; 06-29-2005 at 10:36 AM..
davedark is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-29-2005, 10:55 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
If all you want is a total of all items, then you don't need an array. Just do this:

$subtotal = $subtotal + ($price * $amount);

The price * amount of each item will get added to the subtotal. So when you are done, you'll have a total of all items.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 06-29-2005, 11:40 AM
Novice Talker

Posts: 8
Trades: 0
Cheers,

trying to do things the hard way as ever........ the laptop is safe for now!
davedark is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with arrays please
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16081 seconds with 12 queries