0

I am currently working on a loop to display items from a mysql table. Is there a simple way to display 3 items per row. So far I managed to display all the items in a single row inside an html table . I would appreciate any help; code (without html table tags) below:

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
$database_name = "vog";
$conn = mysql_connect("localhost","root","toor");
mysql_select_db($database_name);

$sql = "select * from client1";

$result = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($result); //Ελεγχος αν υπάρχουν εγγραφές!
?>

<?php
    if($num){
       while ($row = mysql_fetch_array($result))
    {
           echo $img_id = $row['img_id'];
 ?>

<form name="add2cart" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <?php echo $row['img_name'];?>
        <?php echo "<img src=".$row['img_path'].">";?>
<select name="color">
    <option>bnw</option>
    <option>sepia</option>
</select>
<input type="hidden" name="img_name" value="<?php echo $row['img_name']; ?>"> 
<input type="submit" name="add2cart" value="Add to cart"></input>
        <br />
</form> 
<?php
        } 
    }
    else{
        echo "Δεν υπάρχουν εγγραφές με τα κριτήρια που επιλέξατε";
    }
    //add2cart section
if(isset($_POST['add2cart'])){   
$img_name = $_POST['img_name'];
$color = $_POST['color'];
$sql_order ="insert into orders(item_id, img_name, color) 
values(' ', '$img_name',    '$color')";
$result = mysql_query($sql_order);
}
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
vsapountzis
  • 618
  • 3
  • 11
  • 28
  • 1
    Please look into using `mysqli` rather than the `mysql_` functions. They're old and will open you up to a whole world of hurt. See: http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php – Polynomial Dec 05 '11 at 12:20

1 Answers1

4

Declare a variable before your loop like: $currentRow = 1 and then inside, and at the end, of your loop add $currentRow++

You can then check if your row is divisible by 3 if($currentRow % 3 == 0) and put a break in.

Prisoner
  • 27,391
  • 11
  • 73
  • 102