0

I'm experiencing an issue with a for loop in the following code:

<?php
// connect to database and establish $link, okay
// log in user if possible and get $userID, okay

// if logged in, get list of records
$sql = "SELECT id,name FROM `record` WHERE user=$userID ORDER BY accessed";
$records = $link->query($sql); // returns rows successfully

if (!($records->num_rows > 0)) { // if query returns no rows
  // display button to create a record
} else {
  // if query returns rows
  for ($i = 0; $i < $records->num_rows; $i++) {
    $row = $records->fetch_assoc();
    if ($i === 0) {
      // display first existing record button which is also a css hover to display subsequent buttons
    } else if ($i === 1) {
      // display second existing record button if exists
    } else {
      // display all other existing record buttons
    }
  }
  // display button to create an additional record
}
  • If the userID is associated with 0 records in the table record, the first if displays the new record button only, as expected.
  • If the userID is associated with 2 or more records in the table record, the for loop executes as expected, displaying the records followed by the new record button, as expected.
  • If the userID is associated with 1 record, the for loop does not execute, and the additional record button is displayed.

echo $records->num_rows will display the corresponding number of records I see in phpmyadmin.

Any ideas why the for loop is not executing as expected when $records->num_rows returns 1?

  • 3
    if (!($records->num_rows > 0) { am I the only one missing a second closing bracket? – JohannesB Feb 01 '15 at 21:09
  • Missed that as retyping into the editor here, fixed. Sorry about that. – Reno Blair Feb 01 '15 at 21:13
  • So you are saying that when the num_rows==1, the loop doesn't execute once and the code inside if ($i === 0) { } doesn't get executed? – satchcoder Feb 01 '15 at 21:25
  • @satchcoder Exactly. – Reno Blair Feb 01 '15 at 21:36
  • But the loop is executed at least once? Have you tried echoing something at the beggining of each iteration? Everything seems right! Or maybe something strange with the $i, have you tried doing the comparision with only == instead of ===? – satchcoder Feb 01 '15 at 21:38
  • It does not echo anything inside the for when num_rows = 1. I do not believe it is executing at all. I have tried changing the === to ==, no change in output. I have tried changing $records->num_rows to 1 in the for, to see if that would have any effect - no change in output there either. – Reno Blair Feb 01 '15 at 21:47
  • @satchcoder Err, actually you are correct, it is running at least once! I thought I had been echoing something, and looked back. Sure enough, if I echo something before the `if`s, it is there. And the button is now in the code as well, so it looks like I'll have to dig into my style to see if it is hiding the button. Thank you so much! – Reno Blair Feb 01 '15 at 22:04

1 Answers1

0
when $records->num_rows returns 1 
then 
loop will execute for one time that is $i = 0
when $records->num_rows returns 2
then 
loop will execute for two times that is $i = 0 , $i=1
and  $i === 1 will return true and second record button will be displayed along with first records.
when $records->num_rows returns more than 2
then 
loop will execute for more than two times that is $i = 0 , $i=1 ,$i =2 ..so on
,$i===0 , $i === 1 both will return true as well as else part 
and all button will be displayed.
...
..
so.
when $records->num_rows returns 1
secod records button will not displayed.
logsv
  • 544
  • 6
  • 17