3
|------------------ ------------------- -------------------|
|      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.

j08691
  • 204,283
  • 31
  • 260
  • 272
matrix
  • 55
  • 6
  • 2
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 21 '15 at 16:42
  • 1
    What's wrong with the code you have? How are the results incorrect? We don't know what the problem is. – Andy Lester Jan 21 '15 at 16:43
  • 1
    Your issue is your `$i == ?` statements are not quite correct, and you're using the same `$i` for both inner and outer rows. Since your sql is returning name and image1 and image2, then you'll want to probably draw that whole box at once and for that you might want to consider a small table print all that at once in its own `` then every `$i%3==0` add the start/end `` tags instead in the bigger ``
    – Dave Goten Jan 21 '15 at 16:43
  • @DaveGoten can u edit my code i will really thankful toward you. – matrix Jan 21 '15 at 16:47

2 Answers2

1

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

Kiyan
  • 2,155
  • 15
  • 15
1

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.

Dave Goten
  • 701
  • 4
  • 13