简体   繁体   中英

I am trying to generate an HTML table from a MySQL query. Here is the format of the table I am trying to generate from a MySQL query:

|------------------ ------------------- -------------------|
|      Name 1      |      Name 2       |     Name 3        |
|------------------| ------------------|-------------------|
| IMAGE1 | IMAGE1  |   IMAGE2 | IMAGE2 | IMAGE3 | IMAGE3   |  
|----------------- | ------------------|-------------------|
|      Name 4      |      Name 5       |     Name 6        |
|------------------| ------------------|-------------------|
| IMAGE4 | IMAGE4  |   IMAGE5 | IMAGE5 | IMAGE6 | IMAGE6   |  
|----------------- | ------------------|-------------------|

This is my php script

    <?php
    include_once("abc.php");

    $query=mysql_query("select * from dbts LIMIT 6");
    echo'<table>';
    $i=0;
    while($sam=mysql_fetch_array($query))
    {
    $image = $sam['image'];
    $name= $sam['name'];


    if($i==0) 
       {
       echo '<tr>';
       }  

    echo '<td width=180 border=1 COLSPAN=2>'; print"$name"; echo '</td>';
      if($i==2)
        {
     echo '</tr>';
        $i=-1;
        }
      $i++;


    if($i==0) 
       {
       echo '<tr>';
       }  
    echo '<td width=90>'; print"<img src=$image width=90 height=100/>"; echo '</td>';
    echo '<td width=90>'; print"<img src=$image width=90 height=100/>"; echo '</td>';
      if($i==2)
        {
     echo '</tr>';
        $i=-1;
        }
      $i++;
    }
    echo '</table>';
    ?>

Above is the table i want from the php, Can you please help me to understand what I am doing wrong with code and point me in the right direction? or please correct my code according to my above table.

at first you have to convert your query to array

include_once("abc.php");

$query = mysql_query('select * from dbts LIMIT 6');
$db = array();
while($row = mysql_fetch_array($query))
    $db[] = $row;

then

echo'<table>';
$i=0;
for($i = 0; $i <= count($db); $i+=3){
    echo '<tr>';
    for($j = $i; $j < $i + 3; $j++)
        if(isset($db[$j]))
            echo '<td width="180" border="1" COLSPAN="2">' . $db[$j]['name'] . '</td>';
    echo '</tr>';

    echo '<tr>';
    for($j = $i; $j < $i + 3; $j++){
        if(isset($db[$j])){
            echo '<td width="90">' . $db[$j]['image'] . '</td>';
            echo '<td width="90">' . $db[$j]['image'] . '</td>';
        }
    }
    echo '</tr>';
    echo $i;
}
echo '</table>';

i used if(isset($db[$j])) to be sure this code will work, but if you know you have 6 rows in your db you don't have to use this

Something like

<?php 
include_once("abc.php");

$query=mysql_query("select * from dbts LIMIT 6");
$i  = 0;
echo '<table><tr>';
while ($sam=mysql_fetch_array($query)) {
    if ($i > 0 && $i % 3 == 0) { 
    // Theres a better way to do this, but its not coming to me right now...
        echo '</tr><tr>';
    }
    echo "
    <td>
        <table>
            <tr>
                <td colspan=\"2\">{$sam['name']}</td>
            </tr>
            <tr>
                <td>{$sam['image']}</td>
                <td>{$sam['image']}</td>
            </tr>
        </table>
    </td>";
    $i++;
}
echo '</tr></table>';
?>

err: also please add a lot more error checking and look into PDO as others have suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM