/    Sign up×
Community /Pin to ProfileBookmark

php web poll

Code to display poll results. This works except is does not show the results transfer the results into bargraph.php. It appears nothing happens. The file showpoll and bargraph.php are saved in the same folder.

any thoughts why this is not working

[code=php]
<?
$host = ‘localhost’;
$user = ‘root’;
$pass = ‘password’;
$db = ‘mydata’;
print “<html>n”;
print “<head>n”;
print “<title>Vote totals</title>n”;
print “</head>n”;
print “<body>n”;
$dbcon = mysql_connect($host, $user, $pass)
or die(‘Unable to connect to server ‘ . $host);
mysql_select_db($db) or die(‘Unable to find database ‘ . $db);
if(isset($_POST[‘vote’]) && ctype_digit($_POST[‘vote’])) {
$query = ‘UPDATE poll_answers SET votes=votes+1
WHERE choice=’ . $_POST[‘vote’];
$result = mysql_query($query);
}
$num_votes_query = ‘SELECT SUM(votes) AS sumvotes
FROM poll_answers’;
$num_votes_query .= ‘ WHERE poll_num=1’;
if ($result = mysql_query($num_votes_query)) {
$row = mysql_fetch_array($result);
$sum = $row[‘sumvotes’];
}
$totals_query = ‘SELECT activity, votes FROM poll_answers ‘;

if ($result = mysql_query($totals_query)) {
print “<table>n”;
print “<tr><th>Activity</th><th>Votes</th></tr>n”;
while($row = mysql_fetch_array($result)) {
print “<tr><td>” . $row[‘activity’] . “</td>n”;
print “<td align=”right”>” . $row[‘votes’] . “</td></tr>n”;
if($sum) {
$percent = round($row[‘votes’] * 100 / $sum);
print “<tr><td><img src=”bargraph.php?pct=$percent”></td>n”;
print “<td align=”right”>” . $percent . “%</td></tr>n”;
}

}
print “</table>n”;
}
print “</body>n”;
print “</html>n”;
?>
[/code]

bargraph.php file code

[code=php]
<?php
$height = 10;
$width = 100;
$im = imagecreate($width, $height);
$bg = imagecolorallocate($im, 255, 255, 255); // white
$fg = imagecolorallocate($im, 255, 0, 0); // red
imagefilledrectangle($im, 0, 0, $_GET[‘pct’], $height, $fg);
imagerectangle($im, 0, 0, $width, $height, $bg);
header(“Content-type: image/png”);
imagepng($im);
imagedestroy($im);
?>

[/code]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 11.2006 — Not sure if it's just the way this forum is displaying the code, but it looks as if you're using special quote characters around the attribute values inside some of your HTML elements in the code. If you're using a word-processor and not just a plain text editor, it might be automatically doing it without your "permission" to do so.
Copy linkTweet thisAlerts:
@kprocauthorJul 11.2006 — thank you for the info, I changed the funny character to ', still no better

how could I test that it is trying to open bargraph.php
Copy linkTweet thisAlerts:
@NogDogJul 11.2006 — What happens if you call the php file directly from the address line of your browser?
Copy linkTweet thisAlerts:
@kprocauthorJul 11.2006 — I get this message

Warning: Cannot modify header information - headers already sent by (output started at C:Program FilesxampphtdocsNewStuffbargraph.php:2) in C:Program FilesxampphtdocsNewStuffbargraph.php on line 10

‰PNG

line 10 =header("Content-type: image/png");
Copy linkTweet thisAlerts:
@NogDogJul 11.2006 — Make sure that file has absolutely nothing before the opening <?php tag, including newlines, spaces, etc.
Copy linkTweet thisAlerts:
@kprocauthorJul 11.2006 — Thanks for the help, the error message was caused by a space in the header.
Copy linkTweet thisAlerts:
@kprocauthorJul 11.2006 — It apears that the string to open the file bargraph.php is not working.

When I display "survey results.php" the results display but the graph does NOT open.

I believe the issue is here

<i>
</i> print "&lt;tr&gt;&lt;td&gt;&lt;img src=”bargraph.php?pct=$percent”&gt;&lt;/td&gt;n";

Copy linkTweet thisAlerts:
@kprocauthorJul 11.2006 — OPs,

I posted the incorrect code for the bargraph.php file.

its been update. I hope it will help determine what is "not happening" ?

thank you for any help
Copy linkTweet thisAlerts:
@kprocauthorJul 12.2006 — I confirmed that an image is being created when I open the bargraph.php
×

Success!

Help @kproc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...