...

View Full Version : Variables returning blank values!



greens85
12-07-2010, 04:26 PM
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;'>&nbsp;</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/php/creating-a-web-poll-with-php/

Can anyone advise me here?

Many thanks,

Greens85

Jack Ellis
12-08-2010, 12:19 AM
The reason there are no replies is because you haven't been clear at all. Which bit isn't working etc?

mlseim
12-08-2010, 01:46 AM
You're showing us a class and some functions.
There must be other scripting that you are not showing us?

greens85
12-08-2010, 10:48 AM
The reason there are no replies is because you haven't been clear at all. Which bit isn't working etc?

Hi Jack,

Many thanks for your reply.

The problem is that the two variables that are used to create the poll results are being returned blank i.e. $result and $votes. The reason I know this is because I am echoing them out for testing purposes.


echo "Result: $result";
echo "<br />";
echo "Votes: $votes";
echo "<br />";
echo "<br />";

Since I know they are blank I can guess that this bit of script is never going to calculate percentages etc.


$result = isset($result) ? $result : 0;
$percent = round(($result/$votes)*100);
$width = $percent * $this->scale;

As it is obviously dependant on $result and $votes to work. Which is producing the results screen to look like this (print screen attached)

That said I know for a fact the database is populated correctly because I can see the data via phpmyadmin!

The complication is obviously is getting the data and populating the variables correctly.

In my reply to mlseim I will attach all source code and try to provide more details so that someone with much more experience of PHP can advise.

Many thanks,

Greens85

greens85
12-08-2010, 11:04 AM
You're showing us a class and some functions.
There must be other scripting that you are not showing us?

Hi mlseim,

As previously mentioned I was following a tutorial so I cant claim to know the code inside out but I will do my best to show you all you need and explain where possible.

I have attached the class that is being used (obviously I have commented out my database connection credentials).

I think the problem lies in the class and in particular line 145:


$STH = $dbh->prepare('SELECT AID, votes FROM tally WHERE QID = ?');

As i mentioned I converted most queries from sqlite to mySQL I may have forgotten to do this one, although I can't see to much wrong with this. I'm not sure on the question mark but I believe this is more to do with the PDO class than it is the sql query.

As per this page about PDO:
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#10

Anyway the class is attached, if you need any more information please let me know and I will try my best to provide it. In the meantime I will try and get to the bottom of it!

Many thanks,

Greens85

greens85
12-08-2010, 11:24 AM
Hi all,

Not to worry I have now got this sorted!

I hadn't formed a second database connection for the getData function, I had just assumed it would connect because I had the credentials further up!

You live and learn I guess.

Thanks for the responses.

Many thanks,

Greens85



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum