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
Create an associative array within a loop
Old 11-05-2010, 12:44 PM Create an associative array within a loop
Average Talker

Posts: 26
Trades: 0
am trying to write a shopping cart script, that once they have confirmed their order it will insert into the database their order information.

Basically as a product can have a number of different quanitities that they wish to purchase, I want to create an associative array that has a product, linked to the quantity. I want to do this so that the order information stays in one row in the database, so in the 'product' row, it has values like (cow -> 2) cow being the product and 2 being the quantity.

Here is the code I am developing, and I have commented in the array which I am trying to create just the show my idea of thinking. any help, will be greatly appreciated!

PHP Code:
unction updateCart($product_id,$id){
    if(
$_SESSION['cart']) {
        foreach(
$_SESSION['cart'] as $product_id => $quantity) {
            
$total 0;
            
$sql sprintf("SELECT title, price, shipping FROM product WHERE pkID = %d;",$product_id);
            
$result query($sql);
            if(
numRows($result) > ){
                
$row getResult($result);
                
$price $row['price'];
                
$shipping $row['ship'];
                
$line_cost = ($price $shipping) * $quantity;
                
$total $total $line_cost;
                
$product[] = $row['title'] -> $quantity//unsure of how to code
                
$sql2 "INSERT INTO cart (userID, products, total, date) VALUES ('$id', '$product', '$total' '$date')";
                
$query query($sql2);
            }
        }
    }else{
        
$alert 'No items in shopping cart.';
    }
    return 
$total;

applebiz89 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-05-2010, 02:25 PM Re: Create an associative array within a loop
Veter's Avatar
Super Talker

Posts: 147
Trades: 0
Do you want to add one record with all ordered products in the table?
If so then your code is wrong, because you are running insertion query in the foreach cycle. So it will be added as many times as many products in the cart.

If you want multiple records then i duno why just dont add the quantity to the table field instead of using arrays.

Correct your code or be more specific.

To use arrays in tabe just use splitters like:
PHP Code:
$products="Product1=1\nProduct2=3\nProduct3=1" 
And insert that in the mysql table field. It will look like:
Product1=1
Product2=3
Product3=1

in the table.

Then you can explode it in your code later:

PHP Code:
$unclean_products explode("\n",$record["products"]);
foreach(
$unclean_products as $product){
 
$exploded_product explode('=',$product);
 
#here $exploded_product[0] will be the product name
 #and $exploded_product[1] is the quantity
 #and do w/e you want with it

__________________
-=
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
=-
Veter is offline
Reply With Quote
View Public Profile Visit Veter's homepage!
 
Old 11-05-2010, 02:50 PM Re: Create an associative array within a loop
Average Talker

Posts: 26
Trades: 0
well i just want to create an array with all the products and their quantities into just one row in the database.

so for that order in the database it would go in with the order id.

so the database row would look like:

orderID userID products price
12 12 cow 1, pig 3 10

so cow would be the product name and the number would be the quantity associated to it from the $_SESSION['cart'].

Your example is precisely how i want it, but how can you physically insert it into the array within that loop is what I am unsure of, thats why I put the comment at the line of code im unsure of as the rest of it is how I want it to function but i want to insert all the products into the database along with their associated quantities so that the admin knows how many to manage if you get me.
applebiz89 is offline
Reply With Quote
View Public Profile
 
Old 11-05-2010, 03:21 PM Re: Create an associative array within a loop
Veter's Avatar
Super Talker

Posts: 147
Trades: 0
Here it is:

PHP Code:
unction updateCart($product_id,$id){ 
    if(
$_SESSION['cart']) { 
        foreach(
$_SESSION['cart'] as $product_id => $quantity) { 
            
$total 0
            
$sql sprintf("SELECT title, price, shipping FROM product WHERE pkID = %d;",$product_id); 
            
$result query($sql); 
            if(
numRows($result) > ){ 
                
$row getResult($result); 
                
$price $row['price']; 
                
$shipping $row['ship']; 
                
$line_cost = ($price $shipping) * $quantity
                
$total $total $line_cost
                
$product[] = $row['title'].'='.$quantity//the code you want 
            

        } 
        
$products join("\n",$product); //joining array and it will be inserted to db
        
$sql2 "INSERT INTO cart (userID, products, total, date) VALUES ('$id', '$products', '$total' '$date')"
        
$query query($sql2); 
    }else{ 
        
$alert 'No items in shopping cart.'
    } 
    return 
$total

__________________
-=
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
=-
Veter is offline
Reply With Quote
View Public Profile Visit Veter's homepage!
 
Old 11-06-2010, 10:39 AM Re: Create an associative array within a loop
Average Talker

Posts: 26
Trades: 0
brilliant, thanks mate. I did think of doing it a similar way but wasnt sure whether that would be a cheat way to do it, but does exactly what I want as its just for showing the amounts to the administrator.
applebiz89 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Create an associative array within a loop
 

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