This is a discussion on Getting Array data into a variable within the PHP Language forums, part of the PHP Programming Forums category; Hi, I've spent most of today trying to solve this problem, but sadly no luck. I have an shopping ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
<iannorton@gmail.com> wrote in message
news:1105296684.673441.276640@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 |
|
|||
|
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 |
|
|||
|
<iannorton@gmail.com> wrote in message news:1105564155.335161.135570@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 |