wastedthelight Posted October 1, 2007 Share Posted October 1, 2007 Hi. My script works except that the newest entry inserted into the database doesn't show up. It's an empty line. It knows there's another entry and puts a blank row in the table but doesn't fill it in. Thanks. <?php session_start(); require("../include/config.php"); require("../include/functions.php"); if(isset($_SESSION['logged_in'])) { $session_username = $_SESSION['username']; // further checking... if(username_exists($session_username)) { echo ' <center> <table><tr><td> <form action="add.php" method="post" name="addsong"> <table width="600" border="0"> <tr> <td bgcolor="#000000"><span class="style3">Artist</span></td> <td bgcolor="#000000"><span class="style3">Song</span></td> <td bgcolor="#000000"><span class="style3">Section</span></td> <td bgcolor="#000000"><span class="style3">Section Number</span></td> <td bgcolor="#000000"><span class="style3">Requested?</span></td> <td bgcolor="#000000"> </td> </tr> <tr> <td><input name="artist" type="text" /></td> <td><input name="song" type="text" /></td> <td><select name="section"> <option value="HM">HM</option> <option value="IND">IND</option> <option value="NEW">NEW</option> <option value="R&R">R&R</option> </select></td> <td><input name="sectionnumber" type="text" /></td> <td><select name="requested"> <option value="0" selected>No</option> <option value="1">Yes</option> </select></td> <td><input name="submit" value="Add" type="submit" /></td> </tr> <tr><td colspan="6" bgcolor="000000"></td></tr> </table> </form> </center> '; echo " <div align=\"center\"> <table class=\"listfont\" width=\"700\" border=\"1\" bordercolor=\"#000000\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"> <tr bgcolor=\"c0c0c0\"> <td align=\"center\"> </td> <td align=\"center\"> </td> <td align=\"center\"> Artist </td> <td align=\"center\"> Song </td> <td align=\"center\"> Section </td> <td align=\"center\" width=\"30\"> Req. </td> </tr> "; $playlist = mysql_query("SELECT * FROM playlist WHERE dj = '$session_username' ORDER by auto DESC"); $numofrows = mysql_num_rows($playlist); if(mysql_num_rows($playlist) > 0) { //create a loop, because there are rows in the DB while($row = mysql_fetch_assoc($playlist)) { $auto = $row['auto']; $artist = $row['artist']; $song = $row['song']; $section = $row['section']; $sectionnumber = $row['sectionnumber']; $requested = $row['requested']; for($i = 0; $i < $numofrows; $i++) { $rows = mysql_fetch_array($playlist); //get a row from our result set if($i % 2) { //this means if there is a remainder echo "<TR bgcolor=\"yellow\">\n"; } else { //if there isn't a remainder we will do the else echo "<TR bgcolor=\"white\">\n"; } echo " <td> <a href=\"edit.php?auto=$auto\">Edit</a></td> <td> <a href=\"delete.php?auto=$auto\">Delete </a></td> <td>1 ".$rows['artist']."</td> <td>2 ".$rows['song']."</td> <td>3 ".$rows['section']." ".$rows['sectionnumber']."</td><td> "; if ($rows['requested'] != '0') { echo "<img src=\"images/checkmark.jpg\">"; } else echo " xx</td></tr>"; } echo "</td></tr>"; } echo ' </table> </div> '; } else { echo 'No recent playlist. Please add some.'; } } else { echo '<b>Sorry, your session username doesnt exist</b>.'; } } else { echo 'You must be logged in to add to the playlist.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71379-solved-for-loop-broken-first-entry/ Share on other sites More sharing options...
BlueSkyIS Posted October 1, 2007 Share Posted October 1, 2007 because you're pulling 2 records off right away: while ($row = mysql_fetch_assoc($playlist)) { // You pulled off first row and then for($i = 0; $i < $numofrows; $i++) { $rows = mysql_fetch_array($playlist); // You ALREADY got first row from your result set above. Do everything in one loop. There is no need for a for() loop inside the while() loop. ... Alternatively, there is no need for a while() loop outside the for() loop.... Quote Link to comment https://forums.phpfreaks.com/topic/71379-solved-for-loop-broken-first-entry/#findComment-359208 Share on other sites More sharing options...
wastedthelight Posted October 1, 2007 Author Share Posted October 1, 2007 because you're pulling 2 records off right away: while ($row = mysql_fetch_assoc($playlist)) { // You pulled off first row and then for($i = 0; $i < $numofrows; $i++) { $rows = mysql_fetch_array($playlist); // You ALREADY got first row from your result set above. Do everything in one loop. There is no need for a for() loop inside the while() loop. ... Alternatively, there is no need for a while() loop outside the for() loop.... It works! Thanks! Pardon my noobness. Quote Link to comment https://forums.phpfreaks.com/topic/71379-solved-for-loop-broken-first-entry/#findComment-359216 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.