PHP Web Host - Quality Web Hosting For All PHP Applications Free RavenNuke(tm) Add Ons
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
draxx
Worker
Worker


Joined: Nov 19, 2003
Posts: 186

PostPosted: Fri Mar 13, 2009 3:24 pm Reply with quote Back to top

Im trying to make the survey block random. Im sooo close but ..... well .. it doesnt work. I know im missing the obvoius here:

Code:

if ($multilingual == 1) {
   $querylang = 'WHERE planguage=\''.$currentlang.'\' AND artid=\'0\'';
} else {
   $querylang = 'WHERE artid=\'0\'';
}

/** added for random **/

mt_srand((double)microtime()*1000000);
$total = $db->sql_numrows($db->sql_query("select * from ".$prefix."_poll_desc"));
if ($total==0) $artrand=0;
else
$artrand = mt_rand(0,($total-1));
$pollID = $artrand;

/** end add **/

list($pollID) = $db->sql_fetchrow($db->sql_query('SELECT pollID FROM '.$prefix.'_poll_desc '.$querylang.' ORDER BY pollID DESC LIMIT 1'));
$pollID = intval($pollID);
if ($pollID == 0 || empty($pollID)) {
   $content = '';
} else {
   $content = '';
   if (!isset($url)) {
      $url = 'modules.php?name=Surveys&op=results&pollID='.$pollID;
   }
View user's profile Send private message
evaders99
Former Moderator in Good Standing


Joined: Apr 30, 2004
Posts: 3221

PostPosted: Fri Mar 13, 2009 6:06 pm Reply with quote Back to top

You need to actually fill in the $content variable with the survey form
View user's profile Send private message Visit poster's website
draxx
Worker
Worker


Joined: Nov 19, 2003
Posts: 186

PostPosted: Sat Mar 14, 2009 12:56 am Reply with quote Back to top

Code:

<?php

/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/*         Additional security & Abstraction layer conversion           */
/*                           2003 chatserv                              */
/*      http://www.nukefixes.com -- http://www.nukeresources.com        */
/************************************************************************/
/************************************************************************/
/* Additional code clean-up, performance enhancements, and W3C and      */
/* XHTML compliance fixes by Raven and Montego.                         */
/************************************************************************/

if ( !defined('BLOCK_FILE') ) {
   Header('Location: ../index.php');
   die();
}

global $prefix, $multilingual, $currentlang, $db, $boxTitle, $content, $pollcomm, $user, $cookie, $userinfo;

if ($multilingual == 1) {
   $querylang = 'WHERE planguage=\''.$currentlang.'\' AND artid=\'0\'';
} else {
   $querylang = 'WHERE artid=\'0\'';
}

/** added for random **/

mt_srand((double)microtime()*1000000);
$total = $db->sql_numrows($db->sql_query("select * from ".$prefix."_poll_desc"));

//if no polls then no number to choose from

if ($total==0) $artrand=0;
else
$artrand = mt_rand(0,($total-1));

//or else make a random number and make it the pollID

$pollID = $artrand;


/** end add **/

list($pollID) = $db->sql_fetchrow($db->sql_query('SELECT pollID FROM '.$prefix.'_poll_desc '.$querylang.' ORDER BY pollID DESC LIMIT 1'));
$pollID = intval($pollID);
if ($pollID == 0 || empty($pollID)) {
   $content = '';
} else {
   $content = '';
   if (!isset($url)) {
      $url = 'modules.php?name=Surveys&amp;op=results&amp;pollID='.$pollID;
   }
   $content .= '<form action="modules.php?name=Surveys" method="post">';
   $content .= '<input type="hidden" name="pollID" value="'.$pollID.'" />';
   $content .= '<input type="hidden" name="forwarder" value="'.$url.'" />';
   list($pollTitle, $voters) = $db->sql_fetchrow($db->sql_query('SELECT pollTitle, voters FROM '.$prefix.'_poll_desc WHERE pollID=\''.$pollID.'\''));
   $pollTitle = check_html($pollTitle, 'nohtml');
   $voters = intval($voters);
   $boxTitle = _SURVEY;
   $content .= '<span class="content"><strong>'.$pollTitle.'</strong></span><br /><br />';
   $content .= '<table border="0" width="100%">';
   for($i = 1; $i <= 12; $i++) {
      $sql = 'SELECT pollID, optionText, optionCount, voteID FROM '.$prefix.'_poll_data WHERE pollID=\''.$pollID.'\' AND voteID=\''.$i.'\'';
      $query = $db->sql_query($sql);
      list($pollID, $optionText, $optionCount, $voteID) = $db->sql_fetchrow($query);
      $pollID = intval($pollID);
      $voteID = intval($voteID);
      $optionCount = intval($optionCount);
      if (!empty($optionText)) {
         $content .= '<tr><td valign="top"><input type="radio" name="voteID" value="'.$i.'" /></td><td width="100%"><span class="content">'.$optionText.'</span></td></tr>';
      }
   }
   $content .= '</table><br /><center><span class="content"><input type="submit" value="'._VOTE.'" /></span><br />';
   if (is_user($user)) {
      cookiedecode($user);
      getusrinfo($user);
   }
   $sum = 0;
   for($i = 0; $i < 12; $i++) {
      $sql = 'SELECT optionCount FROM '.$prefix.'_poll_data WHERE pollID=\''.$pollID.'\' AND voteID=\''.$i.'\'';
      $query = $db->sql_query($sql);
      list($optionCount) = $db->sql_fetchrow($query);
      $optionCount = intval($optionCount);
      $sum = (int)$sum+$optionCount;
   }

   if (!isset($mode) OR empty($mode)) {
      if(isset($userinfo['umode'])) {
         $mode = $userinfo['umode'];
      } else {
         $mode = 'thread';
      }
   }
   if (!isset($order) OR empty($order)) {
      if(isset($userinfo['uorder'])) {
         $order = $userinfo['uorder'];
      } else {
         $order = 0;
      }
   }
   if (!isset($thold) OR empty($thold)) {
      if(isset($userinfo['thold'])) {
         $thold = $userinfo['thold'];
      } else {
         $thold = 0;
      }
   }
   $r_options = '';
   $r_options .= '&amp;mode='.$mode;
   $r_options .= '&amp;order='.$order;
   $r_options .= '&amp;thold='.$thold;
   $content .= '<br /><span class="content"><a href="modules.php?name=Surveys&amp;op=results&amp;pollID='.$pollID.$r_options.'"><strong>'
      ._RESULTS.'</strong></a><br /><a href="modules.php?name=Surveys"><strong>'._POLLS.'</strong></a><br />';

   if ($pollcomm) {
      $sql = 'SELECT * FROM '.$prefix.'_pollcomments WHERE pollID=\''.$pollID.'\'';
      $query = $db->sql_query($sql);
      $numcom = $db->sql_numrows($query);
      $content .= '<br />'._VOTES.': <strong>'.intval($sum).'</strong> <br /> '._PCOMMENTS.' <strong>'.intval($numcom).'</strong>';
   } else {
      $content .= '<br />'._VOTES.' <strong>'.intval($sum).'</strong>';
   }
   $content .= '</span></center></form>';
}

?>
View user's profile Send private message
evaders99
Former Moderator in Good Standing


Joined: Apr 30, 2004
Posts: 3221

PostPosted: Sat Mar 14, 2009 3:13 am Reply with quote Back to top

So what's the issue?

There's a rand SQL statement that you can use, if the PHP randomization isn't working
View user's profile Send private message Visit poster's website
duck
Involved
Involved


Joined: Jul 03, 2006
Posts: 267

PostPosted: Sat Mar 14, 2009 10:33 am Reply with quote Back to top

one thing I would think an issue is you are basing your pollid on a random number based on number of rows but the ID's may not match. By this I mean though you may have 10 rows of polls their id's may not number 1 - 10 the could be 1, 6 , 8 14, 16 etc therefore when you looking for pollid 5 it doesn't exist.

You need to change select statement to select id and add to array then select a random array value based on the numrows returned to array.

Or as Evaders suggest use a random sql statement.
View user's profile Send private message
draxx
Worker
Worker


Joined: Nov 19, 2003
Posts: 186

PostPosted: Sat Mar 14, 2009 12:20 pm Reply with quote Back to top

The issue is it doesnt work Laughing Smile It always displays to me the last survey created. Its almost like my efforts to change the pollID is not having any effect.

I dont think its the php rand function. I think Im just not telling it the random number in the right place ??

And Duck points out the potential for a bug perhaps? When the poll id's are no longer sequential.
View user's profile Send private message
nuken
RavenNuke(tm) Development Team


Joined: Mar 11, 2007
Posts: 1536
Location: North Carolina

PostPosted: Sat Mar 14, 2009 1:08 pm Reply with quote Back to top

Hers is a random survey block but I get the same thing as you. Only displays last poll not random.

Code:

<?php

/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/*         Additional security & Abstraction layer conversion           */
/*                           2003 chatserv                              */
/*      http://www.nukefixes.com -- http://www.nukeresources.com        */
/************************************************************************/

if ( !defined('BLOCK_FILE') ) {
    Header("Location: ../index.php");
    die();
}
$make_random = 1; //0- Means NO random polls. 1- Means random poll. 
$number_of_days = 30; //Number of days in between voting per user.

global $prefix, $db, $content, $pollcomm, $user, $userinfo, $cookie, $multilingual, $currentlang, $make_random, $number_of_days;

if ($multilingual == 1) {
    $querylang = "WHERE planguage='$currentlang' AND artid='0'";
} else {
    $querylang = "WHERE artid='0'";
}

if ($make_random == 1) {
$result = $db->sql_query("SELECT pollID, pollTitle, voters FROM ".$prefix."_poll_desc $querylang ORDER BY rand() LIMIT 1");
} elseif ($make_random == 0) {
$result = $db->sql_query("SELECT pollID, pollTitle, voters FROM ".$prefix."_poll_desc $querylang ORDER BY pollID DESC LIMIT 1");
}
if ($db->sql_numrows($result) < 1) {
    $content = "<br />No Surveys!<br /><br />";
} else {
    list($pollID, $pollTitle, $voters) = $db->sql_fetchrow($result);
    $url = "modules.php?name=Surveys&amp;op=results&amp;pollID=$pollID";
    $content = "<font class=\"content\"><b>$pollTitle</b></font><br /><br />\n";
    $content .= '<form action="modules.php?name=Surveys" method="post">';
    $content .= '<table border="0" cellpadding="2" cellspacing="0" width="100%">';

    $ip = $_SERVER["REMOTE_ADDR"];
    $past = time()-86400*$number_of_days;
    $result = $db->sql_query("SELECT ip FROM ".$prefix."_poll_check WHERE ip='$ip' AND pollID='$pollID'");
    $result2 = $db->sql_query("SELECT optionText, voteID, optionCount FROM ".$prefix."_poll_data WHERE pollID='$pollID' AND optionText!='' ORDER BY voteID");
    if ($db->sql_numrows($result) > 0) {
        while ($row = $db->sql_fetchrow($result2)) {
            $options[] = $row;
            $sum += (int)$row['optionCount'];
        }
        $ThemeSel = get_theme();
        $leftbar = file_exists("themes/$ThemeSel/images/survey_leftbar.gif") ? 'survey_leftbar.gif' : 'leftbar.gif';
        $mainbar = file_exists("themes/$ThemeSel/images/survey_mainbar.gif") ? 'survey_mainbar.gif' : 'mainbar.gif';
        $rightbar = file_exists("themes/$ThemeSel/images/survey_rightbar.gif") ? 'survey_rightbar.gif' : 'rightbar.gif';
        $l_size = getimagesize("themes/$ThemeSel/images/$leftbar");
        $m_size = getimagesize("themes/$ThemeSel/images/$mainbar");
        $r_size = getimagesize("themes/$ThemeSel/images/$rightbar");
        if (file_exists("themes/$ThemeSel/images/survey_mainbar_d.gif")) $mainbar_d = 'survey_mainbar_d.gif';
        if (isset($mainbar_d)) $m1_size = getimagesize("themes/$ThemeSel/images/$mainbar_d");
       
        foreach ($options as $option) {
            $percent = 100 / $sum * $option['optionCount'];
            $percentInt = (int)$percent * .85;
            $percent2 = (int)$percent;
            $content .= "<tr><td>$option[optionText]<br/>";
            $content .= "<img src=\"themes/$ThemeSel/images/$leftbar\" height=\"$l_size[1]\" width=\"$l_size[0]\" alt=\"$percent2 %\" title=\"$percent2 %\">";
            if ($percent > 0) {
                    $content .= "<img src=\"themes/$ThemeSel/images/$mainbar\" height=\"$m_size[1]\" width=\"$percentInt%\" alt=\"$percent2 %\" title=\"$percent2 %\">";
            } else {
                if (!isset($mainbar_d)) {
                    $content .= "<img src=\"themes/$ThemeSel/images/$mainbar\" height=\"$m_size[1]\" width=\"$m_size[0]\" alt=\"$percent2 %\" title=\"$percent2 %\">";
                }
            }
            $content .= "<img src=\"themes/$ThemeSel/images/$rightbar\" height=\"$r_size[1]\" width=\"$r_size[0]\" alt=\"$percent2 %\" title=\"$percent2 %\"><br/>";
            $content .= "</td></tr>\n";
        }
        $button = '';
    }
    else {
        while ($row = $db->sql_fetchrow($result2)) {
            $content .= "<tr><td valign=\"top\"><input type=\"radio\" name=\"voteID\" value=\"".$row['voteID']."\"></td><td width=\"100%\"><font class=\"content\">".$row['optionText']."</font></td></tr>\n";
            $sum += (int)$row['optionCount'];
        }
        $button .= '<input type="hidden" name="pollID" value="'.$pollID.'">';
        $button .= '<input type="hidden" name="forwarder" value="'.$url.'">';
        $button .= '<input type="submit" value="'._VOTE.'"><br /><br />';
    }
    $db->sql_freeresult($result);
    $db->sql_freeresult($result2);

    $content .= "</table><br /><center>$button
    <font class=\"content\"><a href=\"modules.php?name=Surveys&amp;op=results&amp;pollID=$pollID&amp;mode=$cookie[4]&amp;order=$cookie[5]&amp;thold=$cookie[6]\"><b>"._RESULTS."</b></a><br />
    <a href=\"modules.php?name=Surveys\"><b>"._POLLS."</b></a><br />
    <br />"._VOTES." <b>$sum</b>\n";
    if ($pollcomm) {
        list($numcom) = $db->sql_fetchrow($db->sql_query("SELECT COUNT(*) FROM ".$prefix."_pollcomments WHERE pollID='$pollID'"));
        $content .= "<br /> "._PCOMMENTS." <b>$numcom</b>\n";
    }
    $content .= "</font></center></form>\n";
}
?>
View user's profile Send private message Send e-mail Visit poster's website
duck
Involved
Involved


Joined: Jul 03, 2006
Posts: 267

PostPosted: Sat Mar 14, 2009 1:13 pm Reply with quote Back to top

One other issue I noticed would be the fact that you are doing the rand from 0 to -1 from the rows result. The likelyhood of you having a pollid of 0 is not great unless you set one yourself. you should be rand from 1, $total.

Of course this will not cause the failure your getting everytime but just something I noticed in a quick scan of your code.
View user's profile Send private message
duck
Involved
Involved


Joined: Jul 03, 2006
Posts: 267

PostPosted: Sat Mar 14, 2009 1:17 pm Reply with quote Back to top

Why not change your query to WHERE pollid = $pollid instead of orderby pollid ?

here's a great article on getting random query results you may want to look at.
Only registered users can see links on this board!
Get registered or login to the forums!
View user's profile Send private message
draxx
Worker
Worker


Joined: Nov 19, 2003
Posts: 186

PostPosted: Sat Mar 14, 2009 2:21 pm Reply with quote Back to top

The code I got comes from chatserv on nukemods in a random survey block he had working there. The block works on ravennuke as well but its not ... well its not "raven nuked" .. and it's not as pretty.

I think on the random function ... the polls start at 0 in the database so poll #1 is really poll 0. So we subtract 1 from the number to be sure Im not asking for poll# 5 when its really 4.

I will try this where pollid = instead of orderby - perhaps thats it. Wink
View user's profile Send private message
draxx
Worker
Worker


Joined: Nov 19, 2003
Posts: 186

PostPosted: Sat Mar 14, 2009 10:34 pm Reply with quote Back to top

I was right- I wasent sticking it in the right place.

This one works Smile Thanks guys!

Code:

<?php

/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/*         Additional security & Abstraction layer conversion           */
/*                           2003 chatserv                              */
/*      http://www.nukefixes.com -- http://www.nukeresources.com        */
/************************************************************************/
/************************************************************************/
/* Additional code clean-up, performance enhancements, and W3C and      */
/* XHTML compliance fixes by Raven and Montego.                         */
/************************************************************************/

if ( !defined('BLOCK_FILE') ) {
   Header('Location: ../index.php');
   die();
}

global $prefix, $multilingual, $currentlang, $db, $boxTitle, $content, $pollcomm, $user, $cookie, $userinfo;

if ($multilingual == 1) {
   $querylang = 'WHERE planguage=\''.$currentlang.'\' AND artid=\'0\'';
} else {
   $querylang = 'WHERE artid=\'0\'';
}

/** added for random - Random stuff from chatserv **/

mt_srand((double)microtime()*1000000);
$total = $db->sql_numrows($db->sql_query("select * from ".$prefix."_poll_desc"));
if ($total==0) $artrand=0;
else
$artrand = mt_rand(0,($total-1));
$pollID = $artrand;

/** end add -- translated by draxx **/

// OLD list($pollID) = $db->sql_fetchrow($db->sql_query('SELECT pollID FROM '.$prefix.'_poll_desc '.$querylang.' ORDER BY pollID DESC LIMIT 1'));
list($pollID) = $db->sql_fetchrow($db->sql_query('SELECT pollID FROM '.$prefix.'_poll_desc '.$querylang.' ORDER BY pollID DESC LIMIT '.$artrand.', 1'));
$pollID = intval($pollID);
if ($pollID == 0 || empty($pollID)) {
   $content = '';
} else {
   $content = '';
   if (!isset($url)) {
      $url = 'modules.php?name=Surveys&amp;op=results&amp;pollID='.$pollID;
   }
   $content .= '<form action="modules.php?name=Surveys" method="post">';
   $content .= '<input type="hidden" name="pollID" value="'.$pollID.'" />';
   $content .= '<input type="hidden" name="forwarder" value="'.$url.'" />';
   list($pollTitle, $voters) = $db->sql_fetchrow($db->sql_query('SELECT pollTitle, voters FROM '.$prefix.'_poll_desc WHERE pollID=\''.$pollID.'\''));
   $pollTitle = check_html($pollTitle, 'nohtml');
   $voters = intval($voters);
   $boxTitle = _SURVEY;
   $content .= '<span class="content"><strong>'.$pollTitle.'</strong></span><br /><br />';
   $content .= '<table border="0" width="100%">';
   for($i = 1; $i <= 12; $i++) {
      $sql = 'SELECT pollID, optionText, optionCount, voteID FROM '.$prefix.'_poll_data WHERE pollID=\''.$pollID.'\' AND voteID=\''.$i.'\'';
      $query = $db->sql_query($sql);
      list($pollID, $optionText, $optionCount, $voteID) = $db->sql_fetchrow($query);
      $pollID = intval($pollID);
      $voteID = intval($voteID);
      $optionCount = intval($optionCount);
      if (!empty($optionText)) {
         $content .= '<tr><td valign="top"><input type="radio" name="voteID" value="'.$i.'" /></td><td width="100%"><span class="content">'.$optionText.'</span></td></tr>';
      }
   }
   $content .= '</table><br /><center><span class="content"><input type="submit" value="'._VOTE.'" /></span><br />';
   if (is_user($user)) {
      cookiedecode($user);
      getusrinfo($user);
   }
   $sum = 0;
   for($i = 0; $i < 12; $i++) {
      $sql = 'SELECT optionCount FROM '.$prefix.'_poll_data WHERE pollID=\''.$pollID.'\' AND voteID=\''.$i.'\'';
      $query = $db->sql_query($sql);
      list($optionCount) = $db->sql_fetchrow($query);
      $optionCount = intval($optionCount);
      $sum = (int)$sum+$optionCount;
   }

   if (!isset($mode) OR empty($mode)) {
      if(isset($userinfo['umode'])) {
         $mode = $userinfo['umode'];
      } else {
         $mode = 'thread';
      }
   }
   if (!isset($order) OR empty($order)) {
      if(isset($userinfo['uorder'])) {
         $order = $userinfo['uorder'];
      } else {
         $order = 0;
      }
   }
   if (!isset($thold) OR empty($thold)) {
      if(isset($userinfo['thold'])) {
         $thold = $userinfo['thold'];
      } else {
         $thold = 0;
      }
   }
   $r_options = '';
   $r_options .= '&amp;mode='.$mode;
   $r_options .= '&amp;order='.$order;
   $r_options .= '&amp;thold='.$thold;
   $content .= '<br /><span class="content"><a href="modules.php?name=Surveys&amp;op=results&amp;pollID='.$pollID.$r_options.'"><strong>'
      ._RESULTS.'</strong></a><br /><a href="modules.php?name=Surveys"><strong>'._POLLS.'</strong></a><br />';

   if ($pollcomm) {
      $sql = 'SELECT * FROM '.$prefix.'_pollcomments WHERE pollID=\''.$pollID.'\'';
      $query = $db->sql_query($sql);
      $numcom = $db->sql_numrows($query);
      $content .= '<br />'._VOTES.': <strong>'.intval($sum).'</strong> <br /> '._PCOMMENTS.' <strong>'.intval($numcom).'</strong>';
   } else {
      $content .= '<br />'._VOTES.' <strong>'.intval($sum).'</strong>';
   }
   $content .= '</span></center></form>';
}

?>
View user's profile Send private message
Display posts from previous:       
Post new topic   Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2011 by Raven

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::
:: fisubice Theme Modified by the RavenNuke™ Team ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum