Hello Everyone, I am trying to retrieve names from my database. I am using a While Loop, however I do not want to display the names in one long list. I want to break them up into 3 columns and use a table, so it should be broken up into 3 <td>'s. I have this script but I don't know how to integrate an html table with it. Does anyone have any ideas? Thank you, Jason <?php $something = mysql_query("SELECT * FROM somewhere"); $column_count = 3; $i = 1; while ($row = mysql_fetch_array($something)) { if (is_int($i/$column_count)) { echo $row['your_value'], "<br>"; } else { echo $row['your_value'], " - "; // remove - symbol if you don't want to split columns } $i++; } ?> PHP:
Echo '<table><tr>' before starting your loop; echo '<td>your return query</td>' within loop and '</tr></table>' after loop.
Well I know how to make a table, but this script is confusing and I dont know exactly where to do the <td>
It is a little more work than that because you want to make sure that if the last row isn't complete, you do a colspan. <?php $something = mysql_query("SELECT * FROM somewhere"); $column_count = 0; $i = 1; echo'<table><tr>'; while ($row = mysql_fetch_array($something)) { if ($column_count ==3) { echo'</tr><tr><td>'; echo $row['your_value']; echo'</td>'; $column_count=0; } else { echo'<td>'; echo $row['your_value']; // remove - symbol if you don't want to split columns echo'</td>'; } $i++; } if($column_count > 0){ $colspan=(3-$column_count); echo'<td colspan="'.$colspan.'"> </td>'; } echo'</tr></table>'; ?> PHP:
Try this: <?php $something = mysql_query("SELECT * FROM somewhere"); $column_count = 3; $i = 1; echo '<table>'; while ($row = mysql_fetch_array($something)) { if ($i == 1) { echo '<tr>'; } echo "<td>".$row['your_value']."</td>"; $i++; if ($i > $column_count) { $i=1; echo '</tr>'; } } echo '</table>'; ?> PHP: - ads2help
It isn't NEEDED, but you should add a check at the end to do a colspan if needed to make your markup valid.
Hello, Well as you want to show 3 records per column. You can do like this <table width="120" border="0" cellpadding="5" cellspacing="0"> <?php $selectQuery = "Your Sql Statement'"; $resultQuery= query($selectQuery ); $numRows = numRows($resultQuery); if($numRows =="") { ?> <tr> <td class="txts" style="padding:5px;"><?php echo ("Record not available");?></td> </tr> <tr> <?php } else { $count=0; while($rowQuery=mysql_fetch_array($resultQuery)) { if($count % 3==0) echo "</tr><tr>"; ?> <td style="padding-bottom:10px;padding-top:10px;" valign="top" class="tabs"><table width="100" border="0" cellspacing="0" cellpadding="0" style="border:1px #cccccc solid';"> <tr> <td width="100" align="center"><table width="110" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="30" align="center "><?php echo $rowQuery['recordName']?> </td> </tr> </table></td> </tr> </table></td> <?php $count++; } } ?> </tr> </table> PHP: We hope it works for you Regards, Stylesofts Developing Team