Skip to content
Advertisement

shopping cart with PHP and connected to the MySQL

Good evening, I am currently working on the implementation of a shopping cart through PHP and MySQL, I am getting the following error at line 172 onwards, I have been looking at the quotes but I cannot find the problem, what could be the solution to this?

ERROR : Parse error: syntax error, unexpected 'img' (T_STRING), expecting ';' or ',' in B:XAMPPhtdocsdashboardPHP_AssessmentsSport_Carscart.php on line 171     


 <div class="container" style="width:700px;">  
                    <h3 align="center">Simple PHP Mysql Shopping Cart</h3><br />  
                    <?php  
                    $sql = "SELECT * FROM sport_cars.cars ORDER BY carID ASC";  
                     //prepared statement
                     $statement = $conn->prepare($sql);
                     $statement->execute();
                     $result = $statement->fetchAll();
                     $statement->closeCursor(); 
                     foreach($result as $row):
                        echo "<div class='col-md-4'>";
                        echo "<form method='post' action='cart.php?action=add&id=" .$row['carID']. ' ">";
                        echo "<div><img class='img-responsive' src= " .'view/images/'. $row['Photo'] . " /><br/> ";  
                        echo  "<h4 class='text-info'> . $row['carName'] . </h4> ";
                        echo "<h4 class='text-danger'>.$row['carPrice']; . "</h4> ";  
                        echo "<input type='text' name='quantity' class='form-control' value='1' /> ";  
                        echo "<input type='hidden' name='hidden_name' value=.' ['carPrice']; '. /> ";  
                        echo  "<input type='hidden' name='hidden_price' value=.' $row['carPrice']; '. /> ";  
                        echo  "<input type='submit' name='add_to_cart' style='margin-top:5px;' class='btn btn-success' value='Add to Cart' /> ";
                        echo "</div></form></div>";
                    endforeach;
                    if($result > 0)  
                    {  
                         while($row = $statement -> fetchAll())  
                         {  
                    ?>  
                    <div class="col-md-4">  
                         <form method="post" action="cart.php?action=add&id=<?php echo $row["carID"]; ?>">  
                              <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">  
                                   <img src="<?php echo $row["Photo"]; ?>" class="img-responsive" /><br />  
                                   <h4 class="text-info"><?php echo $row["carName"]; ?></h4>  
                                   <h4 class="text-danger">$ <?php echo $row["carPrice"]; ?></h4>  
                                   <input type="text" name="quantity" class="form-control" value="1" />  
                                   <input type="hidden" name="hidden_name" value="<?php echo $row["carPrice"]; ?>" />  
                                   <input type="hidden" name="hidden_price" value="<?php echo $row["carPrice"]; ?>" />  
                                   <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" />  
                              </div>  
                         </form>  
                    </div>  
                    <?php  
                         }  
                    }  
                    ?>  
                    <div style="clear:both"></div>  
                    <br />  
                    <h3>Order Details</h3>  
                    <div class="table-responsive">  
                         <table class="table table-bordered">  
                              <tr>  
                                   <th width="40%">Item Name</th>  
                                   <th width="10%">Quantity</th>  
                                   <th width="20%">Price</th>  
                                   <th width="15%">Total</th>  
                                   <th width="5%">Action</th>  
                              </tr>  
                              <?php   
                              if(!empty($_SESSION["shopping_cart"]))  
                              {  
                                   $total = 0;  
                                   foreach($_SESSION["shopping_cart"] as $keys => $values)  
                                   {  
                              ?>  
                              <tr>  
                                   <td><?php echo $values["carName"]; ?></td>  
                                   <td><?php echo $values["quantity"]; ?></td>  
                                   <td>$ <?php echo $values["carPrice"]; ?></td>  
                                   <td>$ <?php echo number_format($values["quantity"] * $values["carPrice"], 2); ?></td>  
                                   <td><a href="cart.php?action=delete&id=<?php echo $values["carID"]; ?>"><span class="text-danger">Remove</span></a></td>  
                              </tr>  
                              <?php  
                                        $total = $total + ($values["quantity"] * $values["carPrice"]);  
                                   }  
                              ?>  
                              <tr>  
                                   <td colspan="3" align="right">Total</td>  
                                   <td align="right">$ <?php echo number_format($total, 2); ?></td>  
                                   <td></td>  
                              </tr>  
                              <?php  
                              }  
                              ?>  
                         </table>  
                    </div>
             </div>

I have been changing this cart from a mysqli version of it into pdo and I am having some trouble to make it work, I hope this can be fixed.

Thanks for your help.

Advertisement

Answer

You had some errors in your foreach loop containing $result so try this:

foreach($result as $row){ 

    echo "<div class='col-md-4'>";
    echo "<form method='post' action='cart.php?action=add&id=" .$row['carID'].  ">";
    echo "<div><img class='img-responsive' src= " .'view/images/'. $row['Photo'] . " 
    /> 
    <br/> ";  
    echo  "<h4 class='text-info'>" . $row['carName'] . "</h4> ";
    echo "<h4 class='text-danger'>".$row['carPrice'] . "</h4> ";  
    echo "<input type='text' name='quantity' class='form-control' value='1' /> ";  
    echo "<input type='hidden' name='hidden_name' value='" . $row['carPrice'] . "'/> 
    ";  
    echo  "<input type='hidden' name='hidden_price' value='" . $row['carPrice']  . 
    "'/> ";  
    echo  "<input type='submit' name='add_to_cart' style='margin-top:5px;' class='btn 
    btn- 
    success' value='Add to Cart' /> ";
    echo "</div></form></div>";

}

You could try using printf to help with readability of the code. http://php.net/printf

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement