简体   繁体   中英

PHP MYSQLi: Condition inside a php variable

I have a sucession of sql statements and while loops that display the topic information stored in a variable $topics, inside that variable there is a link to Delete the topic.

What I want to do is to make that link visible only to admin with a condition like:

if (isset($_SESSION['level']) && (string) $_SESSION['level'] === "Administrator")

How can I do that? the link has to stay inside the variable because each topic has its own delete link next to it

this is the variable $topics, which is inside a while loop

 $topics .= "<tr><td><a id='del' href='deletetopic.php?del=$tid'>Delete</a></td></tr>"; 

for simplicity i removed other data that is displayed before the link in the same variable.

$topics is echo'ed later in the script

Edit: this is the actual variable

 $topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class ='post_info'>Posted by: ".getUsername($creator)." on ".convertDate($date)."</span></td><td align='center'>".topicReplies($cid, $tid)."</td><td align='center'>".$views."</td><td><a id='del' href='deletetopic.php?del=$tid'>Delete</a></td></tr>"; 

You should be able to do the following:

if (isset($_SESSION['level']) && (string) $_SESSION['level'] === "Administrator") {
    $topics .= "<tr><td><a id='del' href='deletetopic.php?del=$tid'>Delete</a></td></tr>";
}

Maybe even split things out a bit more so that you get an empty table cell instead of nothing:

$topics .= "<tr><td>";

if (isset($_SESSION['level']) && (string) $_SESSION['level'] === "Administrator") {
    $topics .= "<a id='del' href='deletetopic.php?del=$tid'>Delete</a>";
}

$topics .= "</td></tr>";

EDIT:

This should work:

$topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class ='post_info'>Posted by: ".getUsername($creator)." on ".convertDate($date)."</span></td><td align='center'>".topicReplies($cid, $tid)."</td><td align='center'>".$views."</td><td>";

if (isset($_SESSION['level']) && (string) $_SESSION['level'] === "Administrator") {
    $topics .= "<a id='del' href='deletetopic.php?del=$tid'>Delete</a>";
}

$topics .= "</td></tr>";

Seems to me it would just be:

 $topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class ='post_info'>Posted by: ".getUsername($creator)." on ".convertDate($date)."</span></td><td align='center'>".topicReplies($cid, $tid)."</td><td align='center'>".$views."</td>";

if (isset($_SESSION['level']) && (string) $_SESSION['level'] === "Administrator"){
    $topics .= "<td><a id='del' href='deletetopic.php?del=$tid'>Delete</a></td>";
}else{
   $topics .= "<td>&nbsp;</td>";
 }
 $topics .= "</tr>";

This will append the information always appended, conditionally append the delete link and then finish the row with a </tr> regardless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM