Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts

    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?
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,038
    Thanks
    23
    Thanked 589 Times in 588 Posts
    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"



  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •