Как иметь внутри, если

У меня два ранга на моем форуме Admin = 1 и User = 0 Тогда этот код делает так, что admin получает красное имя.

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