views:

20

answers:

1

Hi everyone, I'hv got a problem when I am using foreach in smarty, an array with 2 item was loop in a foreach, but the result is it loop 3 time.

I use doctrine to get a list of review by a user from database

Doctrine_Core::getTable('review')->findByUser($userId);

then I assign it to smarty and loop in foreach:

{foreach from=$reviewList item=review}

  <p>User {$review.User.name} said: {$review.content}</p>

{/foreach}

However the result is e.g.:

User Joe said: yoyo

User Mary said: hihi

User said:

Please notice that the extra row doesn't get anything from the array.

I have checked that there is only 2 record in database, and I have count the $reviewList by count($reviewList), the result is also 2. When I insert one more record to database, the forloop also loop extra one time. Can anyone tell me why this happen? Thanks a lot!

+1  A: 

This should filter the empty line:

{foreach from=$reviewList item=review}{if $review.User.name}
  <p>User {$review.User.name} said: {$review.content}</p>
{/if}{/foreach}
JochenJung
It work! Thank a lot!!
saili