Hello,
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>";
Thanks… this is pretty awesome. I’ve set some of it up and it works great.
I’m having a hard time with the last part (where I’m supposed to “call” $editors). Where exactly should I put the code below?
$editors = getEditorsList();
foreach($editors as $editor)
{
$editorids[] = $editor['loginid'];
}
if(in_array($_SESSION['loginid'], $editorids))
{
// Code if user is an editor.
}
Okay, what getEditorsList() does is returns all the data fetched with that database query. All I’ve done there is place that data into $editors, then looped through the editors to narrow the data down to their loginid.
This segment:
$editors = getEditorsList();
foreach($editors as $editor)
{
$editorids[] = $editor['loginid'];
}
-Can be placed anywhere before the point where you want to use the editors information.
This segment:
if(in_array($_SESSION['loginid'], $editorids))
{
// Code if user is an editor.
}
-is used anywhere after the place where the $editorids variable is populated, and can be used repeatedly. So to place your link, your code may look like this:
<?php
include("editorFunctions.php");
$editors = getEditorsList();
foreach($editors as $editor)
{
$editorids[] = $editor['loginid'];
}
//Here's some code displaying the page.
if(in_array($_SESSION['loginid'], $editorids))
{
echo "<div class='footervote'><a href='http://www...com/.../footervote.php'>Vote</a></div>";
}
This can probably be further simplified by writing a special function whose only job is to tell you if someone is an editor. For that, your sql would remain very much the same, but you’d only want to have it retrieve loginid’s, then compare a variable you would pass to it. That would allow you to make better use of the editors list without being so bulky, and allow for better re-usability of the code.
If I’ve been unclear at all about this, please let me know. I tend to ramble a bit - even when I type. How sad is that? 
I hope this helps!
I’d recommend making the data retrieval for your list into its own function that can be called from your editorslist.php and rendered as a table, but can also be called by other scripts to test whether a user is on it.
For example:
// Place this function where all the scripts that need it can call it.
function getEditorsList(){
$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);
// Don't forget to make sure you check whether you have any results before attempting to retrieve them.
$rows = array();
if(mysql_num_rows($result))
{
while ($row = mysql_fetch_array($result)) {$rows[] = $row;}
}
return $rows;
}
Your editorslist.php file becomes this:
$arr = array();
echo "<table class=\\"samplesrec1edit\\">";
foreach(getEditorsList() as $row) {
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>";
And now, you can call this to find out if someone is an editor:
$editors = getEditorsList();
foreach($editors as $editor)
{
$editorids[] = $editor['loginid'];
}
if(in_array($_SESSION['loginid'], $editorids))
{
// Code if user is an editor.
}
There may be a better solution, but this is what I’ve got. I hope it helps!