如何有效地将数据显示为数据库中每个项目的HTML? [关闭]

I have a database called products. In this DB I have a field called image_id. I would like to display each image_id in a cell of a table, with a max of 3 cells per row.

This is what I have written. I just want to make sure that I am not stressing out my server for no reason...

$HTML="<table><tr>";

$i=0;
$count=0;
@mysql_select_db($DB_DATABASE);
$query="SELECT image_id FROM produts";
$res=mysql_query($query); 
while(list($image_id)=mysql_fetch_array($res)) 
  {  
   $count++;
   $i++; 

$HTML.="<td>$image_id</td>";  
      if(($i%3)==0) 
         $HTML.="</tr><tr>"; 
  }

$HTML.="</table>";

My understanding is that this should output something like this...

<table>
    <tr>
        <td>image_id_1</td>
        <td>image_id_2</td>
        <td>image_id_3</td>
    </tr>
    <tr>
        <td>image_id_4</td>
        <td>image_id_5</td>
        <td>image_id_6</td>
    </tr>
</table>

Is my coding format acceptable or can this be done more efficiently? Thank you.

Thats exactly how one would go about it. It's the way I've done it. thats the great thing about php, you can insert html code in loops to create a dynamic html page based on the content coming in from your database.

This is why php and html go well together, you can form great dynamic pages.

You're on the right track mate. Someone may suggest a more elegant solution that does not mean your solution is false or inefficient. It's even there in the w3Schools tutorial website in the php and mysql section. So you're fine.

Goodluck!

UPDATE 1

One improvement you could do is doing some if checking and return something else if there is no data to show from the database otherwise your webpage will just show a blank page since the while loop wouldnt be executed when there is not data coming from your database.