473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting Array data into a variable

Hi,

I've spent most of today trying to solve this problem, but sadly no
luck.
I have an shopping basket based on an array which stores the name,
product id, quantity and price for products, i want to take the
Information in the array and output the Quantity and Product ID
information into variables so that i can pass them into a Select
Statement and submit the order to the database.

The Current Code to Show the cart is listed below: -

function show_cart() {
if (isset($_SESSION["item_count"]) && $_SESSION["item_count"] > 0) {
echo "<table class=\"basket\"border=\"0\">";
echo
"<tr><td>Item</td><td>Price</td><td>Amount</td><td>Subtotal</td></td></tr>";

$total = 0;
for($i=0; $i<$_SESSION["item_count"]; $i++) {

echo "<td><a
href=insertselfpagehere.php?deleteitem=1&ProdID=". $_SESSION["items"][$i][0].">";

$item=$_SESSION["items"][$i][0];
echo $_SESSION["items"][$i][1]."</a></td>";
echo "<td>".$_SESSION["items"][$i][2]."</td>";
echo "<td>*".$_SESSION["items"][$i][3]."</td>";
$subtotal = $_SESSION["items"][$i][2] * $_SESSION["items"][$i][3];
echo "<td align=\"right\">$subtotal</td>";
echo "</tr>";
$total += $subtotal;
}
echo "<tr><td><b>total</b></td><td align=\"right\"
colspan=\"3\">$total</td></tr></table>";
}

else {
echo "No items";
}
}

Any help would be much appreciated, i seem to have a mental block with
PHP at the moment.

Kind Regards,

Ian

Jul 17 '05 #1
4 1786
iannor...@gmail.com wrote (in part):
Hi,

I've spent most of today trying to solve this problem, but sadly no
luck.
I have an shopping basket based on an array which stores the name,
product id, quantity and price for products, i want to take the
Information in the array and output the Quantity and Product ID
information into variables so that i can pass them into a Select
Statement and submit the order to the database.

The Current Code to Show the cart is listed below: -

function show_cart() {
if (isset($_SESSION["item_count"]) && $_SESSION["item_count"] > 0) {
echo "<table class=\"basket\"border=\"0\">";
echo
"<tr><td>Item</td><td>Price</td><td>Amount</td><td>Subtotal</td></td></tr>";
$total = 0;
for($i=0; $i<$_SESSION["item_count"]; $i++) {

echo "<td><a
href=insertselfpagehere.php?deleteitem=1&ProdID=". $_SESSION["items"][$i][0].">";
$item=$_SESSION["items"][$i][0];
echo $_SESSION["items"][$i][1]."</a></td>";
echo "<td>".$_SESSION["items"][$i][2]."</td>";
echo "<td>*".$_SESSION["items"][$i][3]."</td>";
$subtotal = $_SESSION["items"][$i][2] * $_SESSION["items"][$i][3];
echo "<td align=\"right\">$subtotal</td>";
echo "</tr>";
$total += $subtotal;
}
echo "<tr><td><b>total</b></td><td align=\"right\"
colspan=\"3\">$total</td></tr></table>";
}

else {
echo "No items";
}
}


First let's start by cleaning up your code a little bit. You can
eliminate all of your escaped quotes by using single quotes:

function show_cart() {
if (isset($_SESSION['item_count']) && $_SESSION['item_count'] > 0) {
echo '<table class="basket" border="0">';
echo
'<tr><td>Item</td><td>Price</td><td>Amount</td><td>Subtotal</td></td></tr>';

$total = 0;
for($i=0; $i<$_SESSION['item_count']; $i++) {

echo '<td><a
href=insertselfpagehere.php?deleteitem=1&ProdID='. $_SESSION['items'][$i][0].'>';

$item=$_SESSION['items'][$i][0];
echo $_SESSION['items'][$i][1].'</a></td>';
echo '<td>'.$_SESSION['items'][$i][2].'</td>';
echo '<td>*'.$_SESSION['items'][$i][3].'</td>';
$subtotal = $_SESSION['items'][$i][2] * $_SESSION['items'][$i][3];
echo '<td align="right">' . $subtotal . '</td>';
echo '</tr>';
$total += $subtotal;
}

echo '<tr><td><b>total</b></td><td align="right" colspan="3">' . $total
.. '</td></tr></table>';

}
else echo "No items";
}

Now a few questions:
1) Since you are using sessions, why are you passing the value of
"$_SESSION['items'][$i][0]" in your link, why not just "$i"?

2) What's your database look like?
Are looking for a statement like:
$q = "Update tablename set quantity='" . $_SESSION['items'][$i][3] . "'
where product_id = '" . $item . "'";

Ken

Jul 17 '05 #2
<ia*******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi,

I've spent most of today trying to solve this problem, but sadly no
luck.
I have an shopping basket based on an array which stores the name,
product id, quantity and price for products, i want to take the
Information in the array and output the Quantity and Product ID
information into variables so that i can pass them into a Select
Statement and submit the order to the database.

