Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-17-2012, 06:19 PM   PM User | #1
d'Anconia
Regular Coder

 
d'Anconia's Avatar
 
Join Date: Jan 2010
Location: Tempe, AZ
Posts: 143
Thanks: 15
Thanked 5 Times in 5 Posts
d'Anconia is an unknown quantity at this point
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?
__________________
Powerful ideas for all lovers of personal and political freedom:
Freedomain Radio
Free Talk Live
d'Anconia is offline   Reply With Quote
Old 11-17-2012, 09:05 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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"

sunfighter is offline   Reply With Quote
Reply

Bookmarks

Tags
loop, mysql, session

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 AM.


Advertisement
Log in to turn off these ads.