greens85 Posted December 7, 2010 Share Posted December 7, 2010 Hi all, I am having some trouble with a script.. all works fine except for the calculation part. It isn't adding up, but the reason for this is because when I echoed all the variable it seems that they are blank, the problem is I can't figure out why? <?php # you don't display errors on in-use scripts, do you? ini_set('display_errors',0); error_reporting(E_ALL|E_STRICT); class webPoll { # makes some things more readable later const POLL = true; const VOTES = false; # number of pixels for 1% on display bars public $scale = 2; public $question = ''; public $answers = array(); private $header = '<form class="webPoll" method="post" action="%src%"> <input type="hidden" name="QID" value="%qid%" /> <h4>%question%</h4> <br /> <ul>'; private $center = ''; private $footer = "\n</ul>%button%\n</form>\n"; private $button = '<p class="buttons"><button type="submit">Vote!</button></p>'; private $md5 = ''; /** * --- * Takes an array containing the question and list of answers as an * argument. Creates the HTML for either the poll or the results depending * on if the user has already voted */ public function __construct($params) { $this->question = array_shift($params); $this->answers = $params; $this->md5 = md5($this->question); $this->header = str_replace('%src%', $_SERVER['SCRIPT_NAME'], $this->header); $this->header = str_replace('%qid%', $this->md5, $this->header); $this->header = str_replace('%question%', $this->question, $this->header); # seperate cookie for each individual poll isset($_COOKIE[$this->md5]) ? $this->poll(self::VOTES) : $this->poll(self::POLL); } private function poll($show_poll) { $replace = $show_poll ? $this->button : ''; $this->footer = str_replace('%button%', $replace, $this->footer); # static function doesn't have access to instance variable if(!$show_poll) { $results = webPoll::getData($this->md5); $votes = array_sum($results); } for( $x=0; $x<count($this->answers); $x++ ) { $this->center .= $show_poll ? $this->pollLine($x) : $this->voteLine($this->answers[$x],$results[$x],$votes); } echo $this->header, $this->center, $this->footer; } private function pollLine($x) { isset($this->answers[$x+1]) ? $class = 'bordered' : $class = ''; return " <li class='$class'> <label class='poll_active'> <input type='radio' name='AID' value='$x' /> {$this->answers[$x]} </label> </li> "; } private function voteLine($answer,$result,$votes) { echo "Answer: $answer"; echo "<br />"; echo "Result: $result"; echo "<br />"; echo "Votes: $votes"; echo "<br />"; echo "<br />"; $result = isset($result) ? $result : 0; $percent = round(($result/$votes)*100); $width = $percent * $this->scale; return " <li> <div class='result' style='width:{$width}px;'> </div>{$percent}% <label class='poll_results'> $answer </label> </li> "; } // remainder of script here ?> The ones that are returning blank are: $result and $votes $results should be the number of votes a specific option has received while $votes is the total number of votes in the whole poll. My database contains the following data... QID AID votes 685b9628ca340529fa54208c65721dd7 2 205 685b9628ca340529fa54208c65721dd7 0 5 685b9628ca340529fa54208c65721dd7 1 2 It's from the following tutorial - http://net.tutsplus.com/tutorials/ph...poll-with-php/ Can anyone advise me here? Many thanks, Greens85 Quote Link to comment https://forums.phpfreaks.com/topic/220928-variables-returning-blank-values/ Share on other sites More sharing options...
AbraCadaver Posted December 7, 2010 Share Posted December 7, 2010 I didn't look at the tutorial (but this the code is horrible): if(!$show_poll) { $results = webPoll::getData($this->md5); $votes = array_sum($results); } This is the only place that $results and $votes are set, so if $show_poll evaluates to false then assign these vars, if it evaluates to true then they are never set anywhere else. Quote Link to comment https://forums.phpfreaks.com/topic/220928-variables-returning-blank-values/#findComment-1144010 Share on other sites More sharing options...
greens85 Posted December 7, 2010 Author Share Posted December 7, 2010 Thanks for the reply... I have a very limited understanding of PHP, so I wouldnt like to comment on how good or bad the code is... my understand of that part is that if the user has voted e.g. if the poll isnt showing: if(!$show_poll) { then show the results at which point the variables need to come into play. It seems that everyone has seemed to be able to implement the tutorial just fine judging by comments etc. The only thing I can think is that I changed all sqlite queries to mySQL queries but as far as I can see I haven't missed any, so I'm not sure why or if that should be a problem! Quote Link to comment https://forums.phpfreaks.com/topic/220928-variables-returning-blank-values/#findComment-1144020 Share on other sites More sharing options...
QuickOldCar Posted December 7, 2010 Share Posted December 7, 2010 Why not just do this as a fix if(!$show_poll) { $results = webPoll::getData($this->md5); $votes = array_sum($results); } else { $results = "No Data"; $votes = 0; } Quote Link to comment https://forums.phpfreaks.com/topic/220928-variables-returning-blank-values/#findComment-1144175 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.