TinyPortal

Development => Block Codes => Topic started by: JayT on August 01, 2006, 01:24:29 PM

Title: Polls and who voted
Post by: JayT on August 01, 2006, 01:24:29 PM
Well, I got a script from SMF that makes a form to pull DB info to see who voted in a poll. Here is the link to the script. (http://www.simplemachines.org/community/index.php?topic=59502.msg415302#msg415302)

Below is the script but the I get an error message when I try to run it. Here is the error: Fatal error: Unknown function: tp_loadtheme() in /Sources/Load.php on line 1037. Any ideas to fix this?


<?php
require_once(
"SSI.php");
if ( !$user_info['is_admin'] )
redirectexit();

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>'
;
}
?>
Title: Re: Polls and who voted
Post by: Xarcell on August 01, 2006, 02:06:24 PM
Thanx...
Title: Re: Polls and who voted
Post by: G6Cad on August 01, 2006, 02:32:32 PM
Where do you run the script from?
In a block or article?    If so, remove the php tags <?php and ?> (start and end) and see if you get it working
Title: Re: Polls and who voted
Post by: Crip on August 01, 2006, 02:35:57 PM
That script should be added in a phpbox.
Title: Re: Polls and who voted
Post by: JayT on August 01, 2006, 09:10:23 PM
That script goes in a php file that is ran from the forum root. However, it doesn't work that way, in a php article or a phpbox block. I am posting here because it seems to be an issue with TP because it works on a regular SMF forum.
Title: Re: Polls and who voted
Post by: G6Cad on August 01, 2006, 10:06:04 PM
Perhaps try to put in
global $scripturl;
in the start of the script for the php block you have ?
Title: Re: Polls and who voted
Post by: JayT on August 01, 2006, 10:13:22 PM
Keeps taking me to an error page saying the page isn't redirecting properly.
Title: Re: Polls and who voted
Post by: G6Cad on August 01, 2006, 10:31:25 PM
Do you have the correct url set up for where the script should find it?
Title: Re: Polls and who voted
Post by: G6Cad on August 01, 2006, 10:34:11 PM
Just to be sure, is this the code you are using now ?  With out the <?php and ?>


global $scripturl;
require_once("SSI.php");
if ( !$user_info['is_admin'] )
redirectexit();

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>';
}
Title: Re: Polls and who voted
Post by: JayT on August 02, 2006, 12:04:17 AM
Quote from: G6 on August 01, 2006, 10:31:25 PM
Do you have the correct url set up for where the script should find it?

Umm, not sure.
Quote from: G6 on August 01, 2006, 10:34:11 PM
Just to be sure, is this the code you are using now ?  With out the <?php and ?>


global $scripturl;
require_once("SSI.php");
if ( !$user_info['is_admin'] )
redirectexit();

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>';
}


Yes, I took out the php
Title: Re: Polls and who voted
Post by: G6Cad on August 02, 2006, 12:18:08 AM
QuoteDo you have the correct url set up for where the script should find it?

This is the part of the code i wonder if you have set correct for the polls on your site

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/>
Title: Re: Polls and who voted
Post by: JayT on August 02, 2006, 02:12:02 AM
At this point I am clueless. I went ahead and posted on the thread over at SMF. Maybe Thantos can figure it out.
Title: Re: Polls and who voted
Post by: 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.
Title: Re: Polls and who voted
Post by: JayT on August 02, 2006, 03:19:01 PM
Thanks Ichbin.

What did you do?
Title: Re: Polls and who voted
Post by: IchBin on August 02, 2006, 04:50:27 PM
I played with it until I got it to work. lol
First thing I thought was that the admin check was unecessary if its used inside TP so I commented that out. Then when I ran it, it said that a certain table wasn't found. So I looked for the table reference and decided to try and set the $db_prefix variable since it didn't seemed to be working right. After that it seemed to work fine. :)
Title: Re: Polls and who voted
Post by: JayT on August 02, 2006, 05:18:53 PM
Cool, well I guess this could be moved to the block codes.
Title: Re: Polls and who voted
Post by: IchBin on August 02, 2006, 05:23:35 PM
Yes, thank you!
Title: Re: Polls and who voted
Post by: Max on August 08, 2006, 06:09:55 PM

intresting script... anyone know how to link the polls to the topics?
Title: Re: Polls and who voted
Post by: Crip on August 08, 2006, 06:58:51 PM
IchBinâââ,¬Å¾Ã,¢..you....you, code monkey ;D