I am trying to create an HTML table from a MySQL query. Here is the format of the table I'm trying to create 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 php. Could you help me understand what I am doing wrong with the code and point me in the right direction? or please correct my code according to my table.

+4
source share
2 answers

first you need to convert your request to an 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 uses if (isset ($ db [$ j])) to make sure this code will work, but if you know that you have 6 lines in your db, you do not need to use this

+1
source

Sort of

<?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: PDO, .

+1

All Articles