Page 1 of 1

Possible To Echo More PHP Code?

Posted: Wed Dec 13, 2006 12:40 pm
by Superman859
I've run across a problem, and am unsure of how to go about fixing it.

I draw all my HTML code from a MySQL Database and print it using an echo statement.

However, I recently was editing the HTML code, and realized it would be super easy to speed things up in the HTML portion by using a small piece of PHP...Basically I create a whole bunch of Javascript variables (for Flash) with method calls and I simply increment a number each time for the most part. So in my HTML code, I used the following code:

Code: Select all

<?php 
$lesson = "sotv";
$number = 01; 
include("../flash/audio_swfobject_include.php");
?>
What this would do is I could change the $lesson to whatever I needed to. Each page has a unique lesson. I would change the $number each time I placed this PHP code in the HTML file - it would increment a number for me...the include file is small and as follows:

Code: Select all

<div id="<?php echo lesson , "-", number;?>"><a href="http://www.learn-korean-now.com/flash_update.php">UF</a></div>
<script type="text/javascript">
   var <?php echo lesson,number; ?> = new SWFObject("../flash/small_button_template.swf", "<?php echo lesson , "-", number;?>", "24", "24", "8", "#ffffcc");
   <?php echo lesson,number; ?>.addVariable("audioName","audio/<?php echo lesson , "-", number;?>.mp3");
   <?php echo lesson,number; ?>.write("<?php echo lesson , "-", number;?>");
</script>
However, I put it online on the server and did a test. What happens is it prints out the PHP to the HTML source code, rather than processing it as PHP. I assume this is because it already processed PHP when it was writing all of this HTML (with a little PHP), and didn't check the HTML that it wrote for more PHP...

I'm just wondering if it is possible at all to pull this off with PHP. I'm using this one Flash file over 50 times per page, and basically change the number each time I use it. If you look at the include code above, I have to change the number 7 times each time I use it. That's changing 350 numbers, when I could simply change it 50 times. With PHP, I thought I could simply change it one time as a variable, and have it change it in the seven necessary places for me.

It's just a problem because I'm trying to process PHP code after I already processed PHP code (the first time is printing all the HTML that also contains these mini clips of PHP).

Is there any way I can pull this off? Otherwise it's going to take me hours upon hours to simply change numbers.

An example of what I'm trying to reproduce 50 times per page but changing a number is below (without any PHP)...

Code: Select all

<div id="evv-01"><a href="http://www.learn-korean-now.com/flash_update.php">UF</a></div>
<script type="text/javascript">
   var evv01 = new SWFObject("../flash/small_button_template.swf", "evv-01", "24", "24", "8", "#ffffcc");
   evv01.addVariable("audioName","audio/evv-01.mp3");
   evv01.write("evv-01");
</script>
for the next Flash audio clip, it'd be evv-02 and evv02 that I would be using, all the way up to 50+ per page.[/syntax]

Posted: Wed Dec 13, 2006 12:59 pm
by Christopher
It should be:

Code: Select all

<?php echo $lesson . $number; ?>

Posted: Wed Dec 13, 2006 12:59 pm
by John Cartwright
This is commonly referred to as templating. What you typically will do is have flags to indicate a value needs to be processed..

I'll use your html block as an example

Code: Select all

<div id="{LESSON}-{NUMBER}"><a href="http://www.learn-korean-now.com/flash_update.php">UF</a></div>
<script type="text/javascript">
   var {LESSON}{NUMBER} = new SWFObject("../flash/small_button_template.swf", "{LESSON}-{NUMBER}", "24", "24", "8", "#ffffcc");
   {LESSON}{NUMBER}.addVariable("audioName","audio/{LESSON}-{NUMBER}.mp3");
   {LESSON}{NUMBER}.write("{LESSON}-{NUMBER}");
</script>
$mixed = array('{LESSON}', '{NUMBER}');
$replace = array($row['lesson'], $row['number']);

echo str_replace($mixed, $replace, $code);

