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
More help with arrays
Old 06-29-2005, 06:14 PM More help with arrays
Novice Talker

Posts: 8
Trades: 0
Hello

I'm new to all of this and still having problems with this wedding giftlist I'm creating. I need to pass the variables from the select array and also the relevant quantity so that this info can be stored in a database.

This: print_r ($_POST['select']); prints the correct array contents however
print_r ($amounted); only gives me the last variable in the while loop.

I need it so I can access all of the variables related to each other i.e
giftid and quantity together.

Hope this makes sense, not sure of the right way to progress with this script

thanks......


PHP Code:
include ('connect2.inc');
print 
"<p>&nbsp;</p><table border='0' cellpadding='1' cellspacing='0' width='90%'>
<tr id='highlight3'><td>Gift</td><td>Quantity</td><td>&nbsp;</td><td>Price</td><td>Subtotal</td></tr>"
;
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];
$amounted = array($amount);
$ntotal $price $amount;
$ntotal number_format ($ntotal,2);
$subtotal $subtotal + ($price $amount);
$subtotal =number_format ($subtotal,2); 
print 
"
<tr id=''><td>
$title</td><td>$amount</td><td>x</td><td>£$price</td>
<td>£
$ntotal</td></tr>";


}    

 
}
print_r ($_POST['select']);
print_r ($amounted); 
// end table 
print "
<tr><td colspan='5'>&nbsp;</td></tr>
<tr id='highlight3'><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<td><strong>Total</strong></td>
<td><strong>£
$subtotal</strong></td></tr></table>"
davedark is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-29-2005, 08:21 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
don't use short vars in the while loop when referencing record row names. use the long version i.e. $row_result['columnName'];.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 06-30-2005, 04:03 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
He's already used the long form to store into variables so that's not the trouble.

davedark, you are overwriting your array every time around the loop, hence you only ever see the last update since you've stomped on all the past ones. I'm nots ure what you want in your subtotal variables etc, but try this:

PHP Code:
 $amounted = array();
 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];
 
$amounted[] = $amount//This adds one item to the end of your array instead of wiping it.
 
$ntotal $price $amount;
 
$ntotal number_format ($ntotal,2);
 
$subtotal $subtotal + ($price $amount);
 
$subtotal =number_format ($subtotal,2);
 print 
"
 <tr id=''><td>
$title</td><td>$amount</td><td>x</td><td>£$price</td>
 <td>£
$ntotal</td></tr>";
 
 
 }    
 
 
 }
 
print_r ($_POST['select']);
 
print_r ($amounted); 
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-30-2005, 06:03 AM
Novice Talker

Posts: 8
Trades: 0
Thanks Oberon, unfortunately that doesn't work. Still seem to be getting just one variable from the loop. The subtotal is not a problem, that part of the script can be ignored.

I just need to get the amount and the gift or item selected to 'pair up' so they can be put in the database, I thought the best way of doing this would be an indexed array......maybe I'm wrong.

Got any other ideas?

Thanks
davedark is offline
Reply With Quote
View Public Profile
 
Old 06-30-2005, 06:51 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
If you're wanting to put things into a database, why not just put them in directly instead of going via an array?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-30-2005, 06:56 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
If you are putting things into the database, why not just do it directly inside the loop? No need to bother with an array since you'd only have to loop over the array to do the insertions later.


PHP Code:
 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'];
 
 
mysql_query("INSERT INTO some_table(id,amount,details,whatever) VALUES($id,'$amount','$details',$whatever_else_you_want)");
 
 }    
 
 
 } 
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 06-30-2005, 06:58 AM
Novice Talker

Posts: 8
Trades: 0
Have a look, you'll need to log in as firstname - sarah lastname - shaw.

http://www.pennyandrik.co.uk/giftlist.php

i used an array for the checkboxes, select[]. The while loop loops through and gets the amount for each one.

I really am lost in my own coding!!!!!!!!

Cheers
davedark is offline
Reply With Quote
View Public Profile
 
Old 06-30-2005, 07:20 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
dave, I last month I did a purchasing cycle for a client's shopping cart which almost had the same logic as what your trying to do. I came across the same problems your having. I went about solving those problems a lot differently though. I can tell you now though, one problem I had was referencing column names in result sets by short vars inside a while loop. This problem evapourated when I stuck to long vars for all resultset values within the loop.

My style is very different to yours, but the code below has adapted my approach to your form logic (as best as I could figure it out).

PHP Code:

<?php
include ('connect2.inc');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>webmaster forum help</title>
</head>
<body>
<p>Gift Cart Checkout</p>
<table border='0' cellpadding='1' cellspacing='0' width='90%'>
<tr id='highlight3'><td>Gift</td><td>Quantity</td><td>&nbsp;</td><td>Price</td><td>Subtotal</td></tr>"
<?php
if(isset($_POST['select'])){
// recordset by gift id
    
$giftID $_POST['select'];
    foreach(
$giftID as $index => $gID);
    
$iDstring=implode(','$giftID);
    
$sql 'SELECT * FROM table_individual_tickets WHERE gift_ID IN ('.$iDstring.')';
    
$rsGiftPurchase mysql_query($sql$dbConn) or die(mysql_error());
    
$row_rsGiftPurchase mysql_fetch_assoc($rsGiftPurchase);
    
$totalRows_rsGiftPurchase mysql_num_rows($rsGiftPurchase);
// sum of totals in recordset
    
$sql2 'SELECT SUM(total) FROM giftlist WHERE gift_id = ('.$iDstring.')';
    
$grandTotal mysql_query($sql2$dbConn) or die(mysql_error());
?>
<?php
do {
?>
<tr>
    <td><?php echo $row_rsGiftPurchase['title']; ?></td>
    <td><?php echo $row_rsGiftPurchase['amount']; ?></td>
    <td>&nbsp;</td>
    <td><strong>&pound;</strong><?php echo $row_rsGiftPurchase['price']; ?></td>
    <td><strong>&pound;</strong><?php echo $row_rsGiftPurchase['total']; ?></td>
</tr>
<?php 

while ($row_rsGiftPurchase mysql_fetch_assoc($rsGiftPurchase));
 
?>
<tr>
    <td colspan='5'>&nbsp;</td>
</tr>
<tr id='highlight3'>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><strong>Total</strong></td>
    <td><strong>&pound;<?php echo $grandTotal?></strong></td>
</tr>
</table>
<?php
// end generate table if isset($_POST['select'])
}
?>
</body>
</html>
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 07-01-2005, 08:20 AM
Novice Talker

Posts: 8
Trades: 0
Thanks Metho,

Its good to see another way around your code. I've tried a few of your ideas and they work so far, think I've pretty much sorted it out.

Cheers
davedark is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to More help with arrays
 

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.37321 seconds with 12 queries