如何在if中包含if语句


How to have a if statment inside if

我的论坛中有两个级别Admin=1和User=0。然后这段代码会让管理员获得红色名称。

 if($info['rank'] == 1) { $x= "<a class='admin'>".$last_user."</a>"; 
            } else { $x= "<a class='user'>".$last_user."</a>"; }

但是每个人的名字都红了。。

在您想要的地方使用$x。

<?PHP 
if (mysql_num_rows($res) > 0) {
    while ($row = mysql_fetch_assoc($res)) {

        if($info[rank] == 1) { $x= "<div class='admin'>".$last_user."</div>"; } else { $x="<div>".$last_user."</div>"; }
        $tid = $row['id'];
        $cid = $row['category_id'];
        $title = $row['topic_title'];
        $views = $row['topic_views'];
        $date = $row['topic_date'];
        $creator = $row['topic_creator'];
        if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
        if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
        $topics .="
        <li class='category'><div class='left_cat'>
        <a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
        <div class='title'><i class='icon-comment'></i>&nbsp;".$title."</div></a>
        <div class='info'><i class='icon-comments icon-1'></i>&nbsp;".topicreplies($cid, $tid)." Comments&nbsp; &nbsp; &nbsp;<i class='icon-user icon-1'>
        &nbsp;</i>".$last2_user."&nbsp;&nbsp; 
        $x
        &nbsp; &nbsp;<i class='icon-calendar'>&nbsp;</i>&nbsp;".convertdate($date)."</div>
        </div>";
        $topics .= "<div class='right_cat'>
        <div class='face'>
            <img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
        <div class='comments_cat'>
            <div class='comments_top'>Comments</div>
            <div class='comments'>".topicreplies($cid, $tid)."</div></div>
        <div class='views_cat'>
            <div class='views_top'>Views</div>
            <div class='views'>".$views."</div></div>
        </div></li>";
    }
    echo $topics;
} else {
    echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}
?>

我已经在顶部为您制作了变量。希望它能在上运行

if (mysql_num_rows($res) > 0) {
    while ($row = mysql_fetch_assoc($res)) {
        $tid = $row['id'];
        $cid = $row['category_id'];
        $title = $row['topic_title'];
        $views = $row['topic_views'];
        $date = $row['topic_date'];
        $creator = $row['topic_creator'];
        if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
        if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
        $topics .= "<li class='category'><div class='left_cat'>
        <a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'><div class='title'><i class='icon-comment'></i>&nbsp;".$title."</div></a>
        <div class='info'><i class='icon-comments icon-1'></i>&nbsp;".topicreplies($cid, $tid)." Comments&nbsp; &nbsp; &nbsp;<i class='icon-user icon-1'>
        &nbsp;</i>".$last2_user."&nbsp";

语法错误出现在最后一行,即;".$last2_user。";只需在"nbsp;这将解决你的问题。

看起来您正试图在创建的字符串中插入if语句。据我所知,你不能那样做。然而,解决方案很简单。结束字符串,插入if语句,然后附加所需内容的其余部分。像这样:

if (mysql_num_rows($res) > 0) {
    while ($row = mysql_fetch_assoc($res)) 
    {
    $tid = $row['id'];
    $cid = $row['category_id'];
    $title = $row['topic_title'];
    $views = $row['topic_views'];
    $date = $row['topic_date'];
    $creator = $row['topic_creator'];
    if ($row['topic_last_user']== "") 
        { $last_user = getusername($row['topic_creator']); } 
        else 
        { $last_user = getusername($row['topic_last_user']); }
    if ($row['topic_last_user']== "") 
        { $last2_user = 'Started by'; } 
        else 
        { $last2_user = 'Most recent by'; }
    $topics .= "<li class='category'><div class='left_cat'>
    <a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
    <div class='title'><i class='icon-comment'></i>&nbsp;".$title."</div></a>
    <div class='info'>
    <i class='icon-comments icon-1'></i>&nbsp;".topicreplies($cid, $tid)." 
    Comments&nbsp; &nbsp; &nbsp;<i class='icon-user icon-1'>
    &nbsp;</i>".$last2_user."&nbsp";
    // The string above is ended - and you insert your if statement.
    // I changed the echo to an append to the string btw.
    if ($info['rank'] == 1) {  $topics .=  "<div class='admin'>".$last_user."</div>"; } else { $topics .=  "<div>".$last_user."</div>"; }
    // Now you get to continue appending to the string.
    $topics .= "&nbsp; &nbsp; &nbsp;<i class='icon-calendar'>&nbsp;</i>&nbsp;".convertdate($date)."</div>
    </div>";
    $topics .= "<div class='right_cat'>
    <div class='face'>
        <img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
    <div class='comments_cat'>
        <div class='comments_top'>Comments</div>
        <div class='comments'>".topicreplies($cid, $tid)."</div></div>
    <div class='views_cat'>
        <div class='views_top'>Views</div>
        <div class='views'>".$views."</div></div>

    </div></li>";
    }
    echo $topics;
} else {
    echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}

下面的代码是我在上面提供的例子中得到的,这里是

<style type="text/css">
    .user { font-weight:900; color: #000; text-decoration: none !important; }
    .admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<form action="" method="POST">
    Input number and press enter: 
    </br>User = 0 Admin = 1</br>    
    <input name="rank" id="rank" type="text" />
</form>
<?php
    if (isset($_POST['rank'])) {
    $info['rank'] = $_POST['rank'];
    if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; } 
    else { $x= "<a class='user'>User</a>"; }
    echo $x;
   }
?> 

正如我在上面的评论中提到的,你的代码工作得很好,除非其他地方发生了我们看不到的事情。

没有表格。

<style type="text/css">
    .user { font-weight:900; color: #000; text-decoration: none !important; }
    .admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<?php
    $info['rank'] = 1;
    if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; }
    else { $x= "<a class='user'>User</a>"; }
    echo $x;
?>