I don't know if you can get away with html is eval(), but that could potentially be a quick fix to this, although your system would become vulnerable to attack if someone could maliciously enter evil php code.

Posted: Wed Dec 13, 2006 1:26 pm
by Superman859
Hmm I'm a bit confused.

Based on your code, if I were to replace lesson with fv, and number with 01, wouldn't that replace all places that said {LESSON} or {NUMBER}?

Here is a larger chunk of the actual HTML code...

Code: Select all

<table class="vocab">
<tr>
<th>Korean</th>
<th>English</th>
</tr>
<tr>
<td>네</td>
<td>yes</td>
<td><div id="fv01"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so01 = new SWFObject("../flash/small_button_template.swf", "fv01", "24", "24", "8", "#ffffcc");
   so01.addVariable("audioName","audio/fv01.mp3");
   so01.write("fv01");
</script>
</td>
</tr>
<tr>
<td>아니오</td>
<td>no</td>
<td><div id="fv02"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so02 = new SWFObject("../flash/small_button_template.swf", "fv02", "24", "24", "8", "#ffffcc");
   so02.addVariable("audioName","audio/fv02.mp3");
   so02.write("fv02");
</script>
</td>
</tr>
<tr>
<td>안녕하세요</td>
<td>hello</td>
<td><div id="fv03"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so03 = new SWFObject("../flash/small_button_template.swf", "fv03", "24", "24", "8", "#ffffcc");
   so03.addVariable("audioName","audio/fv03.mp3");
   so03.write("fv03");
</script>
</td>
</tr>
<tr>
<td>안녕히 가세요*</td>
<td>goodbye</td>
<td><div id="fv04"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so04 = new SWFObject("../flash/small_button_template2.swf", "fv04", "24", "24", "8", "#ffffcc");
   so04.addVariable("audioName","audio/fv04.mp3");
   so04.write("fv04");
</script>
</td>
</tr>
<tr>
<td>안녕히 계세요*</td>
<td>goodbye</td>
<td><div id="fv05"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so05 = new SWFObject("../flash/small_button_template.swf", "fv05", "24", "24", "8", "#ffffcc");
   so05.addVariable("audioName","audio/fv05.mp3");
   so05.write("fv05");
</script>
</td>
</tr>
<tr>
<td>감사합니다*</td>
<td>thank you</td>
<td><div id="fv06"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so06 = new SWFObject("../flash/small_button_template.swf", "fv06", "24", "24", "8", "#ffffcc");
   so06.addVariable("audioName","audio/fv06.mp3");
   so06.write("fv06");
</script>
</td>
</tr>
<tr>
<td>고맙습니다*</td>
<td>thank you</td>
<td><div id="fv07"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so07 = new SWFObject("../flash/small_button_template.swf", "fv07", "24", "24", "8", "#ffffcc");
   so07.addVariable("audioName","audio/fv07.mp3");
   so07.write("fv07");
</script>
</td>
</tr>
<tr>
<td>괜찮아요</td>
<td>it's alright / your welcome</td>
<td><div id="fv08"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so08 = new SWFObject("../flash/small_button_template.swf", "fv08", "24", "24", "8", "#ffffcc");
   so08.addVariable("audioName","audio/fv08.mp3");
   so08.write("fv08");
</script>
</td>
</tr>
<tr>
<td>미안해요</td>
<td>I'm sorry</td>
<td><div id="fv09"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so09 = new SWFObject("../flash/small_button_template.swf", "fv09", "24", "24", "8", "#ffffcc");
   so09.addVariable("audioName","audio/fv09.mp3");
   so09.write("fv09");
</script>
</td>
</tr>
<tr>
<td>실례합니다</td>
<td>excuse me</td>
<td><div id="fv10"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so10 = new SWFObject("../flash/small_button_template.swf", "fv10", "24", "24", "8", "#ffffcc");
   so10.addVariable("audioName","audio/fv10.mp3");
   so10.write("fv10");
