tekkenfan2 Posted June 30, 2014 Share Posted June 30, 2014 (edited) Ok, so I have some code that takes the records in my database and outputs them in a table, but currently after the first record it starts spacing them incorrectly Here is the code <?php require_once('config.php'); require_once('menu.php'); echo '<h1>View All Alien Interactions</h1>'; echo '<table> <tr>'; foreach($fields AS $label){ echo "<th>{$label}</th>"; } echo '<th>Edit</th><th>Delete</th>'; echo '</tr>'; $fields_str = '`contact_id`, `'.implode(array_keys($fields), '`, `').'`'; $sql = "SELECT {$fields_str} FROM `alien_abduction`"; foreach($dbh->query($sql) as $row) { echo '<tr>'; foreach($fields AS $field=>$value){ echo '<td>'.(isset($row[$field]) && strlen($row[$field]) ? $row[$field] : ' '.'</td>'); } echo '</tr>'; echo '<td><a href="edit.php?contact_id='.$row['contact_id'].'">Edit</a></td>'; echo '<td><a href="delete.php?contact_id='.$row['contact_id'].'">Delete</a></td>'; echo '</tr>'; echo '</table>'; } ?> I just want it to start a new line after importing each record This is a picture of what its curently doing, look at the second row, it just keeps adding all additional entries on this line (I whited out personal info) Edited June 30, 2014 by tekkenfan2 Quote Link to comment https://forums.phpfreaks.com/topic/289346-simple-table-formatting-question/ Share on other sites More sharing options...
ginerjm Posted June 30, 2014 Share Posted June 30, 2014 YOu are checking if a field in your result row exists when you know it will be there. Simply echo the row fields. If it's blank you'll output that field in its proper place without shifting other ones in its place. Quote Link to comment https://forums.phpfreaks.com/topic/289346-simple-table-formatting-question/#findComment-1483425 Share on other sites More sharing options...
Ch0cu3r Posted June 30, 2014 Share Posted June 30, 2014 Remove the closing </tr> tag directly after the foreach loop, this is probably what is causing it. If all you are doing is dumping the data in $row into <td> tags, then you can write while loop as. while($row = mysql_fetch_row($result) { echo ' <tr> <td>' . implode('</td><td>', $row) . '</td> <td><a href="edit.php?contact_id='.$row['contact_id'].'">Edit</a></td> <td><a href="delete.php?contact_id='.$row['contact_id'].'">Delete</a></td> </tr>'; } Quote Link to comment https://forums.phpfreaks.com/topic/289346-simple-table-formatting-question/#findComment-1483426 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.