使用PHP在三列html表中显示MySql表的三条记录

使用PHP在三列html表中显示MySql表的三条记录

问题描述:

I Need to show three records of MySql table in three different column using php.My Query is

SELECT * FROM TABLE1 WHERE Id IN(1,2,3)

I want to show the result as here enter image description here

How can i Write LOOP for it?like

while(loop condition)
{
//what will go here?

}

UPDATE: First row fields will show in first column of html table and second record fields will display in second column and so on...I am not asking only show three records

我需要使用php在三个不同的列中显示MySql表的三条记录。我的查询是 p> \ n

  SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
  code>  pre> 
 
 

我想在此处显示结果 p>

如何为其编写LOOP? p>

  while(循环条件)
 {
 //将会发生什么?
 
} 
  code>  pre> 
 
  

UPDATE: strong>第一行字段将显示在html表的第一列中,第二行记录字段将显示在第二列中,依此类推......我不是只要求显示三条记录 p> div>

OP saying, it's not so simple. But it is.

So, you have 2 ways to do it.

First. In this case, you are loop through on the 3 columns. Fetch the first row. This put all the data into a div. Class name is column_1. Do it for the other 3. Then floating the divs to left to each other.

$i = 1;
while($row = $db->fetch_row()) {
    ?>
<div class="column_<?php echo $i; ?>">
    <div class="picture">
        <?php echo $row["image"]; ?>
    </div>
    <div class="description">
        <?php echo $row["desc"]; ?>
    </div>
    ... and so on...

</div>
<?php
    $i++;
}

Second one, when you first collect the data about 3 rows, and then put them into a table rows by row.

<?php
    while($row = $db->fetch_row()) {
        $results[] = $row;
    }
?>
<table>
    <tr>
        <td><?php echo $result[0]['image'] ?></td>
        <td><?php echo $result[1]['image'] ?></td>
        <td><?php echo $result[2]['image'] ?></td>
    </tr>
    <tr>
        <td><?php echo $result[0]['desc'] ?></td>
        <td><?php echo $result[1]['desc'] ?></td>
        <td><?php echo $result[2]['desc'] ?></td>
    </tr>

</table>

EDIT

I forgot that, there is a third solution. You can just build the table empty, and then you can update the cells with an ajax call with jQuery.

One way of looping through them is foreach()

assuming you have your results in $results array:

foreach($results as $result) {
   //create your table
   $result['id']; //has the item id
   $result['title']; //has item title
   //and so on...
}

HERE is a great tutorial on looping through mysql result sets :D (W3Schools)

Another one HERE

To provide a answer to your comment you must understand how HTML tables work...
<tr> = table row
<td> = table data

You are asking for an entire source code, and this is NOT that place, we don't do your job for you, but if you want, you will have to pay me :) and I am not sure that you agree with this :)

HERE is a good and easy to understand tutorial on HTML tables.

while ($data=mysql_fetch_array($rs)) {

}