TinyPortal

Development => Support => Topic started by: Innocenzia on March 31, 2008, 06:21:23 PM

Title: Poll-code..?
Post by: Innocenzia on March 31, 2008, 06:21:23 PM
I'm using this great code by IchBinâ,,¢ (thank you very much!!), however, i want to use it in the stats-page only, so i made the block show only there, but it doesn't work, i've come to realize this is because it loads from the index.php page somehow, aleast that's where you end up if using the button, and so i want to know if i can make it show up in the action=stats page instead of loading to the index-page? (since the block i'm using is only viewable form the stats-page.

Quote from: IchBinâ,,¢ on August 02, 2006, 04:11:59 AM
Here you go. This will display to anyone, so make sure you hide the block from other membergroups if you don't want people to see it.


require_once("SSI.php");
global $scripturl;
//if ( !$user_info['is_admin'] )
// redirectexit();
$db_prefix = 'smf_';
if ( isset($_POST['act']) && $_POST['act'] == 'display' )
{
if ( empty($_POST['topics']) )
$cond = "1";
else
$cond = "FIND_IN_SET(t.ID_TOPIC, '" . implode(',',explode(',',$_POST['topics'])) . "')";
// get the polls
$res = db_query("
SELECT
p.ID_POLL, p.question
FROM ({$db_prefix}topics AS t, {$db_prefix}polls AS p)
WHERE t.ID_POLL = p.ID_POLL
AND $cond
", __FILE__, __LINE__);
$polls = array(); $pollIds = array();
while ( $row = mysql_fetch_assoc($res) )
{
$polls[$row['ID_POLL']] = $row;
$polls[$row['ID_POLL']]['votes'] = array();
$pollIds[] = $row['ID_POLL'];
}
mysql_free_result($res);

if ( empty($polls) )
die("No polls in those topics");
// get the votes
$res = db_query("
SELECT mem.ID_MEMBER, mem.realName, pc.label, lp.ID_POLL
FROM {$db_prefix}log_polls AS lp
LEFT JOIN {$db_prefix}members AS mem ON lp.ID_MEMBER = mem.ID_MEMBER
LEFT JOIN {$db_prefix}poll_choices AS pc ON (lp.ID_CHOICE = pc.ID_CHOICE AND lp.ID_POLL=pc.ID_POLL)
WHERE FIND_IN_SET(lp.ID_POLL, '".implode(',',$pollIds)."')
", __FILE__, __LINE__);
while ( $row = mysql_fetch_assoc($res) )
$polls[$row['ID_POLL']]['votes'][] = $row;
mysql_free_result($res);

foreach($polls AS $poll)
{
echo '
<table border="1">
<caption>', $poll['question'], '</caption>
<tr>
<td align="center">Voted For</td>
<td align="center">By Member</td>
</tr>';
if ( empty($poll['votes']) )
echo '
<tr>
<td colspan="2" align="center">No members voted for this poll</td>
</tr>';
foreach($poll['votes'] AS $vote)
{
echo '
<tr>
<td align="center">', $vote['label'], '</td>
<td align="center"><a href="', $scripturl, '?action=profile;u=', $vote['ID_MEMBER'], '">', $vote['realName'], '</a></td>
</tr>';
}
echo '
</table>
<br/>
<br/>';
}
}
else
{
// create a form in wish they can input the topics
echo '
<form action="', $_SERVER['PHP_SELF'], '" method="post" >
<input type="hidden" name="act" value="display" />
Input the topics you want to display the polls for in a comma (,) delimited format <br/>
(i.e:  1,5,99,2058,63940,2) (Leave blank for ALL) <br/>
<input type="text" name="topics" size="100" /> <br/>
<input type="submit" value="Show Polls" />
</form>';
}


Also don't forget to set $db_prefix = 'smf_'; to your database prefix.