Jump to content

too weird $i increment


dianikol85

Recommended Posts

Hi to all. I have this function to display a list from a db and then a weird thing is happening. It increases the $i counter 2 steps instead of 1...

 

The sql query runs ok in phpadmin. Then I echo the $i counter within the for loop and it prints 0,2,4....

 

Any suggestions???

 

By the way i don't use mysqli. Boss's choise. I can't help it, hehe. and the language in the <th> is greek so don't bother...

 

 

function getCustomersList()
{
$sql = "select USER_ID, USER_NAME, USER_LNAME, USER_MAIL, USER_ADDRESS from user where USER_GROUP = '2' order by USER_ID desc limit 5";
$res = mysql_query($sql);
$num_rows = mysql_num_rows($res);

if ($num_rows > 0)
{
	echo "<table cellpadding='0' cellspacing='0' class='table_info'>
			<thead>
				<th>Κωδ. πελάτη</th>
				<th>Ονοματεπώνυμο</th>
				<th>Διεύθυνση</th>
				<th>E - mail</th>
			</thead>
			<tbody>";

	for ($i = 0; $i < $num_rows; $i++)
	{echo $i;
		$row = mysql_fetch_array($res);

		echo "<tr>";

		echo "<td><a href='users/edit_user.php?USER_ID=$row[uSER_ID]>$row[uSER_ID]</a></td>";			
		echo "<td>$row[uSER_NAME] $row[uSER_LNAME]</td>";
		echo "<td>$row[uSER_ADDRESS]</td>";
		echo "<td>$row[uSER_MAIL]</td>";

		echo "</tr>";
	}	


	echo "	</tbody>
		  </table>";
}
else
{
	echo "<div id='empty'>Δεν υπάρχουν καταχωρημένοι πελάτες</div>";
}
}

Edited by dianikol85
Link to comment
Share on other sites

I'm not sure why you the $i variable is incrementing twice -- I'm not seeing any code that should do that. However, maybe adjust your code to use a while loop, rather than a for loop. Maybe something like this: (not tested, since I obviously don't have your database setup in place to check this)

 

function getCustomersList()
{
       $sql = "select USER_ID, USER_NAME, USER_LNAME, USER_MAIL, USER_ADDRESS from user where USER_GROUP = '2' order by USER_ID desc limit 5";
       $res = mysql_query($sql);
       $num_rows = mysql_num_rows($res);

       if ($num_rows > 0)
       {
               echo "<table cellpadding='0' cellspacing='0' class='table_info'>
                               <thead>
                                       <th>Κωδ. πελάτη</th>
                                       <th>Ονοματεπώνυμο</th>
                                       <th>Διεύθυνση</th>
                                       <th>E - mail</th>
                               </thead>
                               <tbody>";

               while($row = mysql_fetch_array( $res )) {

                       echo "<tr>";

                       echo "<td><a href='users/edit_user.php?USER_ID=$row[uSER_ID]>$row[uSER_ID]</a></td>";                   
                       echo "<td>$row[uSER_NAME] $row[uSER_LNAME]</td>";
                       echo "<td>$row[uSER_ADDRESS]</td>";
                       echo "<td>$row[uSER_MAIL]</td>";

                       echo "</tr>";
               }       


               echo "  </tbody>
                         </table>";
       }
       else
       {
               echo "<div id='empty'>Δεν υπάρχουν καταχωρημένοι πελάτες</div>";
       }
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...