</script>
</td>
</tr>
<tr>
<td>그래요</td>
<td>really? is that so?</td>
<td><div id="fv11"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so11 = new SWFObject("../flash/small_button_template.swf", "fv11", "24", "24", "8", "#ffffcc");
   so11.addVariable("audioName","audio/fv11.mp3");
   so11.write("fv11");
</script>
</td>
</tr>
<tr>
<td>뭐</td>
<td>what</td>
<td><div id="fv12"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so12 = new SWFObject("../flash/small_button_template.swf", "fv12", "24", "24", "8", "#ffffcc");
   so12.addVariable("audioName","audio/fv12.mp3");
   so12.write("fv12");
</script>
</td>
</tr>
</table>
All of this, (plus more using the same pattern), is stored in a MySQL database specific row and column. I want to speed up the process of changing all those values from fv01 to fv02 to fv03, etc...Otherwise it takes forever. I cannot simply make one change to lesson/number for each page, or else the same audio file would be played every time. I need to be able to change number every time I use that Javascript code on the page, but don't want to have to change it 7 times within the Javascript. I would prefer to only make the change once each time I use the Javascript and have that change be reproduced in the Javascript itself.

When I create a page, it searches the database for the row that has the same pagename, and then finds the column titled 'body'. That is where the HTML body is found, which is where all this would be located.

Here is the main chunk of the PHP code used to create pages...each echo row[1] or echo row[2] is referring to parts of the site. The first few build the navbar. Then another builds a news section at the top of the site, another the login section, and another builds the body. The body is what I'm interested in here.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title>Learn Korean Now</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" href="http://www.learn-korean-now.com/files/class.css" type="text/css" />
<?php
if ($prompt === "1"){
echo "<script language=\"JavaScript\">";
echo "alert(\"This page has been installed into the database.\");";
echo "</script>";
}
?>
</head>
<body>
<div id="wrapper">
<table width="725" cellspacing="0" cellpadding="0" border="0">
<tr><td colspan="2"><img src="http://www.learn-korean-now.com/files/imgs/header.jpg" alt="" /></td></tr>
<tr><td width="170" valign="top" background="http://www.learn-korean-now.com/files/imgs/columnbg.jpg">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td><img src="http://www.learn-korean-now.com/files/imgs/1_top.jpg" alt="" /></td></tr>
<tr><td background="http://www.learn-korean-now.com/files/imgs/1bg.jpg">
<!-- Iterate the links down and repeat the bg -->
<?php
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[1];
?>
</td></tr>
<tr><td><img src="http://www.learn-korean-now.com/files/imgs/1_bot.jpg" alt="" /></td></tr>
<tr><td><img src="http://www.learn-korean-now.com/files/imgs/2_top.jpg" alt="" /></td></tr>
<tr><td background="http://www.learn-korean-now.com/files/imgs/linkbg.jpg">
<!-- Iterate the links down and repeat the bg -->
<?php
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[2];
?>
</td></tr>
<tr><td><img src="http://www.learn-korean-now.com/files/imgs/2_bot.jpg" alt="" /></td></tr>
<tr><td><img src="http://www.learn-korean-now.com/files/imgs/3_top.jpg" alt="" /></td></tr>
<tr><td background="http://www.learn-korean-now.com/files/imgs/linkbg.jpg">
<!-- Iterate the links down and repeat the bg -->
<?php
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[3];
?>
</td></tr>
<tr><td><img src="http://www.learn-korean-now.com/files/imgs/3_bot.jpg" alt="" /></td></tr>
<tr><td><img src="http://www.learn-korean-now.com/files/imgs/4_top.jpg" alt="" /></td></tr>
<tr><td background="http://www.learn-korean-now.com/files/imgs/linkbg.jpg">
<!-- Iterate the links down and repeat the bg -->
<?php
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[4];
?>
</td></tr> <tr><td><img src="http://www.learn-korean-now.com/files/imgs/4_bot.jpg" alt="" /></td></tr>
</table>
</td>
<td width="555" valign="top">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td><table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td width="374" height="165" valign="top" background="http://www.learn-korean-now.com/files/imgs/news.jpg">
<div class="news">
<?php
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[5];
?>
</div>
<div class="title">
<?php
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[7];
?>
</div>
</td>
<td width="181" height="165" valign="top" background="http://www.learn-korean-now.com/files/imgs/login.jpg">
<?php
if(!isset($_SESSION['user_id'])) {
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[6];
} else {
echo "<div style=\"margin-left: 20px; margin-top: 30px; \">";
if($_SESSION['member'] == 1) {
echo "Welcome Back, <br /> <b>{$_SESSION['username']}</b>!";
} else {
echo "Welcome To <br />Learn Korean Now, <b>{$_SESSION['username']}</b>";
}
echo "<form action=\"../phpBB2/login.php". $_SESSION['sid'] ."\" method=\"POST\">
<input type=\"hidden\" name=\"redirect\" value=\"../index.php\">
<input type=\"submit\" name=\"logout\" value=\"Logout\">
</form>";
echo "</div>";
}
?>
</td></tr>
</table>
</td></tr>
<tr><td>
<div class="body">
<?php
$query = "SELECT * FROM site_info WHERE pagename = '$filename'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[8];
?>
</div>
</td></tr>
</table>
</td>
</tr>
</table>
<img src="http://www.learn-korean-now.com/files/imgs/footer.jpg" alt="Copyright Learn Korean Now" /><br /> 
</div>
</body>
</html>
When I tried using PHP to do this, it didn't process the PHP, but came out like the following little snippet in the HTML source code...

