0

Hello I am doing a shopping store project in Php.I am trying to display my products just like a shopping cart page.I did it in a list view which shows all products one below another in vertical format.I had thoughts to show in a tabular format with 4-5 products on each row as in real world shopping websites but I am getting nowhere. my code is:

<?php
$sql = 'SELECT * FROM burgers ORDER BY id';
$result = $db->query($sql);
$output = '<ul>';
while ($row = $result->fetch()) {
    $output .= '<li>"'.$row['title'].'" made by </br> '.$row['chef'].': Rs '.$row['price'].'<img src="images/'.$row['image'].'" width="100" height="100" /><br />';
    if (isset($_SESSION['username'])) {
        $output .= '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
    }
    else {
        $output .= '<a href="login.php">Login</a></li>';
    }
}
$output .= '</ul>';
echo $output;
?> 
2
  • 2
    If you want all the items to be listed like a table, having the same type of elements at the same horizontal position, better use a table. Commented Oct 16, 2013 at 7:55
  • I tried it its showing some nonsense output or I am not able to do it properly. Commented Oct 16, 2013 at 7:58

1 Answer 1

2

If you want to show it in tabular format you can just use table. It is created for things like this. Just do something like this:

$output = '<table><tr>';
$products_per_row = 5;
$i = 0;
while ($row = $result->fetch()) {
    $i++;
    $output .= '<td>"'.$row['title'].'" made by </br> '.$row['chef'].': Rs '.$row['price'].'<img src="images/'.$row['image'].'" width="100" height="100" /><br />';
    if (isset($_SESSION['username'])) {
        $output .= '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></td>';
    }
    else {
        $output .= '<a href="login.php">Login</a></td>';
    }
    if ($i == $products_per_row) { $output .= '</tr><tr>'; $i = 0; }
}
$output .= '</tr></table>';

If you want to use li you can fix it's width, use css for this (li { width: x px; } ), where x is row (ul's container) width divided by number of columns. Eg if you want 5 products per row, and your container above ul (and ul) has 1000px width, just do li with 200px. Don't forget about padding and margins.

Sign up to request clarification or add additional context in comments.

1 Comment

I modifed code - in last if you should set $i = 0. You'll see that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.