开发者

Having a Link Only Appear If a Logged-In User Appears on a Dynamic List

For the function below, I would like the link <div class="footervote"><a href="http://www...com/.../footervote.php">Vote</a></div> to only appear if the logged in user currently appears on editorlist.php. (I. e. if the loginid in the function corresponds to any of the usernames that currently appear in editorlist.php.)

Appearing on editorlist.php is something that is dynamic.

How can I do this?

Thanks in advance,

John

function show_userbox()
{
    // retrieve the session information
    $u = $_SESSION['username'];
    $uid = $_SESSION['loginid'];
    // display the user box
    echo '<div id="userbox">

                <div class="username">'.$u.'</div>              
                <div class="submit"><a href="http://www...com/.../submit.php">Submit an item.</a></div>
                <div class="changepassword"><a href="http://www...com/.../changepassword.php">Change Password</a></div>
                <div class="logout"><a href="http://www...com/.../logout.php">Logout</a></div>
                <div class="footervote"><a href="http://www...com/.../footervote.php">Vote</a></div>

         </div>';
}

On editorlist.php:

$sqlStr = "SELECT 
    l.loginid, 
    l.username, 
    l.created,
    DATEDIFF(NOW(), l.created) AS days,
    COALESCE(s.total, 0) AS countSubmissions, 
    COALESCE(c.total, 0) AS countComments,
    COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore,
    DATEDIFF(NOW(), l.created) + COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore2
FROM login l    
LEFT JOIN (
    SELECT loginid, COUNT(1) AS total 
    FROM submission 
    GROUP BY loginid
) s ON l.loginid = s.loginid
LEFT JOIN (
    SELECT loginid, COUNT(1) AS total 
    FROM comment 
    GROUP BY loginid
) c ON l.loginid = c.loginid
GROUP BY l.loginid
ORDER BY totalScore2 DESC 
LIMIT 10";

  $result = mysql_query($sqlStr);

$arr = array(); 
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) { 
    echo '<tr>';
    echo '<td class="sitename1edit1"><a href="http://www...com/.../members/index.php?profile='.$row["username"].'">'.stripslashes($row["username"]).'</a></td>';
    echo '<td class="sitename1edit2">'.($row["countSubmissions"]).'</td>';
    echo '<td class="sitename1edit2">'.($row["countComments"]).'</td>';
    echo '<td class="sitename1edit2">'.($row["days"开发者_运维百科]).'</td>';
    echo '<td class="sitename1edit2">'.($row["totalScore2"]).'</td>';
    echo '</tr>';
    }
echo "</table>";


You could try running the SQL query from editorlist.php to determine if the user is on the list, rather than trying to pull data from the editorlist.php page itself.


I would make your editor sql something more manageable, like:

function getEditors($editor = false) {
    $sqlStr = "SELECT  l.loginid,  l.username,   l.created,
    DATEDIFF(NOW(), l.created) AS days,   COALESCE(s.total, 0) AS countSubmissions, 
    COALESCE(c.total, 0) AS countComments,  COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore,   DATEDIFF(NOW(), l.created) + COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore2
FROM login l LEFT JOIN (
    SELECT loginid, COUNT(1) AS total    FROM submission 
    GROUP BY loginid) s ON l.loginid = s.loginid LEFT JOIN (  SELECT loginid, COUNT(1) AS total 
    FROM comment    GROUP BY loginid ) c ON l.loginid = c.loginid ";

  if( $editor !== false ) { //if we specified an editor, find it
     $sqlStr .= " WHERE `l.loginid` = '" . $editor . "'";
  }

   $sqlStr .= "GROUP BY l.loginid
   ORDER BY totalScore2 DESC 
   LIMIT 10";

$result = mysql_query($sqlStr);

  if( $editor !== false) { // if we specified an editor, return that editor or false
     if( $row = mysql_fetch_assoc($result)) {
        return $row;
     }
     return false;
  }else { // otherwise, return the array of editors 
    $editors = array();
     while( $row = mysql_fetch_assoc($result)) {
       $editors[] = $row;
     }
    return $editors;
  }
}

and that way for your editorlist.php you could do

$editors = getEditors();
foreach( $editors as $editors ) {
    // echo your table row like you were doing
}

and in show_userbox() you could do

$editor = getEditors(12); 
if( $editor ) {
  // echo your vote html stuff
} 

i'm not saying do this implicitly.. but i think it'll give you the right idea of where to jump from..


If a user is listed as an Editor throughout the site, I would recommend setting a $_SESSION[] variable to record whether the User is an Editor or not at the same time you process their login.

In the event that Editors are restricted with regards to what they have control over, maybe setting the session variable to an array of items which they can edit may be an option.

Just thinking - check once and remember, rather than check every. single. time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