Help with including one script into another

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 &gt; 0){
	?&gt;
	&lt;tr&gt;
		&lt;td valign="top" style="padding: 5px;"&gt;
		&lt;form name="poll" method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
		&lt;table width="100%" cellpadding="0" cellspacing="0" border="0" class="question"&gt;
		&lt;?php
			if(isset($error)){
		?&gt;
			&lt;tr&gt;
				&lt;td valign="top" colspan="2" align="center" style="padding: 0px 0px 10px 0px;"&gt;&lt;?php echo $error; ?&gt;&lt;/td&gt;
			&lt;/tr&gt;
		&lt;?php
			}
		?&gt;
			&lt;?php
				while($question = mysql_fetch_array($query)){
			?&gt;
				&lt;tr&gt;
					&lt;td valign="top" style="padding: 0px 10px 0px 0px;"&gt;&lt;input type="radio" name="questions" value="&lt;?php echo $question['id']; ?&gt;" /&gt;&lt;/td&gt;
					&lt;td valign="top" width="100%"&gt;&lt;?php echo $question['question']; ?&gt;&lt;/td&gt;
				&lt;/tr&gt;
			&lt;?php
			}
			?&gt;
			&lt;tr&gt;
				&lt;td valign="top" colspan="2" align="center" style="padding: 10px 0px 0px 0px;"&gt;&lt;input type="submit" name="vote" value="Submit Vote" /&gt;&lt;br /&gt;Vote To See Results&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;
		&lt;/form&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
	&lt;?php
		}
	?&gt;
&lt;/table&gt;
&lt;?php
	}
?&gt;

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 :slight_smile:

So i now have a script (which works perfectly) that looks like this:

&lt;?php
ob_start(); // begin output buffering

include($docroot . 'includes/config.php');
include($docroot . '/includes/layout.php');
$page_script_name = 'index.php';

$head1;

$pageDetails = '
&lt;title&gt;free4blackberry - title&lt;/title&gt;
&lt;meta name="description" content="DESC" /&gt;
&lt;meta name="keywords" content="description" content="KEYWORDS" /&gt;
';

$head2;

$header;

include($docroot . '/includes/menu.php');

include($docroot . '/includes/poll2/index.php');

$leftAds;

$comingSoon = '
&lt;div class="coming-soon" style="border-top: 1px solid #666;"&gt;
    &lt;p&gt;Coming Soon:&lt;/p&gt;
    &lt;ul class="sitenav-linkstyles"&gt;
     &lt;li&gt;Blackberry Pearl 9100 3G links&lt;/li&gt;
     &lt;li&gt;Blackberry Curve 9300 links&lt;/li&gt;
     &lt;li&gt;Blackberry Storm 2 9550 links&lt;/li&gt;
     &lt;li&gt;Blackberry Tour 9630 links&lt;/li&gt;
     &lt;li&gt;Download Section&lt;/li&gt;
      &lt;div id="cs-done"&gt;
       &lt;li style="list-style: none; text-decoration: none; text-decoration: none; padding: 3px 0px 3px 0px;"&gt;&lt;strong&gt;&lt;i&gt;Completed:&lt;/i&gt;&lt;/strong&gt;&lt;/li&gt;
       &lt;li&gt;PHP Polling System&lt;/li&gt;
       &lt;li&gt;Guide - &lt;a href="/how_to/install_games/index.php" &gt;How To Install Games&lt;/a&gt;&lt;/li&gt;
       &lt;li&gt;Blackberry Torch &lt;a href="/phones/9800/index.php" &gt;9800 links&lt;/a&gt;&lt;/li&gt;
       &lt;li&gt;My must have#8217;s - &lt;a href="/top_5/games.php" &gt;Top 5 Free Games&lt;/a&gt;&lt;/li&gt;
       &lt;li&gt;Guide - &lt;a href="/how_to/install_themes/index.php" &gt;How To Install Themes&lt;/a&gt;&lt;/li&gt;
      &lt;!-- end #cs-done --&gt;&lt;/div&gt;
     &lt;li style="list-style-type: none;"&gt;&lt;a href="/update_log.php"&gt;&lt;i&gt;See all...&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
   &lt;!-- end .coming-soon --&gt;&lt;/div&gt;
   ';

