CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   Looping Through MySQL Queries (http://www.codingforums.com/showthread.php?t=282457)

d'Anconia 11-17-2012 06:19 PM

Looping Through MySQL Queries
 
Okay so my issue is that I am trying to loop through $_SESSION variables with each element of that array getting a query (that checks the database for its existence). I am having problems figuring out the correct way to do the syntax, especially for the query.

Essentially I am trying to concatenate the element name from the $_SESSION array with the variable $n which will just be an integer increasing by 1 every time it goes through the loop.

PHP Code:

$n 0;
while(isset(
$_SESSION['item_' $n])) {
    $[
B]current_cart_query "SELECT product_name, product_price, product_desciption, image_path 
        FROM products WHERE product_id = $_SESSION['item_'" 
$n]" LIMIT 1";[/B//need to do a join?
    
$current_cart_results mysqli_query($dbc$current_cart_query) or trigger_error("Query: "
            
$current_cart_query "\n<br />MySQL Error: " mysqli_error($dbc));
    
$cc_row mysqli_fetch_array($current_cart_resultsMYSQLI_ASSOC);
    
$product_total $ccrow['product_price '] * $_SESSION['item_' $n 'qty'];
    echo 
'
        <div class="product_box">
            <span class="product_image">
                <a href="view_product.php?product_id=' 
$ccrow['product_id'] . '">
                    <img src="' 
$ccrow["image_path"] . '" />
                </a>
            </span>
            <strong>' 
$ccrow["product_name"] . '</strong><br />
            "<em>' 
$ccrow["product_description"] . '</em>"<br />
            <strong>' 
$ccrow["product_price"] . '</strong> x ' $_SESSION["item_" $n "qty"] . 
                            
' = $' $product_total '
        </div>
        '
;
    
$n++;
}


Currently the error that I am getting says the following:
Code:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/kylhur/ecommerce.danconia.us/cart.php on line 46
Where line 46 is the line in bold. What is the correct syntax to do this? Maybe there is an easier way?

sunfighter 11-17-2012 09:05 PM

Your problem is this:
WHERE product_id = $_SESSION['item_'" . $n]"
More specifically $_SESSION['item_'" . $n]"

Id work that out before the query statement, before the while statement:
PHP Code:

$n 0;
$index "item_".$n;
while(isset(
$_SESSION['item_' $n])) {
    
$current_cart_query "SELECT product_name, product_price, product_desciption, image_path
        FROM products
        WHERE product_id = $_SESSION[$index]
        LIMIT 1"




All times are GMT +1. The time now is 05:57 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.