Is this php loop correct?


    while ($row = $result->fetch_assoc()) 
    {
        $myList .= '<li id="' . htmlentities($row['id']) . '">' . 
                 htmlentities($row['description']) . '</li>';
    } else {
        echo "NO RECORDS HAVE BEEN SAVED ADD SOME RECORDS USING THE FORM ABOVE";
    // return the list
    return $myList;
  }

It doesn’t look right. My Php, is reeaallly rusty, but i believe i can still help…

You probably need something like:

if ($result = $mysqli->query($query)) 
{
 while ($row = $result->fetch_assoc()) 
    {
        $myList .= '<li id="' . htmlentities($row['id']) . '">' . 
                 htmlentities($row['description']) . '</li>';
    }

     // return the list
    return $myList;

} else {
        echo "NO RECORDS HAVE BEEN SAVED ADD SOME RECORDS USING THE FORM ABOVE";
}

ps. i used this as a reference (http://www.weberdev.com/Manuals/PHP/function.mysqli-fetch-assoc.html). I may have gotten the “$Result” line in the If Statment wrong. But the Logic is there.

You want to check to see if the DB returned anything, if so, then Loop thru them.

Returns the T_Else which I cannot seem to fix.

The code is part of my class


  public function BuildTasksList()
  {
    // initialize output
    $myList = '';
    // build query
    $result = $this->mMysqli->query('SELECT * FROM tasks ' .
                                    'ORDER BY order_no ASC');
    // build task list as <li> elements

    while ($row = $result->fetch_assoc())
    {
		$myList .= '<li id="' . htmlentities($row['id']) . '">' .
                 htmlentities($row['description']) . '</li>';
    } else {
		echo "NO RECORDS HAVE BEEN SAVED ADD SOME RECORDS USING THE FORM ABOVE";
    // return the list
    return $myList;
  }
  }

Ah, basically I have said while your looping all records if are no records print no records text and no one can do it. PHP for you. NET would have done it 2seconds

[QUOTE=k77;2337719]Ah, basically I have said while your looping all records if are no records print no records text and no one can do it. PHP for you. NET would have done it 2seconds[/QUOTE]

Yep! thats why my Php is rusty. I’m a .net guy. :wink:

Hi mr raydred,

I am raghavendra from bangalore

you can do it like:

$row = $result->fetch_assoc();
if( ! $row )
{
$myList .= ‘<li id="’ . htmlentities(-1) . '">NO RECORDS HAVE BEEN SAVED ADD SOME RECORDS USING THE FORM ABOVE</li>";
}
while ( $row )
{
$myList .= ‘<li id="’ . htmlentities($row[‘id’]) . ‘">’ .
htmlentities($row[‘description’]) . ‘</li>’;
}
return $myList;

You can’t have an ‘ELSE’ after a while - a while is a loop, you can only use ‘else’ after an ‘if’ control structure

You are almost spot on with the code you posted but you still put an else after the while

Like Raydred said, check your $result var to see if it had rows, if not then post the message, otherwise loop

You can use mysql_num_rows to find out how many rows were returned from a SQL SELECT


$result = $this->mMysqli->query('SELECT * FROM tasks ORDER BY order_no ASC');
 
if (mysql_num_rows($result) > 0) {
    // build task list as <li> elements
 
    while ($row = $result->fetch_assoc())
    {
        $myList .= '<li id="' . htmlentities($row['id']) . '">' .
                 htmlentities($row['description']) . '</li>';
    }
} else {
  // Couldn't find any rows
}

[QUOTE=k77;2337719]and no one can do it. PHP for you. NET would have done it 2seconds[/QUOTE]Don’t blame it on php you’re the developer who did it wrong …

I think it helps to have exposure to both technologies and more. .NET is lazy with syntax and does a lot more for you in terms of formatting and correcting basic programming errors such as unused references or uninitialised variables, but syntax and simple errors are what got us here in the first place and gave us all keen eyes.

It also teaches you to be appreciative of what you’ve got, because it took a lot of time and effort for people to develop the technologies we use today!