$content;

$pageH1 = '
	&lt;h1&gt;Free BlackBerry Downloads, Guides, News and More&lt;/h1&gt;
';

$topAds;

$containerStart = '
	&lt;div id="news-feed"&gt;
';

	include($docroot . '/includes/newsFeed.php');

$containerEnd = '
	&lt;/div&gt;
	&lt;br /&gt;
	&lt;!-- end .content --&gt;&lt;/div&gt;
';

$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;
?&gt;

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:

&lt;?php
	
	$pollQuery = mysql_query("SELECT * FROM `poll` ORDER BY `id` DESC LIMIT 1");
	$rows = mysql_num_rows($pollQuery);
	
	if($rows &gt; 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 &gt; 0){

			if(isset($error)){
	
			}
	
				while($question = mysql_fetch_array($pollQuery)){
			
			}
			
		}
	
	}
	
	$pollOutput = '
&lt;table width="156" cellpadding="0" cellspacing="0" border="0" class="maintable" align="center"&gt;
	&lt;tr&gt;
		&lt;td valign="top" align="center" class="title"&gt;'.$title.';&lt;/td&gt;
	&lt;/tr&gt;
    
    &lt;tr&gt;
		&lt;td valign="top" style="padding: 5px;"&gt;
		&lt;table width="100%" cellpadding="0" cellspacing="0" border="0" class="question"&gt;
        
        &lt;tr&gt;
					&lt;td valign="top" nowrap="nowrap"&gt;'.$question['question'].';&lt;/td&gt;
					&lt;td valign="top" height="10" width="100%" style="padding: 0px 10px;"&gt;
					&lt;table width="100%" cellpadding="0" cellspacing="0" border="0"&gt;
						&lt;tr&gt;
							&lt;td valign="top" width="'.$percentage.';%" &gt; '.$perStyle.' &lt;img src="/includes/poll/images/dot.gif" width="1" height="19" /&gt;&lt;/td&gt;
							&lt;td valign="top" width="'.$percentage2.';%"&gt;&lt;/td&gt;
						&lt;/tr&gt;
					&lt;/table&gt;
					&lt;/td&gt;
					&lt;td valign="top"&gt;'.$responses['total'].';&lt;/td&gt;
				&lt;/tr&gt;
                
                &lt;tr&gt;
				&lt;td valign="top" colspan="3" align="center" style="padding: 10px 0px 0px 0px;"&gt;Total Votes: '.$total.';&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;
		&lt;/td&gt;
	&lt;/tr&gt;

&lt;/table&gt;

&lt;table width="156" cellpadding="0" cellspacing="0" border="0" class="maintable" align="center"&gt;
	&lt;tr&gt;
		&lt;td valign="top" align="center" class="title"&gt;'.$title.';&lt;/td&gt;
	&lt;/tr&gt;
    
    &lt;tr&gt;
		&lt;td valign="top" style="padding: 5px;"&gt;
		&lt;form name="poll" method="post" action="'.$_SERVER['PHP_SELF'].';"&gt;
		&lt;table width="100%" cellpadding="0" cellspacing="0" border="0" class="question"&gt;
        
        &lt;tr&gt;
				&lt;td valign="top" colspan="2" align="center" style="padding: 0px 0px 10px 0px;"&gt;'.$error.';&lt;/td&gt;
			&lt;/tr&gt;
            
            &lt;tr&gt;
					&lt;td valign="top" style="padding: 0px 10px 0px 0px;"&gt;&lt;input type="radio" name="questions" value="'.$question['id'].';" /&gt;&lt;/td&gt;
					&lt;td valign="top" width="100%"&gt;'.$question['question'].';&lt;/td&gt;
				&lt;/tr&gt;
                
                &lt;tr&gt;
				&lt;td valign="top" colspan="2" align="center" style="padding: 10px 0px 0px 0px;"&gt;&lt;input type="submit" name="vote" value="Submit Vote" /&gt;&lt;br /&gt;Vote To See Results&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;
		&lt;/form&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
    
    &lt;/table&gt;
	';
	
?&gt;

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?