The Current Code to Show the cart is listed below: -

function show_cart() {
if (isset($_SESSION["item_count"]) && $_SESSION["item_count"] > 0) {
echo "<table class=\"basket\"border=\"0\">";
echo
"<tr><td>Item</td><td>Price</td><td>Amount</td><td>Subtotal</td></td></tr>";
$total = 0;
for($i=0; $i<$_SESSION["item_count"]; $i++) {

echo "<td><a
href=insertselfpagehere.php?deleteitem=1&ProdID=". $_SESSION["items"][$i][0].
">";
$item=$_SESSION["items"][$i][0];
echo $_SESSION["items"][$i][1]."</a></td>";
echo "<td>".$_SESSION["items"][$i][2]."</td>";
echo "<td>*".$_SESSION["items"][$i][3]."</td>";
$subtotal = $_SESSION["items"][$i][2] * $_SESSION["items"][$i][3];
echo "<td align=\"right\">$subtotal</td>";
echo "</tr>";
$total += $subtotal;
}
echo "<tr><td><b>total</b></td><td align=\"right\"
colspan=\"3\">$total</td></tr></table>";
}

else {
echo "No items";
}
}

Any help would be much appreciated, i seem to have a mental block with
PHP at the moment.

Kind Regards,

Ian


I'd love to help you, but I'm not quite sure what the "problem" is. There
doesn't appear to be any coding errors (PHP wise) here, although you are
missing some quotes in certain places:

echo '<td><a>
href="insertselfpagehere.php?deleteitem=1&ProdID=' .$_SESSION["items"][$i][0]
..'">';

You remembered them with the <table> tag, I'm sure you just overlooked this
one.

Also, if "insertselfpagehere.php" is meant to denote, well, whatever page
you are at, you can always use:

$_SERVER['php_self']

as a better alternative (for example, if the page name changes, you don't
need to change it within the script).

But, like I said, I'm not sure where your problem is. If you could post a
description of the problem and the error output (or the incorrect output and
what it should be), I'll do my best to help out.

-Noah
Jul 17 '05 #3
Thanks for the replies,
The problem is that i really don't know too much about arrays.
I know how to use varialbes and mysql with PHP but i'm stuck with
Arrays.
All i need to do is get the contents of the array into variables, after
this i need to add them to a mysql DB so that the order can be saved,
processed or used as a quote at a later point.

Ken mentioned: -
2) What's your database look like?
Are looking for a statement like:
$q = "Update tablename set quantity='" . $_SESSION['items'][$i][3] . "'
where product_id = '" . $item . "'";
This seems like what i'm after, only this would require X amounts of DB
lookups (X being the number of different items in the DB)
Is this the best way to do it?

Kind Regards,

Ian

Jul 17 '05 #4

<ia*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| Thanks for the replies,
| The problem is that i really don't know too much about arrays.
| I know how to use varialbes and mysql with PHP but i'm stuck with
| Arrays.
| All i need to do is get the contents of the array into variables, after
| this i need to add them to a mysql DB so that the order can be saved,
| processed or used as a quote at a later point.
|
| Ken mentioned: -
| 2) What's your database look like?
| Are looking for a statement like:
| $q = "Update tablename set quantity='" . $_SESSION['items'][$i][3] . "'
| where product_id = '" . $item . "'";
|
|
| This seems like what i'm after, only this would require X amounts of DB
| lookups (X being the number of different items in the DB)
| Is this the best way to do it?
|
| Kind Regards,
|
| Ian
|

Orders eh - presumably you have order header and order detail tables to
enable you to have 1->n items per order. If this is the case then you have
to have one insert/update for each unique row.

rtfm springs to mind. http://dev.mysql.com/doc/mysql/en/INSERT.html and
http://dev.mysql.com/doc/mysql/en/UPDATE.html would be good places to start.

Chris
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tek9_AK | last post by:
I need to find a way to transfer all the values of an array inside a function out of the fuction into another array. IE function splice($filename){ if(file_exists($filename)){...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: Tom Page | last post by:
Hello all I have a question that may or may not be simple - I need my three dimensional array to be of the form: array where jj and kk range from 0 to some constant fixed at the very...
80
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
18
by: lawrence | last post by:
If I'm pretty sure there is just one form on the page, can i do this? var myForm = document.forms; If I'm not sure about the form, is it safer to do this? if (document.forms) { var myForm =...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
2
by: Matthew Sajdera | last post by:
All - This probably qualifies for the "Stupid Question of the Day" award but I am stuck on this. I have an IIS Application object in which I am storing a multi-dimensional array. Later on in...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
6
by: sathyashrayan | last post by:
Dear Group, Please look at the following demo link. http://www.itsravi.com/demo/new_pms/admin/addproject.php
2
by: pedalpete | last post by:
I've got a php page which is outputing xml to a file. The code has been working without a hitch for weeks when running as localhost, but a few days ago I uploaded it to my ec2 instance, and it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.