Php 获取foreach循环外部的行,但在其中进行比较(smarty模板)

Php 获取foreach循环外部的行,但在其中进行比较(smarty模板),php,foreach,smarty,smarty3,Php,Foreach,Smarty,Smarty3,这是我的问题-我有一个带有评论的页面,看起来像这样: {foreach from=$comments item=row} Here is the body of comment and like/unlike button {/foreach} - Query to retrieve all the comments from DB - foreach loop to show them, like that: foreach ($stmt as $comment) { - another q

这是我的问题-我有一个带有评论的页面,看起来像这样:

{foreach from=$comments item=row}
Here is the body of comment and like/unlike button
{/foreach}
- Query to retrieve all the comments from DB
- foreach loop to show them, like that:
foreach ($stmt as $comment) {
- another query inside of the foreach loop to find out data of users who 
 already liked this comment.
 - then if/else statement to choose which button to show
}
我需要检查用户是否已经喜欢该评论,并取决于“显示喜欢/不喜欢”按钮。通常在php中,我会这样做:

{foreach from=$comments item=row}
Here is the body of comment and like/unlike button
{/foreach}
- Query to retrieve all the comments from DB
- foreach loop to show them, like that:
foreach ($stmt as $comment) {
- another query inside of the foreach loop to find out data of users who 
 already liked this comment.
 - then if/else statement to choose which button to show
}
但由于我使用的是smarty,我无法在foreach循环中执行任何查询,因为它是模板,不允许使用任何逻辑代码。我需要检查的是:

{foreach from=$comments item=row}
{if {$comment_id} != {$row.id} && {$user_liked} != $user_id}
show like button
{/if}
{if {$comment_id} == {$row.id} && {$user_liked} == $user_id}
show dislike button
{/if}
{/foreach}
我的数据库结构,以便您理解这些行的含义:

Table `liking`:
-comment_id
-user_liked

Table `comments`:
-id
And $user_id is actually $_SESSION['user_id'];
所以问题是: 如何从带有注释的foreach循环外部的
liking
表中获取
comment\u id
user\u liked
行,以便在循环内部与注释进行比较?
我试着把它们全部取出来,并在comments-foreach循环中使用另一个foreach循环来获取它们,但它就是不能正常工作
Smarty
引擎很酷,但说到这些事情,它确实令人失望。。。谢谢所有帮助我的人,我很高兴听到任何建议

在获取注释talbe时,可以在php代码中获取liking表。 例如:

在smarty:

{foreach from=$comments item=row} {如果$row.liked==0} 显示类似按钮 {else} 显示不喜欢按钮 {/if}
{/foreach}

谢谢你的回答!但是现在我得到的是-
警告:非法字符串偏移量“喜欢”
您必须检查php代码,喜欢的字段没有分配给smarty。您能在这里粘贴更多代码吗?