Need some supervision on this code

The while loop or php code below the query is coded is intended to produce the same display I have illustrated below the php code wrapped in code tags. I come here because I am not sure if the php code will exactly produce the same display that you guys can see in the INTENDED DISPLAY illustration i have done below.

$result = mysql_query("SELECT
                      cart.id         cart_id,
                      cart.id         cart_id,
                      cart.cartId     cartId,
                      cart.cookieId   cookie_Id,
                      cart.qty        qt_y,                     
                      cdkb.id         cdkb_id,
                      cdkb.name       name,
                      cdkb.image      image,
                      cdkb.price      price,
                      dkb.id          dkb_id,
                      dkb.name        name1, 
                      dkb.image       image1,
                      dkb.price       price2,
                      dbl.product_id  product_id,
    		      dbl.price       price3,
    		      dbl.variety     variety,
    		      dbl.description description                  
    FROM
            cart
        
    
            LEFT OUTER JOIN cdkb
               ON cart.id = cdkb.id    
          
            LEFT OUTER JOIN dkb
               ON cart.id = dkb.id
                    
            LEFT OUTER JOIN dbl
               ON dbl.id = dkb.id 
        
              
        WHERE
            cart.cookieId ='" . GetCartId() . "' ' ORDER BY cdkb.name AND dkb.name ASC"); 
     while($row = mysql_fetch_array($result))
    {  if($row['cdkb_id']){
    echo "<div>qty</div>";
    echo"<div>". $row['image'] . "</div>";
    echo"<div>". $row['name'] . "</div>";
    echo"<div>". $row['price'] . "</div>";
    echo"<div>remove</div>";
    }
    else{
    echo "<div>qty</div>";
    echo"<div>". $row['image1'] . "</div>";
    echo"<div>". $row['name1'] . "</div>";
    foreach ($row['dkb_id'] as $variety) {
    echo"<div>".$variety['variety']. $variety['price3'] . "</div>";
    // the vriety and price3 field will display three times as in table dbl has three prices and three variety. It is correct to use a foreach loop for  in this case?}
    echo"<div>remove</div>"; 
    // end of foreach loop inside the else statement
    }// end of if else statement
    }// end of while loop
    ?>
    

So the final display or INTENDED DISPLAY illustration is as below but i am not sure if the code above will exactly produce as below.

 while($row = mysql_fetch_array($result))
    {  
    if (table cdkb) { 
      [1]qyt         image      name          price        remove
         1            ---       marina        $18.90        remove?    
         }end of if statement
    Else table dkb {
       1]qyt            Image         Name             Price                  Remove
         1               ---         marina         Small Tray  $18.90        remove? 
                                                    Medium Tray $30.24
                                                    Large Tray  $35.90
                      } // end of else statement
                      
                      }//end of while loop

Thanks!

Of course the best thing to do would be to try it and see. But you have 2 things to worry about. Is the query getting what you expect it to? And is the code going to create the mark-up you want? It looks like it will make subsequent div’s that unless you have some way of applying CSS to them won’t fall into a “table like” position.

Perhaps if you put together an array to simulate expected db results and use that you can more easily mess with the mark-up to get it into shape?

I don’t know if you have trouble with multi-dimensional arrays or not, but I put this together (changing the while to a foreach and adding newlines)

<?php
$result = array(
		'0' => array('cdkb_id' => 'id 1',
			'image' => 'img 1',
			'name' => 'name 1',
			'price' => 'price 1'
			),
		'1' => array('image1' => 'img 2',
			'name1' => 'name 2',
			'dkb_id' => array('0' => array('variety' => 'variety 2-1',
						 'price3' => 'price 2-1'
						),
					'1' => array('variety' => 'variety 2-2',
						 'price3' => 'price 2-2'
						),
					'2' => array('variety' => 'variety 2-3',
						 'price3' => 'price 2-3'
						)
					)
			),
		'2' => array('cdkb_id' => 'id 3',
			'image' => 'img 3',
			'name' => 'name 3',
			'price' => 'price 3'
			),
		'3' => array('image1' => 'img 4',
			'name1' => 'name 4',
			'dkb_id' => array('0' => array('variety' => 'variety 4-1',
						 'price3' => 'price 4-1'
						),
					'1' => array('variety' => 'variety 4-2',
						 'price3' => 'price 4-2'
						),
					'2' => array('variety' => 'variety 4-3',
						 'price3' => 'price 4-3'
						)
					)
			)		
		);
foreach($result as $row)
{ 
  if($row['cdkb_id']){
    echo "<div>qty</div>\
";
    echo"<div>". $row['image'] . "</div>\
";
    echo"<div>". $row['name'] . "</div>\
";
    echo"<div>". $row['price'] . "</div>\
";
    echo"<div>remove</div>\
";
    }
    else{
    echo "<div>qty</div>\
";
    echo"<div>". $row['image1'] . "</div>\
";
    echo"<div>". $row['name1'] . "</div>\
";
    foreach ($row['dkb_id'] as $variety) {
    echo"<div>".$variety['variety']. " ~ " . $variety['price3'] . "</div>\
";
    // the vriety and price3 field will display three times as in table dbl has three prices and three variety. It is correct to use a foreach loop for  in this case?}
    echo"<div>remove</div>\
"; 
    }// end of foreach loop inside the else statement
    }// end of if else statement
}
?>

it produces this

<div>qty</div>
<div>img 1</div>
<div>name 1</div>
<div>price 1</div>
<div>remove</div>
<div>qty</div>
<div>img 2</div>
<div>name 2</div>
<div>variety 2-1 ~ price 2-1</div>
<div>remove</div>
<div>variety 2-2 ~ price 2-2</div>
<div>remove</div>
<div>variety 2-3 ~ price 2-3</div>
<div>remove</div>
<div>qty</div>
<div>img 3</div>
<div>name 3</div>
<div>price 3</div>
<div>remove</div>
<div>qty</div>
<div>img 4</div>
<div>name 4</div>
<div>variety 4-1 ~ price 4-1</div>
<div>remove</div>
<div>variety 4-2 ~ price 4-2</div>
<div>remove</div>
<div>variety 4-3 ~ price 4-3</div>
<div>remove</div>

As I don’t see any class selectors, maybe you want “place holder” divs? Although IMHO giving them a class attribute would be cleaner.

I was thinking, if you’re not already “locked in” to using divs, why not use a table? There’s lots of flak about using tables for layout, but don’t shy away from using them for their intended purpose, tabular data. Which this is. And it could be argued that using a table would be better than “divitis”,

I back Mittineague, this would be ideal for a table, and more accessible also.

@Mittineague I think i am lucked in to use divs I could re-arrange. what I have put here it’s just to get the php code straight. I will test it on tables and see the results as well. I have changed the while loop to a foreach loop and it should be working ok. Well I say “should” because I am having an
Parse error: syntax error, unexpected T_ELSE. Look at the if and else statment design and what’s possiblly causing this error.


if {
 for (blabla) {
if {

}// end of if statement inside the for loop

} //end of for loop.

} end of if statement 
else{ // line 379


}// end of else statement

What I believe causing the T_ELSE syntax error above is the if statement inside the for loop. Php believes that the else statement is working together with the if statement inside the for loop and what I want is the if statement outside the for loop work together with the else statement. The error looks like this:


Parse error: syntax error, unexpected T_ELSE in /home3/nyhungry/public_html/cart.php on line 379 

Look at line 379 in the code above. I wonder if there is a way to end the if statement inside the for loop and avoid php to confuse to believe that the if statement inside the for loop is working together with the else statement?

Well I believe that could be the source of this error I might be mistaken, I have check that the brackets ends and are placed where they should be.

@rguy84 Thank you.

@Mittineague thank you for the array illustration that’s exactly on what I called The INTENDED DISPLAY in post #1.

That code has a number of problems, and guess what … none of them are T_ELSE :rolleyes:

Below is the whole code and an explanation of why I think is not working. thanks!

<?php
    
    switch($_GET["action"])
    {
    case "add_item":
    {
    AddItem($_GET["ids"], $_GET["qty"]);
    ShowCart();
    break;
    }
    case "update_item":
    {
    UpdateItem($_GET["ids"], $_GET["qty"]);
    ShowCart();
    break;
    }
    case "remove_item":
    {
    RemoveItem($_GET["ids"], $_GET["id"]);
    ShowCart();
    break;
    }
    default:
    {
    ShowCart();
    }
    }
    function AddItem($itemId, $qty){
    
    
    $result = mysql_query("SELECT COUNT(*) FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
    
    $row = mysql_fetch_row($result);
    $numRows = $row[0];
    
    if($numRows == 0)
    {
    // This item doesn't exist in the users cart,
    // we will add it with an insert query
    
    mysql_query("INSERT INTO cart(cookieId, id, qty) values('" . GetCartId() . "', $itemId, $qty)");
    //printf ("Inserted records: &#37;d\
", mysql_affected_rows());
    
    }
    else
    {
    // This item already exists in the users cart,
    // we will update it instead
     mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
    
    }
    
    }
    
    function UpdateItem($itemId, $qty)
    {
    
    mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
    //printf ("Updated records: %d\
", mysql_affected_rows());
    
    }
    
    function RemoveItem($itemId) 
    {
    
    mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId");
    }
    echo $ids;
    ?>
    
    <?php 
    function ShowCart()
    
    {
    
    $result = mysql_query("SELECT
                  cart.id         cart_id,
                  cart.id         cart_id,
                  cart.cartId     cartId,
                  cart.cookieId   cookie_Id,
                  cart.qty        qt_y,                     
                  cdkb.id         cdkb_id,
                  cdkb.name       name,
                  cdkb.image      image,
                  cdkb.price      price,
                  dkb.id          dkb_id,
                  dkb.name        name1, 
                  dkb.image       image1,
                  dkb.price       price2,
                  dbl.product_id  product_id,
              dbl.price       price3,
              dbl.variety     variety,
              dbl.description description                  
        FROM
        cart


        LEFT OUTER JOIN cdkb
           ON cart.id = cdkb.id    

        LEFT OUTER JOIN dkb
           ON cart.id = dkb.id

        LEFT OUTER JOIN dbl
           ON dbl.id = dkb.id 


    WHERE
        cart.cookieId ='" . GetCartId() . "' ' ORDER BY cdkb.name AND dkb.name ASC");?>
    
    <div id="cart">
    <div id="group">
    <div id="quantity">Qty</div>
    <div id="cartpic">Pic</div>
    <div id="product">Product</div>
    <div id="cartprice">Price</div>
    <div id="remove">Remove</div>
    </div>
    <?php
      
    $totalCost=0;
    foreach($result as $row)
    {  
    if($row['ckb_id']){
    // Increment the total cost of all items
    $totalCost += ($row["qt_y"] * $row["price"]);
    
    ?>
    
    <div id="cart1">
    <select name="<?php echo $row["cdkb_id"];?>" onChange="UpdateQty(this)">
    <?php  print($row["cdkb_id"]);?>
    <?php
    
    for($i = 1; $i <= 30; $i++)
    {
    echo "<option ";
    if($row["qt_y"] == $i)
    {
    echo " SELECTED ";
    }
    echo ">" . $i . "</option>";
    }
    ?>
    </select>
    </div>
    <div id="cart2">
    <img src="images/logopic.gif"<?php /*?><?php echo $row["image"]; ?><?php */?> alt="we" width="60" height="50" />
    </div>
    <div id="cart3"><p><?php echo $row["name"]; ?></p></div>
    
    
    
    <div id="cart4"><p>
    $<?php echo number_format($row["price"], 2, ".", ","); ?></p></div>
    
    <div id="cart5">
    <p><?php
    printf('<a href="cart.php?action=remove_item&id=%d&ids=%d&register=%s">Remove</a>', $_GET['id'], $row['cdkb_id'], $_GET['register']);
    ?></p></div>
    
    <hr size="1" color="red" >
    
    <script language="JavaScript">
    
    function UpdateQty(item)
    {
    itemId = item.name;
    newQty = item.options[item.selectedIndex].text;
    
    document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
    }
    
    </script>
    
    
    
    
    
    
    <?php
    }
    ?>
    <?php
    else {
    ?>
    <?php // Increment the total cost of all items
    $totalCost += ($row["qt_y"] * $row["price2"]);
    
    ?>
    
    <div id="cart1">
    <select name="<?php echo $row["dkb_id"];?>" onChange="UpdateQty(this)">
    <?php  print($row["dkb_id"]);?>
    <?php
    
    for($i = 1; $i <= 30; $i++)
    {
    echo "<option ";
    if($row["qt_y"] == $i)
    {
    echo " SELECTED ";
    }
    echo ">" . $i . "</option>";
    }
    ?>
    </select>
    </div>
    <div id="cart2">
    <img src="images/logopic.gif"<?php /*?><?php echo $row["image2"]; ?><?php */?> alt="we" width="60" height="50" />
    </div>
    <div id="cart3"><p><?php echo $row["name2"]; ?></p></div>
    
    <?php foreach ($row['dkb_id'] as $variety) {?>
    <div id="cart4">
       <?php echo"<p>".$variety['variety']. " ~ " . $variety['price3'] . "</p>" ?>
    <p>$<?php echo number_format($row["price3"], 2, ".", ","); ?></p></div>
    <?php } ?>
    <div id="cart5">
    <p><?php
    printf('<a href="cart.php?action=remove_item&id=%d&ids=%d&register=%s">Remove</a>', $_GET['id'], $row['dkb_id'], $_GET['register']);
    ?></p></div>
    
    <hr size="1" color="red" >
    
    <script language="JavaScript">
    
    function UpdateQty(item)
    {
    itemId = item.name;
    newQty = item.options[item.selectedIndex].text;
    
    document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
    }
    
    </script>
    
    
    
    
    <font face="verdana" size="2" color="black" style="clear:right;">
    <b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td>
    
    
    <?php } ?>
    <?php } ?>
    <div id="shopping">
    <a href=<?php echo "exa2.php?id=".intval($id).  '&register='. $_GET['register']. ""?>> Keep Shopping</a></div>
    <?php /*?><tr>
    <td colspan="4">
    <font face="verdana" size="1" color="black">
    <a href=<?php echo "cart.php?id=".intval($id).  '&register='. $_GET['register']. ""?>>Your Shopping Cart</a></font></td>
    </tr><?php */?>
    
    </tr>
    </table>
    
    </table>

In the code above below the query there will be an if and else statement sequence that is causing a parse errror: syntax error, as it fallows Parse error: syntax error, unexpected T_ELSE. Look at the if and else statment design and what’s possiblly causing this error.

if {
      for (blabla) {
        if {
    
        }// end of if statement inside the for loop
    
      } //end of for loop.
    
    } end of if statement 
    else{ // line 379
    
    
    }// end of else statement

What I believe is causing the T_ELSE syntax error above as I said in the last post is the if statement inside the for loop. Php believes that the else statement is working together with the if statement inside the for loop and what I want is the if statement outside the for loop work together with the else statement. The error looks like this:

PS everything that possibly causing the parse error is below the query. thanks.

The INTENDED DISPLAY in between the if and else would look like

  foreach($result as $row)
        {  
        if (table cdkb) { 
          [1]qyt         image      name          price        remove
             1            ---       marina        $18.90        remove?    
             }end of if statement
        Else table dkb {
           1]qyt            Image         Name             Price                  Remove
             1               ---         marina         Small Tray  $18.90        remove? 
                                                        Medium Tray $30.24
                                                        Large Tray  $35.90
                          } // end of else statement
                          
                          }//end of while loop

This is the array structure behind the INTENDED display!

<?php
    $result = array(
            '0' => array('cdkb_id' => 'id 1',
                'image' => 'img 1',
                'name' => 'name 1',
                'price' => 'price 1'
                ),
            '1' => array('image1' => 'img 2',
                'name1' => 'name 2',
                'dkb_id' => array('0' => array('variety' => 'variety 2-1',
                             'price3' => 'price 2-1'
                            ),
                        '1' => array('variety' => 'variety 2-2',
                             'price3' => 'price 2-2'
                            ),
                        '2' => array('variety' => 'variety 2-3',
                             'price3' => 'price 2-3'
                            )
                        )
                ),
            '2' => array('cdkb_id' => 'id 3',
                'image' => 'img 3',
                'name' => 'name 3',
                'price' => 'price 3'
                ),
            '3' => array('image1' => 'img 4',
                'name1' => 'name 4',
                'dkb_id' => array('0' => array('variety' => 'variety 4-1',
                             'price3' => 'price 4-1'
                            ),
                        '1' => array('variety' => 'variety 4-2',
                             'price3' => 'price 4-2'
                            ),
                        '2' => array('variety' => 'variety 4-3',
                             'price3' => 'price 4-3'
                            )

The only thing on the way is the T_ELSE because there is an if in between the if and else statement inside the for loop…

Thank.

I looked but couldn’t see anything obvious. I suspect there’s a missng } or ; somewhere. AFAIK you can nest an if inside a for OK. But the else has to pair up with an if or elseif.

If you clean up the code, (format it, indent it, remove unnecessary open/close tags), the T_ELSE problem will go away. (You will have a new one tho, but should be easier to spot now).