Dominor Novus
Dominor Novus

Reputation: 2268

Combine PHP foreach loop with a SQL foreach loop

Forward:

I've checked through the similarly worded questions on Stack Overflow but none are specific to what I'm attempting.

What I have:

I have session data, two loops and a SQL table.

  1. The first loop checks for specific existing session data and uses the keys (rather than the values) of this data to determine what ids should be selected (SQL) from a table.
  2. The second loop outputs the SQL results row by row.

...this can also be described in a less abstract and friendlier manner:

  1. The first loop looks for items currently added to the shopping cart (key = product id, value = quantity) to determine what products should be listed on "Your order" page.
  2. The second loop list rows of data associated with each product id such as the title, description and unit cost.

What I need:

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)

My code:

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>

My question:

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

Answers (2)

indriq
indriq

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

shapeshifter
shapeshifter

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

Related Questions