Johnny Johnny - 4 years ago 76
MySQL Question

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




Answer Source

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>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download