2013-08-31 2 views
0

내 포럼 Admin = 1 및 User = 0에서 2 개의 순위가 있습니다. 그러면 관리자가 빨간색 이름을 얻도록이 코드가 수행됩니다.if 문을 내부에 가지고있는 방법

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

하지만 모두가 빨간색으로 이름을 얻고있다 ..

+0

전체 코드를 방해하지 않고 표시 할 수 있습니까? –

+0

당신이 필요로하는 것은 PHP로 처리됩니다. 코드를 다시 확인하십시오. 제안은 코드 highligh로 IDE를 사용합니다. – SaidbakR

+0

다른 질문을하기 전에 http://whataveyoutried.com을 읽어보십시오 :) –

답변

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>"; 
} 

?> 

나는 상단에 변수를 만들었습니다. 은 다음 코드는 내가 위에서 제공된 예제에있는 것입니다

+1

최고야! :) – user2733334

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."   "& nbsp"에서 견적을 닫으십시오. 그러면 문제가 해결됩니다.

+0

좋아요. 그래서 수정했고 이것을'if ($ info [ 'rank'] = = 1) {echo "

".$last_user."
"; } else {echo "
".$last_user."
"} "그런 다음 이것은 오류입니다. ** 구문 분석 오류 : 구문 오류, 예기치 않은 '}', 예상 ','또는 ';' C : \ Wamp \ www \ forum \ index.php 157 라인 ** – user2733334

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>"; 
} 
+0

이 의견을 이해할 수 있는지 확실하지 않습니다. – Fluffeh

+0

새로운 문제가 생겼어 @Fluffeh – user2733334

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

@ user2733334 위 코드는 요청한 코드입니다. 도움이되기를 바랍니다. – Malcolm