0

I'm just going to include my whole "shopping.php" page code so you know where I'm coming from.

<!DOCTYPE html>
<html>
<head><title>Shopping Page</title>
<style type="text/css">
</style>
<script type="text/javascript">
</script>
</head>
<body> 
    <form name="form1">
    <?php
    if (isset($_GET['cat'])) $cat=$_GET['cat'];
    else $cat = "fx";
    echo $cat;

    //1. Make a connection to the database
    $dbconn = mysqli_connect("localhost","root","","mydatabase1") 
      or die(mysqli_connect_error());
    $sql = "select productid,name,imagefile "
    ."from products "
    ."where categoryid='$cat'";
   echo $sql;


    //2. Run a query against the database
    $result = mysqli_query($dbconn,$sql) 
    or die(mysqli_error($dbconn));

...and this is the part where I want the results returned as a 3 column table:

//3. Return the results
while ($row = mysqli_fetch_row($result))
{
    echo "<a href='order.php?prod=$row[0]'>$row[1]</a><br>";
    echo "<img src='images/products/$row[2]' "
    ."height=200px width =175px><br>";
}

 //4. Release the resources
 mysqli_free_result($result);

//5. Close the connection
mysqli_close($dbconn);
?>
</form>
</body>
</html>

Right now I get the results but it's just listed vertically. I tried a few different ways but I guess my noobness is getting the best of me. Any help would be greatly appreciated. Thanks.

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41

2 Answers2

0

All I see is a two column table but you should make an HTML table. (The <br> tags are making it vertical)

$count = 0;
echo "<table><tr><th>Name</th><th>Image</th></tr>";
while ($row = mysqli_fetch_row($result))
{
     if($count % 3 == 0){
         echo "<tr>";  //every third image make a new row
     }
     echo "<td><a href='order.php?prod=$row[0]'>$row[1]</a>";
     echo "<img src='images/products/$row[2]' "
     ."height=200px width =175px></td>";
     if($count % 3 == 2){
         echo "</tr>";  //every third image end the row
     }
     $count++;
}
echo "</table>";

ALSO

Please see this reference as you should bind your parameters if you are using mysqli or use PDO. How can I prevent SQL injection in PHP?

In response to your comment

Added a count which adds in the table row tags every third image. The % is the modulus operator which give you the remainder on division.

Community
  • 1
  • 1
dstudeba
  • 8,878
  • 3
  • 32
  • 41
  • Thanks. I definitely think you have me on the right track. What I was trying to get was 3 columns, each with the name and then the image underneath. Kind of like a simplified version of the "featured products" section on this site. [link](http://www.halloweenasylum.com/) – Mega Lopunny Sep 23 '15 at 03:53
  • Oh I see that is what you mean by three columns, give me a minute. – dstudeba Sep 23 '15 at 03:55
  • Thank you so much!!! I was trying to do the count thing before but my placement must have been off. Worked like a charm, – Mega Lopunny Sep 23 '15 at 04:34
0

You can do it in many ways. Using table is the better solution according to me. But if you want the easiest way than it will be using div. Use columns Property to make column. Use the following style:

<style>
.column3 {
    -webkit-column-count: 3; /* Chrome, Safari, Opera */
    -moz-column-count: 3; /* Firefox */
    column-count: 3;
}
</style>

And wrap your result with this div

<div class="column3">
//Your Result
</div>
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41