Accessing Variable before it is Parsed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Accessing Variable before it is Parsed

Post by nickman013 »

Hello,

I guess it is impossible to access a variable before it has been parsed.

I need help re-arranging this page so the title var will be from the database.

Code: Select all

<?
$page = $row['Name'];
include("/home/muot/public_html/site/includes/top.php");
?>
<!--START CONTENT-->
<table border=2 width=450><tr><td>
<?php  
$username5= "muot_video";  
$password5= "passwords";  
$database5= "muot_videos";  
$connection5 = mysql_connect('localhost',$username5,$password5);  
mysql_select_db($database5); 
$clause = '';  
if (!empty($_GET['ID'])) {  
   $clause  = 'WHERE ';   
   $clause .=  (is_numeric($_GET['ID'])   
      ? '`ID` '  
      : '`Filename` '  
   );  
   $clause .= '= \''. mysql_real_escape_string($_GET['ID']) .'\' LIMIT 1';   
}  
     
$sql5 = 'SELECT `Filename`, `ID` FROM `videos` '. $clause;   

$result5 = mysql_query($sql5) or die(mysql_error()); 

if (mysql_num_rows($result) == 1) { 
   $row = mysql_fetch_assoc($result5); 
   echo '<EMBED SRC="/pages/videos/'.$row['Filename'].'" AUTOSTART=true width="490" height="380" controller="true"></EMBED>';  
} 
else { 
   while ($row = mysql_fetch_assoc($result5)) { 
      echo '<a href="/newsite/play2.php?ID='.$row['ID'].'">'.$row['Filename'].'</a> <br />'; 
   } 
} 
?>
</tr></td>
</table>
<!--END CONTENT-->
<?
include("/home/muot/public_html/site/includes/bottom.php");
?>
top.php

Code: Select all

<title>muot.net - <? echo $page ?></title>
	</head>
	<body>
		<div id=alltop>
			<div id=muot>
				muot.net
			</div>
			<div id=menu>
				<a class=alltop href=/site>main</a> :: <a class=alltop href=/site/muotPages/muotVideos.php>videos</a> :: <a class=alltop href=/site/muotPages/muotReport.php>muot report</a>
			</div>
		</div>
		<br>
		<span style="float:right;" id=ads>
			<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script>
		</span><br><br>
<div id=contents align=center>
bottom.php

Code: Select all

</div>
	</body>
</html>
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post by kyoru »

have you tried moving $row and the include under here?

Code: Select all

if (mysql_num_rows($result) == 1) { 
   $row = mysql_fetch_assoc($result5); 
   echo '<EMBED SRC="/pages/videos/'.$row['Filename'].'" AUTOSTART=true width="490" height="380" controller="true"></EMBED>';  
} 
else { 
   while ($row = mysql_fetch_assoc($result5)) { 
      echo '<a href="/newsite/play2.php?ID='.$row['ID'].'">'.$row['Filename'].'</a> <br />'; 
   } 
}
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

That has to be inbetween the includes.
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post by kyoru »

sorry i see what your trying to do now, i think you would have to set the mysql at the beginning, set the output to a variable. then assign the page header data and output the table with the variable. Like this...

Code: Select all

MySQL connection
-assign output

Retreive row[name]
-include top 

Output the table + output variable
-include bottom
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Your can use ob_start() to put the page in a buffer and then parse the data and replace something like %PAGETITLE% with the page title.

Code: Select all

function ParsePage($Buffer) {
  Global $PageData;
  $Buffer = str_replace("%WEBPAGE_TITLE%", $PageData['title'], $Buffer);
  $Buffer = str_replace("%REVISIT%", $PageData['revisit'], $Buffer);
  $Buffer = str_replace("%WEBPAGE_HEADING%", $PageData['heading'], $Buffer);
  $Buffer = str_replace("%DESCRIPTION%", $PageData['description'], $Buffer);
  $Buffer = str_replace("%KEYWORDS%", $PageData['keywords'], $Buffer);
  return($Buffer);
}

ob_start("ParsePage");

// content & code

ob_end_flush();
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

The title is a row in the database. It changes each time the ID is changed.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

bad designs should not be fixed with a bandaid approach.. better to do it right the first time ;)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

nickman013 wrote:The title is a row in the database. It changes each time the ID is changed.
With the code I wrote above you can set the page title in the footer, or anywhere you want to.

Code: Select all

$PageData['title'] = // your db query result
ob start goes at the very top and ob_end_flush goes below your footer.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks!

It works!!!
Post Reply