如何在if内部使用if语句

时间:2013-08-31 10:27:56

标签: php mysql sql

我的论坛中有两个等级Admin = 1和User = 0然后这段代码正在执行,管理员获取红色名称。

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

但每个人都有红名......

4 个答案:

答案 0 :(得分:0)

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”。 只需关闭“&amp; nbsp”的报价; 这将解决您的问题。

答案 1 :(得分:0)

看起来您正在尝试在正在创建的字符串中插入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>";
}

答案 2 :(得分:0)

使用您想要的$ 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>";
}

?>

我在顶部为你做变量。 希望它有效

答案 3 :(得分:0)

以下代码是我在上面提供的示例中提供的代码,Here

<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;
?>