Code: Select all

<td><?php 
$lesson = "fv";
$number = 03; 
include("../flash/audio_swfobject_include.php");
?></td>   
<td>어머니</td>

<td>mother</td>
</tr>
<tr>
<td><?php 
$lesson = "fv";
$number = 04; 
include("../flash/audio_swfobject_include.php");
?></td>   

Posted: Wed Dec 13, 2006 1:44 pm
by John Cartwright
Superman859 wrote:Hmm I'm a bit confused.

Based on your code, if I were to replace lesson with fv, and number with 01, wouldn't that replace all places that said {LESSON} or {NUMBER}?
If you have all your html stored in a single row then yes. If you iterate through your recordset and output as you go along, no.

Code: Select all

$mixed = array('{LESSON}', '{NUMBER}');
while ($row = mysql_fetch_assoc($result)) 
{
   $replace = array($row['lesson'], $row['number']);

   echo str_replace($mixed, $replace, $code); 
}

Posted: Wed Dec 13, 2006 2:19 pm
by Christopher
Or to use PHP as a template language, something like:

Code: Select all

<?php
$things = array(
     'fv01' => '&#45348;',
     'fv02' => '&#50500;&#45768;&#50724;',
// ... the others here
};

foreach ($things as $code => $text) {
?>
<tr>
<td><?php echo $text; ?></td>
<td>yes</td>
<td><div id="<?php echo $code; ?>"><a href="http://www.learn-korean-now.com/flash_update.php">Update Flash</a></div>
<script type="text/javascript">
   var so01 = new SWFObject("../flash/small_button_template.swf", "<?php echo $code; ?>", "24", "24", "8", "#ffffcc");
   so01.addVariable("audioName","audio/<?php echo $code; ?>.mp3");
   so01.write("<?php echo $code; ?>");
</script>
</td>
</tr>

<?php
}
?>

Posted: Wed Dec 13, 2006 7:53 pm
by RobertGonzalez
Superman859 wrote:When I tried using PHP to do this, it didn't process the PHP, but came out like the following little snippet in the HTML source code...

Code: Select all

<td><?php 
$lesson = "fv";
$number = 03; 
include("../flash/audio_swfobject_include.php");
?></td>   
<td>어머니</td>

<td>mother</td>
</tr>
<tr>
<td><?php 
$lesson = "fv";
$number = 04; 
include("../flash/audio_swfobject_include.php");
?></td>   
If you are getting PHP code in the source of the HTML then your server is not parsing PHP properly. Do you have a server upon which you are running this PHP file? Are you making sure to use long PHP tags (<?php)?