Friday, October 21, 2011

Display data horizontally from MySQL table


It’s not a tough job. You can see that the records are pulling up and displaying 2 records in a row and after that in the next rows for the next 2 records. The code for such display style is as follows:
Email1Email2
Email3Email4
Email5Email6
It’s not a tough job. You can see that the records are pulling up and displaying 2 records in a row and after that in the next rows for the next 2 records. The code for such display style is as follows:
1
2
3
4
<?php
mysql_connect ('localhost','root','pwdadmin');
mysql_select_db ('test');
 ?>
1
2
<table>
<tr>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
$sql_result = mysql_query ("SELECT email FROM members");
$record_count = 0;  //Keeps count of the records echoed.
while ($row=mysql_fetch_row($sql_result))
{
    //Check to see if it is time to start a new row
    //Note: the first time through when
    //$record_count==0, don't start a new row
    if ($record_count % 2==0 && $record_count != 0)
    {
        echo '</tr><tr>';
    }
    echo '<td>';
    //Echo out the entire record in one table cell:
    for ($i=0; $i< count($row); $i++)
    {
        echo $row[$i];
    }
    echo '</td>';
    //Indicate another record has been echoed:
    $record_count++;
}
?>
1
2
</tr>
</table>