Jump to content

[SOLVED] For Loop: Broken first entry


wastedthelight

Recommended Posts

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.  ;D

 

<?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.';
}
?>

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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!  ;D  Pardon my noobness.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.