在if语句中嵌入foreach的代码问题

时间:2015-07-26 19:17:17

标签: php

我试图在IF语句中嵌入一个foreach语句,如下所示。我得到了一个意想不到的T_ELSE"在" ELSE"条款是。我错过了语法吗?

if ($user_ID == $row7[0]['wp_users.ID']){

foreach ($row7 as $result) {
echo "<br>" . $result['text'] . "&nbsp;&nbsp;<em>(by: <a 
href=\"http://example.com/port?ID=$result[ID]\">" . $result['display_name']
. "</a>" . ")</em>" . "{" . ($user_ID == $row7[0]['wp_users.ID']) ?
"&nbsp;|&nbsp;<a href=\"http://example.com/edit? 
text=$the_SID&cat=$result[category]\">Edit this text</a><br>" : '') . "}";

   }

else {

foreach ($row7 as $result) {
echo "<br>" . $result['text'] . "&nbsp;&nbsp;<em>(by: <a
href=\"http://example.com/port?ID=$result[ID]\">" . $result['display_name']
. "</a>" . ")</em>";
   }

}

1 个答案:

答案 0 :(得分:1)

if ($user_ID == $row7[0]['wp_users.ID']) {

    foreach ($row7 as $result) {

        $verify= '&nbsp;|&nbsp;<a href="http://example.com/edit? text=' . $the_SID . '&cat=' . $result[category] . '">Edit this text</a><br>';
        $UserIdCorrect = ($user_ID == $row7[0]['wp_users.ID']) ? $verify : '';

        echo '<br>' . $result['text'] . '&nbsp;&nbsp;<em>(by: <a href="http://example.com/port?ID=$result[ID]">' . $result['display_name'] . '</a>' . ')</em>' . "{" . $UserIdCorrect . "}";

    }

} else {

        foreach ($row7 as $result) {
            echo "<br>" . $result['text'] . "&nbsp;&nbsp;<em>(by: <a href=\"http://example.com/port?ID=$result[ID]\">" . $result['display_name']
                . "</a>" . ")</em>";
        }

}