Reputation: 2268
I've checked through the similarly worded questions on Stack Overflow but none are specific to what I'm attempting.
I have session data, two loops and a SQL table.
...this can also be described in a less abstract and friendlier manner:
Since my session data consists of products ids (keys) and a corresponding quantity (values) I need to show the quantity as part of each outputted row.
id | title | description | unit cost | amount | subtotal
session data key | from database | from database | from database | session data value | basic PHP math
(also in database) (unit cost * amount)
The first loop to use the session data keys to query the require products:
$id_from_session = "WHERE id = '9999999999'"; //fix due to not being sure how to simuataneously incorporate "WHERE" and "OR" into query loop (only looping the "OR" condition).
foreach($_SESSION['post'] as $key=>$value)
{
$id_from_session .= "OR id ='".$key."'";
}
The second loop to output the resulting rows of the query:
foreach ( $yourorders as $yourorder )
{
?>
<?php echo $yourorder->title; ?></br>
<?php echo $yourorder->description; ?></br>
<?php echo $yourorder->value; ?></br>
<?php /* I NEED THE QUANTITY HERE */ ?></br>
<?php /* VALUE * QUANTITY HERE */ ?></br>
<?php
}
<?php /* OVERALL TOTAL OUTSIDE OF LOOP */ ?></br>
How do I incorporate the quantity from the session data loop (and the quantity * unit value) into the SQL results loop?
Upvotes: 2
Views: 367
Reputation: 412
$_SESSION['post'][$yourorder->id] gives you the quantity since the keys are the product ids :)
well.. if you can be absolutely sure that your shopping cart keys don't need to be escaped you can write this:
$id_from_session = "WHERE id IN ('" . implode( "', '", array_keys($_SESSION['post']) ) . "')";
Upvotes: 0
Reputation: 2967
$_SESSION['post'][$yourorder->id]
should get you the quantity right? so,
<?php echo $_SESSION['post'][$yourorder->id]; ?>
or if short tags are allowed
<?= $_SESSION['post'][$yourorder->id]; ?>
Also a nicer way to build the WHERE string.
$where = array();
foreach($_SESSION['post'] as $key=>$value)
{
$where[] = "id ='".$key."'";
}
$id_from_session = "WHERE " . implode(' OR ', $where);
Upvotes: 2