Search code examples
phpsmarty

PHP / Smarty - Identifying an empty "foreach"


Here is my current code:

{php} foreach($result as $key=>$value)

{ echo "<div class='internalpadding'>";

{/php}

This is a test.

{php} } {/php}

This code is displaying "This is a test." a specific amount of times in a row - one time for each post a user has in the database. That part is working perfectly.

However, if a user has no posts, there's simply nothing displayed. This makes sense, since it should just be displaying text for posts that do exist. But is there a way I can make it give some type of message like "There are no posts for this user" if nothing is found using foreach?


Solution

  • if ($result == null)
     echo "There are no posts for this user" ;
    else {
           foreach($result as $key=>$value)
    
             { echo "<div class='internalpadding'>";
               echo "This is a test" ; }
    }