Irish SEO,  Marketing & Webmaster Discussion

 

Problem Inserting 2D array into mysql database.

This is a discussion on Problem Inserting 2D array into mysql database. within the Coding Help forums, part of the Webmaster Help category; Hi guys, I have read a few threads on this but I'm still stuck ! I have an array which ...


Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help

Register Forum Rules FAQDonate Calendar Search Today's Posts Mark Forums Read

  #1 (permalink)  
Old 20-11-2008, 04:45 PM
Frontpage User
 
Join Date: Nov 2008
Location: England
Posts: 6
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
daveharding will become famous soon enough
Send a message via MSN to daveharding Send a message via Skype™ to daveharding
Smile Problem Inserting 2D array into mysql database.

Hi guys,

I have read a few threads on this but I'm still stuck ! I have an array which is a shopping basket, and it holds details about each item, and a number of items. For example, one users basket may be..

Topside Joint
Cotswold Edge
beef
9
1

Pork
Cotswold Edge
sausages
6.00
1

which is the output as...

$name = $_SESSION['basket'][$key]['name'];
$supplier = $_SESSION['basket'][$key]['supplier'];
$category = $_SESSION['basket'][$key]['category'];
$price = $_SESSION['basket'][$key]['price'];
$qty = $_SESSION['basket'][$key]['qty'];

echo $id . "<br />";
echo $name . "<br />";
echo $supplier . "<br />";
echo $category . "<br />";
echo $price . "<br />";
echo $qty . "<br />";

I KNOW this is a bad method (and it doesn't even work!) but this is how my code currently stands:


foreach($_SESSION['basket'] as $key => $another)
{
$name = $_SESSION['basket'][$key]['name'];
$supplier = $_SESSION['basket'][$key]['supplier'];
$category = $_SESSION['basket'][$key]['category'];
$price = $_SESSION['basket'][$key]['price'];
$qty = $_SESSION['basket'][$key]['qty'];


$sql = "INSERT INTO orders VALUES(0,'".$id."','".$oid."','".$name."','".$pric e."','".$supplier."','".$category."')"
$result = mysql_query($sql, $connection)
or die("MySQL Error: ".mysql_error());
}

How can I do this in a way thats a) better/more efficient and b) just works?

Thanks in advance

Dave
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 20-11-2008, 04:46 PM
Frontpage User
 
Join Date: Nov 2008
Location: England
Posts: 6
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
daveharding will become famous soon enough
Send a message via MSN to daveharding Send a message via Skype™ to daveharding
Default

p.s. not sure why, but in the above view $price has a space in it, this does not appear in my code. Cheers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 20-11-2008, 10:45 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,204
Nominated 6 Times in 4 Posts
Nominated TOTW/F/M Award(s): 1
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

can you print the array or the sql statement here?

print_r($_SESSION);

//or

echo $sql;
__________________
:. Web Design & Development Web Design Ireland
:. Search Engines Optimization Search Engines Optimization
:. Car Parts & Accessories Car Parts
:. Cars Ireland Cars Ireland
:. I Have 2 Find It Directory SEF Directory
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 21-11-2008, 10:37 AM
Frontpage User
 
Join Date: Nov 2008
Location: England
Posts: 6
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
daveharding will become famous soon enough
Send a message via MSN to daveharding Send a message via Skype™ to daveharding
Default

Thanks of your reply louie,

this is the print of $_SESSION['basket'] with 2 items in it.

Array ( [0] => Array ( [id] => 1 [name] => Pork [price] => 6.00 [supplier] => Cotswold Edge [category] => sausages [qty] => 1 ) [1] => Array ( [id] => 2 [name] => Pork [price] => 6.00 [supplier] => Cotswold Edge [category] => sausages [qty] => 1 ) )

hopefully you can make sense of it!

Dave
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 21-11-2008, 10:52 AM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,204
Nominated 6 Times in 4 Posts
Nominated TOTW/F/M Award(s): 1
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

You have a 3D array so you will need another foreach loop inside the "foreach"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 21-11-2008, 01:55 PM
Frontpage User
 
Join Date: Nov 2008
Location: England
Posts: 6
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
daveharding will become famous soon enough
Send a message via MSN to daveharding Send a message via Skype™ to daveharding
Default

when would I run the sql statement?

Could you give me a rough idea of what the code would look like?

Cheers!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 21-11-2008, 02:10 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,204
Nominated 6 Times in 4 Posts
Nominated TOTW/F/M Award(s): 1
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

what exactly does the $key holds as value?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 21-11-2008, 02:16 PM
Frontpage User
 
Join Date: Nov 2008
Location: England
Posts: 6
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
daveharding will become famous soon enough
Send a message via MSN to daveharding Send a message via Skype™ to daveharding
Default

