Hi
I want to display results which are retrieved from the database in the following format:

1 2 3
4 5 6
7 8 9
........
.......
.....
based on the mysql_row_count() dividing the results into 3 columns and n rows.

$result1 = mysql_query("SELECT * FROM table2"); 

$row = mysql_fetch_array( $result1 );
for ($i = 0; $i < mysql_num_rows($result1); ++$i)
{
$row = mysql_fetch_row($result1);
 echo '<table cellspacing="9"><tr><td>'.$row[i].'</td><td>'.$row[i+1].'</td><td>'.$row[i+2].'</td></tr></table>
<br/><br/> 
'; 
}

I have beeeen trying but couldnt get any nearer to it..

Thanks.

Recommended Answers

All 4 Replies

Your variables are overwriting each other. And you are using the wrong variables names. The i should be $i . The method you are trying to use won't work easily either. I am typing up a solution now.

This should do it:

$result1 = mysql_query( "SELECT * FROM `tabe1`" );
$table = '<table cellpadding="3" cellspacing="0"><tr>';
if ( mysql_num_rows( $result1 ) > 0 ) {
    $i = 0;
    while( $row = mysql_fetch_assoc( $result1 ) ) {
        if ( $i == 3 ) {
            $table .= '</tr><tr>';
            $i = 0;
        }
        $table .= "<td>{$row['column_name']}</td>";
        $i++;
    }
}
else {
    $table .= '<td>No data found!</td>';
}
$table .= '</tr></table>';

echo $table;
commented: You are simply amazing God Bless You kkeith!!!!!! Thanks a Thousand Tons. +1

This should do it:

$result1 = mysql_query( "SELECT * FROM `tabe1`" );
$table = '<table cellpadding="3" cellspacing="0"><tr>';
if ( mysql_num_rows( $result1 ) > 0 ) {
    $i = 0;
    while( $row = mysql_fetch_assoc( $result1 ) ) {
        if ( $i == 3 ) {
            $table .= '</tr><tr>';
            $i = 0;
        }
        $table .= "<td>{$row['column_name']}</td>";
        $i++;
    }
}
else {
    $table .= '<td>No data found!</td>';
}
$table .= '</tr></table>';

echo $table;

:-O :idea: :ooh:
You just replied back in an hour with The EXACT Solution. I have been breaking my head for 5hrs to get at least any nearer to it. Its just a PERFECT Solution and worked like a magic.

You are simply amazing

God Bless You kkeith!!!!!!
Thanks a Thousand Tons.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.