GFXUniverse Posted November 10, 2009 Share Posted November 10, 2009 Hi, guyz, this is what m loking for, for example, i have a table in mysql database with only one column and this is what i usually do to display data (m not writing the actual code, just typing simple words) <table> <?php while ($row = mysql_fetch_array($sql)) { ?> <tr> <td> <?php echo $row['record']; ?><br> </td> </tr> <?php } ?> </table> Result: Recored1 Recored2 Recored3 . .so on But I want to display data in table like this in rows, i mean, first 4 records must be displayed in first row using 4 colums, (and only 4 records in a row) Please help me! Quote Link to comment https://forums.phpfreaks.com/topic/180933-help-me-to-display-mysql-data-in-website-like-as-i-want/ Share on other sites More sharing options...
joel24 Posted November 10, 2009 Share Posted November 10, 2009 <table> <?php $rowcount = 0; while ($row = mysql_fetch_array($sql)) { if ($rowcount % 4 == 0) { echo '<tr>'; } ?> <td> <?php echo $row['record']; ?><br> </td> <?php if ($rowcount % 4 == 0) { echo '</tr>'; } $rowcount++; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/180933-help-me-to-display-mysql-data-in-website-like-as-i-want/#findComment-954549 Share on other sites More sharing options...
GFXUniverse Posted November 10, 2009 Author Share Posted November 10, 2009 no working look the Result is: my exact code: <table border="1" align="center" cellpadding="5" > <?php $rowcount = 0; while($rows = mysql_fetch_array($result)) { if ($rowcount % 4 == 0) { echo '<tr>'; } ?> <td width="100" height="50"> <?php echo $rows['name']; ?><br> </td> <?php if ($rowcount % 4 == 0) { echo '</tr>'; } $rowcount++; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/180933-help-me-to-display-mysql-data-in-website-like-as-i-want/#findComment-954561 Share on other sites More sharing options...
Philip Posted November 10, 2009 Share Posted November 10, 2009 Did you take a look at the source on the outputted page? You might spot the problem there Quote Link to comment https://forums.phpfreaks.com/topic/180933-help-me-to-display-mysql-data-in-website-like-as-i-want/#findComment-954564 Share on other sites More sharing options...
GFXUniverse Posted November 10, 2009 Author Share Posted November 10, 2009 with the help of joel24's code i tried to modify this code little bit, and it worked now!!! here is the code <table border="0" align="center" cellpadding="5" > <?php $tr = 1; $td = 4; while($rows = mysql_fetch_array($result)) { if ($tr == 1) { echo '<tr>'; } ?> <td> <?php echo $rows['name']; ?><br> </td> <?php if ($tr == 4) { echo '</tr>'; $tr = 1; } else { $rowcount++; } } ?> </table> with table border = 1, it looks %100 correct only if database fills all the cells in the table otherwise i have to keep the table border = 0. thats fine. Thanks man for giving me little time, and guyz please tell me if any other improvement needed. Quote Link to comment https://forums.phpfreaks.com/topic/180933-help-me-to-display-mysql-data-in-website-like-as-i-want/#findComment-954571 Share on other sites More sharing options...
GFXUniverse Posted November 10, 2009 Author Share Posted November 10, 2009 Sorry the old code was incorrect. this one is correct <table border="0" align="center" cellpadding="5" > <?php $$columns= 4; $r = 1; while($rows = mysql_fetch_array($result)) { if ($tr == 1) { echo '<tr>'; } ?> <td> <?php echo $rows['name']; ?><br> </td> <?php if ($tr == $columns) { echo '</tr>'; $tr = 1; } else { $tr++; } } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/180933-help-me-to-display-mysql-data-in-website-like-as-i-want/#findComment-955143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.