Hi Everyone,
Im having trouble trying to get a simple poll script to be included into my main script.
I originally build this script following the tutorial found over here at jotlab Tutorial: How to build a poll system with PHP and mySQL.
Im trying to customsize it a bit, as i found it simple and easy to change to suit my needs for it. I want to organsize it in such a way that the entire table outputs when echoing a single variable.
Here are the main two original bits of it that do currently work, but not quite the way i want it to:
<link rel="stylesheet" href="http://www.free4blackberry.com/includes/poll/css/style.css" type="text/css" media="screen" />
<?php
include('config.php');
if(isset($_POST['vote']) && isset($_POST['questions'])){
$query = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND `responses`.`ip`='".$_SERVER['REMOTE_ADDR']."' AND pid=(SELECT pid FROM `questions` WHERE id='".$_POST['questions']."' LIMIT 1)");
if(mysql_num_rows($query) == 0){
$query = mysql_query("INSERT INTO `responses` (`qid`, `ip`) VALUES ('".$_POST['questions']."', '".$_SERVER['REMOTE_ADDR']."')");
} else {
$error = 'You Already Voted';
}
} else if(!isset($_POST['questions']) && isset($_POST['vote'])){
$error = 'You Need To Select a Question';
}
include('poll.php');
?>
Here is the include, poll.php:
<?php
$query = mysql_query("SELECT * FROM `poll` ORDER BY `id` DESC LIMIT 1");
$rows = mysql_num_rows($query);
if($rows > 0){
$poll = mysql_fetch_array($query);
$title = $poll['name'];
} else {
$title = 'No Poll Yet';
}
$query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
$me = array($total);
while($row = mysql_fetch_array($query)){
$me[] = $row['hits'];
}
$max = max($me);
$query = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND `responses`.`ip`='".$_SERVER['REMOTE_ADDR']."' AND pid='".$poll['id']."'");
if(mysql_num_rows($query) > 0){
$total = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND pid='".$poll['id']."'");
$total = mysql_num_rows($total);
?>
<table width="156" cellpadding="0" cellspacing="0" border="0" class="maintable" align="center">
<tr>
<td valign="top" align="center" class="title"><?php echo $title; ?></td>
</tr>
<?php
$query = mysql_query("SELECT * FROM `questions` WHERE `pid`='".$poll['id']."' ORDER BY `question`");
$questions = mysql_num_rows($query);
if($questions > 0){
?>
<tr>
<td valign="top" style="padding: 5px;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="question">
<?php
while($question = mysql_fetch_array($query)){
$responses = mysql_query("SELECT count(id) as total FROM `responses` WHERE qid='".$question['id']."'");
$responses = mysql_fetch_array($responses);
if($total > 0 && $responses['total'] > 0){
$percentage = round(($responses['total'] / $max) * 100);
} else {
$percentage = 0;
}
$percentage2 = 100 - $percentage;
?>
<tr>
<td valign="top" nowrap="nowrap"><?php echo $question['question']; ?></td>
<td valign="top" height="10" width="100%" style="padding: 0px 10px;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" width="<?php echo $percentage; ?>%" <?php if($percentage > 0){?>style="background: url('/includes/poll/images/bar.jpg') repeat-x;"<?php } ?>><img src="/includes/poll/images/dot.gif" width="1" height="19" /></td>
<td valign="top" width="<?php echo $percentage2; ?>%"></td>
</tr>
</table>
</td>
<td valign="top"><?php echo $responses['total']; ?></td>
</tr>
<?php
}
?>
<tr>
<td valign="top" colspan="3" align="center" style="padding: 10px 0px 0px 0px;">Total Votes: <?php echo $total; ?></td>
</tr>
</table>
</td>
</tr>
<?php
}
?>
</table>
<?php
} else {
?>
<table width="156" cellpadding="0" cellspacing="0" border="0" class="maintable" align="center">
<tr>
<td valign="top" align="center" class="title"><?php echo $title; ?></td>
</tr>
<?php
$query = mysql_query("SELECT * FROM `questions` WHERE `pid`='".$poll['id']."' ORDER BY `question`");
$questions = mysql_num_rows($query);
if($questions > 0){
?>
<tr>
<td valign="top" style="padding: 5px;">
<form name="poll" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="question">
<?php
if(isset($error)){
?>
<tr>
<td valign="top" colspan="2" align="center" style="padding: 0px 0px 10px 0px;"><?php echo $error; ?></td>
</tr>
<?php
}
?>
<?php
while($question = mysql_fetch_array($query)){
?>
<tr>
<td valign="top" style="padding: 0px 10px 0px 0px;"><input type="radio" name="questions" value="<?php echo $question['id']; ?>" /></td>
<td valign="top" width="100%"><?php echo $question['question']; ?></td>
</tr>
<?php
}
?>
<tr>
<td valign="top" colspan="2" align="center" style="padding: 10px 0px 0px 0px;"><input type="submit" name="vote" value="Submit Vote" /><br />Vote To See Results</td>
</tr>
</table>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
Now this does work, but i have just redesigned my sites’ script a bit, with the help of the most legendary guy ever, linux freelancer
So i now have a script (which works perfectly) that looks like this:
<?php
ob_start(); // begin output buffering
include($docroot . 'includes/config.php');
include($docroot . '/includes/layout.php');
$page_script_name = 'index.php';
$head1;
$pageDetails = '
<title>free4blackberry - title</title>
<meta name="description" content="DESC" />
<meta name="keywords" content="description" content="KEYWORDS" />
';
$head2;
$header;
include($docroot . '/includes/menu.php');
include($docroot . '/includes/poll2/index.php');
$leftAds;
$comingSoon = '
<div class="coming-soon" style="border-top: 1px solid #666;">
<p>Coming Soon:</p>
<ul class="sitenav-linkstyles">
<li>Blackberry Pearl 9100 3G links</li>
<li>Blackberry Curve 9300 links</li>
<li>Blackberry Storm 2 9550 links</li>
<li>Blackberry Tour 9630 links</li>
<li>Download Section</li>
<div id="cs-done">
<li style="list-style: none; text-decoration: none; text-decoration: none; padding: 3px 0px 3px 0px;"><strong><i>Completed:</i></strong></li>
<li>PHP Polling System</li>
<li>Guide - <a href="/how_to/install_games/index.php" >How To Install Games</a></li>
<li>Blackberry Torch <a href="/phones/9800/index.php" >9800 links</a></li>
<li>My must have#8217;s - <a href="/top_5/games.php" >Top 5 Free Games</a></li>
<li>Guide - <a href="/how_to/install_themes/index.php" >How To Install Themes</a></li>
<!-- end #cs-done --></div>
<li style="list-style-type: none;"><a href="/update_log.php"><i>See all...</i></a></li>
</ul>
<!-- end .coming-soon --></div>
';
$content;
$pageH1 = '
<h1>Free BlackBerry Downloads, Guides, News and More</h1>
';
$topAds;
$containerStart = '
<div id="news-feed">
';
include($docroot . '/includes/newsFeed.php');
$containerEnd = '
</div>
<br />
<!-- end .content --></div>
';
$rightColumn;
$footer;
$pageScripts;
// echo the results - do not add anything past this:
echo $head1 . $pageDetails . $head2 . $header . $menu . $pollOutput . $leftAds . $comingSoon . $content . $pageH1 . $topAds . $containerStart . $fileContents . $containerEnd . $rightColumn . $footer . $pageScripts;
exit;
?>
I am trying to output the table in poll.php when echoing the variable $pollOutput and this is where i am hopelessly stuck.
Despite taking this script apart and putting it back together in various ways 20odd+ times… I still have no idea where im going wrong.
So to cut the very long story short, this is what i have so far:
<?php
$pollQuery = mysql_query("SELECT * FROM `poll` ORDER BY `id` DESC LIMIT 1");
$rows = mysql_num_rows($pollQuery);
if($rows > 0){
$poll = mysql_fetch_array($pollQuery);
$title = $poll['name'];
} else {
$title = 'No Poll Yet';
}
$pollQuery = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
$me = array($total);
while($row = mysql_fetch_array($pollQuery)){
$me[] = $row['hits'];
}
$max = max($me);
$pollQuery = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND `responses`.`ip`='".$_SERVER['REMOTE_ADDR']."' AND pid='".$poll['id']."'");
if(mysql_num_rows($pollQuery) > 0){
$total = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND pid='".$poll['id']."'");
$total = mysql_num_rows($total);
$pollQuery = mysql_query("SELECT * FROM `questions` WHERE `pid`='".$poll['id']."' ORDER BY `question`");
$questions = mysql_num_rows($pollQuery);
if($questions > 0){
while($question = mysql_fetch_array($pollQuery)){
$responses = mysql_query("SELECT count(id) as total FROM `responses` WHERE qid='".$question['id']."'");
$responses = mysql_fetch_array($responses);
if($total > 0 && $responses['total'] > 0){
$percentage = round(($responses['total'] / $max) * 100);
} else {
$percentage = 0;
}
$percentage2 = 100 - $percentage;
}
if($percentage > 0){
$perStyle = '<style="background: url(#8217;/includes/poll/images/bar.jpg#8217;) repeat-x;">';
}
}
} else {
$pollQuery = mysql_query("SELECT * FROM `questions` WHERE `pid`='".$poll['id']."' ORDER BY `question`");
$questions = mysql_num_rows($pollQuery);
if($questions > 0){
if(isset($error)){
}
while($question = mysql_fetch_array($pollQuery)){
}
}
}
$pollOutput = '
<table width="156" cellpadding="0" cellspacing="0" border="0" class="maintable" align="center">
<tr>
<td valign="top" align="center" class="title">'.$title.';</td>
</tr>
<tr>
<td valign="top" style="padding: 5px;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="question">
<tr>
<td valign="top" nowrap="nowrap">'.$question['question'].';</td>
<td valign="top" height="10" width="100%" style="padding: 0px 10px;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" width="'.$percentage.';%" > '.$perStyle.' <img src="/includes/poll/images/dot.gif" width="1" height="19" /></td>
<td valign="top" width="'.$percentage2.';%"></td>
</tr>
</table>
</td>
<td valign="top">'.$responses['total'].';</td>
</tr>
<tr>
<td valign="top" colspan="3" align="center" style="padding: 10px 0px 0px 0px;">Total Votes: '.$total.';</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="156" cellpadding="0" cellspacing="0" border="0" class="maintable" align="center">
<tr>
<td valign="top" align="center" class="title">'.$title.';</td>
</tr>
<tr>
<td valign="top" style="padding: 5px;">
<form name="poll" method="post" action="'.$_SERVER['PHP_SELF'].';">
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="question">
<tr>
<td valign="top" colspan="2" align="center" style="padding: 0px 0px 10px 0px;">'.$error.';</td>
</tr>
<tr>
<td valign="top" style="padding: 0px 10px 0px 0px;"><input type="radio" name="questions" value="'.$question['id'].';" /></td>
<td valign="top" width="100%">'.$question['question'].';</td>
</tr>
<tr>
<td valign="top" colspan="2" align="center" style="padding: 10px 0px 0px 0px;"><input type="submit" name="vote" value="Submit Vote" /><br />Vote To See Results</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
';
?>
Now this partly works, as in it displays the table(twice), at the right point on the page, with a jumble of things inside of it. A mess basically.
I am pretty sure that the tables parts need to be echoed peice by peice, because of what seems like the reuse of the variable $query?
Does anyone have any idea where i am going wrong here?