I'm not sure, sorry a bit new to this. this is my addToBasket.php file which adds items the customer wants, into my array.
<? //removed some irrelevant stuff
$basketArray = array();
$basketArray['id'] = $_GET['id'];
$basketArray['name'] = $_GET['name'];
$basketArray['price'] = $_GET['price'];
$basketArray['supplier'] = $_GET['supplier'];
$basketArray['category'] = $_GET['category'];
$basketArray['qty'] = 1;
$price = $_GET['price'];
$subtotal = $_SESSION['subtotal'];

$found = false;

if(isset($_SESSION['basket']))
{
foreach($_SESSION['basket'] as $key => $another)
{
if($_SESSION['basket'][$key]['id'] == $_GET['id'])
{
$found = true;
break;
}
}
}

if($found == false)
{
$subtotal = ($subtotal + $price);
$_SESSION['subtotal'] = $subtotal;
$_SESSION['basket'] [] = $basketArray;
}
header("Location: foodItems.php?category=".$category."" );
exit();
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9 (permalink)  
Old 21-11-2008, 02:53 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,204
Nominated 6 Times in 4 Posts
Nominated TOTW/F/M Award(s): 1
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

Something to get you rolling:

$sql = "";
$oid = "someid"; //don't know what that is so I gave it a value
$id = !empty($_GET['id']) ? preg_replace("#[^0-9]#","",$_GET['id']) : "111";
$name = !empty($_GET['name']) ? preg_replace("#[^0-9a-zA-Z\- ]#","",$_GET['name']) : "name";
$price = !empty($_GET['price']) ? preg_replace("#[^0-9]#","",$_GET['price']) : "999";
$supplier = !empty($_GET['supplier']) ? preg_replace("#[^0-9a-zA-Z\- ]#","",$_GET['supplier']) : "SUPPLIER";
$category = !empty($_GET['category']) ? preg_replace("#[^0-9a-zA-Z\- ]#","",$_GET['category']) : "CATEGORY";

$basketArray = array();
$basketArray['id'] = $id;
$basketArray['name'] = $name;
$basketArray['price'] = $price;
$basketArray['supplier'] = $supplier;
$basketArray['category'] = $category;
$basketArray['qty'] = 1;
//$price = $_GET['price'];
$subtotal = !empty($_SESSION['subtotal']) ? $_SESSION['subtotal'] : "0";
$found = false;
//print_r($basketArray);
if(!empty($_SESSION['basket'])) {
foreach($_SESSION['basket'] as $key) {
foreach($key as $x=>$y){
echo $x."-".$y."<br />";
if($y == $id) {
$found = true;
echo "<h1>found</h1>";
break;
}
}
}
}
if(!$found) {
$subtotal = ($subtotal + $price);
$_SESSION['subtotal'] = $subtotal;
$_SESSION['basket'][] = $basketArray;
//insert data to DB
$sql = "INSERT INTO orders VALUES(0,'".$id."','".$oid."','".$name."','".$pric e."','".$supplier."','".$category."')";
}else{
//update quantity
$sql = "UPDATE orders.....";
}
//header("Location: foodItems.php?category=".$category."" );
//exit();
//print_r($_SESSION['basket']);

if($sql != ""){
echo "<h1>".$sql."</h1>";
//$result = mysql_query($sql, $connection)
//or die("MySQL Error: ".mysql_error());
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10 (permalink)  
Old 22-11-2008, 03:22 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,204
Nominated 6 Times in 4 Posts
Nominated TOTW/F/M Award(s): 1
louie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud oflouie has much to be proud of
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

so, was the above code any help to you?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
2d array, array, arrays, database, inserting, mysql, php, problem, sql

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Periodically inserting data in to a database jason Coding Help 4 24-09-2008 09:25 PM
Checkbox inserting in mysql Joseph Grogan Coding Help 1 10-01-2008 10:40 AM
Ajax and mysql and PHP, pulling info from a database Ciarán Mc Cann Coding Help 10 09-08-2007 12:26 PM
Inserting PHP multidimensional arrays into a mySQL database Ciarán Mc Cann Coding Help 15 03-08-2007 06:15 PM
Connecting to 2 Mysql database in the same script?? Ciarán Mc Cann Coding Help 11 24-07-2007 10:21 AM


Sponsored links

Pepperjam Network
Paid On Results www.zanox.com


All times are GMT +1. The time now is 06:36 AM.


Powered by: vBulletin Version 3.8.2, Copyright ©2000 - 2009, Jelsoft Enterprises Limited.
Hosted in Ireland by Blacknight - Test your ISP |Irish Hosting Directory| Armchair.ie|Logo by Eden Web Design|Avatars by Afterglow |Latest Blog Entries | VPS HostingAd Management by RedTyger

Search Engine Friendly URLs by vBSEO 3.3.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51