# Is this php loop correct?

**URL:** <https://forum.kirupa.com/t/is-this-php-loop-correct/262444>\
**Category:** programming\
**Created:** [June 5, 2008, 10:44pm UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444 "2008-06-05T22:44:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![k77](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@k77](https://forum.kirupa.com/u/k77)\
**Post date:** [June 5, 2008, 10:44pm UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/1 "2008-06-05T22:44:05Z")

</div>

```php

    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;
  }

```

---

<div class="post-metadata">

**Author:** ![Raydred](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/raydred/32/65_2.png) [@Raydred](https://forum.kirupa.com/u/Raydred)\
**Post date:** [June 5, 2008, 11:47pm UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/2 "2008-06-05T23:47:44Z")

</div>

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

You probably need something like:

```php
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](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.

---

<div class="post-metadata">

**Author:** ![k77](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@k77](https://forum.kirupa.com/u/k77)\
**Post date:** [June 6, 2008, 12:31am UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/3 "2008-06-06T00:31:31Z")

</div>

Returns the T\_Else which I cannot seem to fix.

The code is part of my class

```php

  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;
  }
  }

```

---

<div class="post-metadata">

**Author:** ![k77](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@k77](https://forum.kirupa.com/u/k77)\
**Post date:** [June 6, 2008, 1:11am UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/4 "2008-06-06T01:11:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Raydred](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/raydred/32/65_2.png) [@Raydred](https://forum.kirupa.com/u/Raydred)\
**Post date:** [June 6, 2008, 4:55am UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/5 "2008-06-06T04:55:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Raghavendra](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@Raghavendra](https://forum.kirupa.com/u/Raghavendra)\
**Post date:** [June 6, 2008, 6:38am UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/6 "2008-06-06T06:38:00Z")

</div>

Hi mr raydred,

I am raghavendra from bangalore

---

<div class="post-metadata">

**Author:** ![borrob](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@borrob](https://forum.kirupa.com/u/borrob)\
**Post date:** [June 6, 2008, 11:24am UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/7 "2008-06-06T11:24:24Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [June 6, 2008, 11:38am UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/8 "2008-06-06T11:38:57Z")

</div>

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

```auto

$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
}

```

---

<div class="post-metadata">

**Author:** ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)\
**Post date:** [June 6, 2008, 12:28pm UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/9 "2008-06-06T12:28:20Z")

</div>

[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 …

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [June 9, 2008, 1:33pm UTC](https://forum.kirupa.com/t/is-this-php-loop-correct/262444/10 "2008-06-09T13:33:04Z")

</div>

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!
