PDA

View Full Version : [SOLVED] help with HTML's pre tag


rbprogrammer
November 23rd, 2008, 12:43 AM
Ok, so I have this code in a PHP script on my server:

$body = $_GET['body'];
if( $body == "source" )
{
println("<html>");
println("<head>");
println("<title>Website Source Code</title>");
println("</head>");
println("<body>");
println("<pre>");
$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
$lines = file($pfile);
if( $lines != false )
{
$numbers = "";
$codelines = "";
foreach ($lines as $line_num => $line)
{
$numbers = $numbers . ($line_num+1) . " : <br/>\n";
$codelines = $codelines . htmlspecialchars($line) . "<br/>\n";
}
println("<table>");
println(" <tr>");
println(" <td>" . $numbers . "</td>");
println(" <td>" . $codelines . "</td>");
println(" </tr>");
println("</table");
}
println("</pre>");
println("</body>");
println("</html>");
exit;
}

Basically, when the variable body equals "source", I want to display the source code of that PHP script. But, for some reason, whitespace is not being displayed in the browser. Meaning all the code is at one level with no indentations. Looking at the source code, there are the spaces there. But for some reason, firefox is not showing it. What am I doing wrong? I though the pre tage was supposed to preserve white space and display it....

slavik
November 23rd, 2008, 01:28 AM
do you know about <? ?> ? they show where PHP code is and where it is not.

drubin
November 23rd, 2008, 07:02 AM
do you know about <? ?> ? they show where PHP code is and where it is not.

Simple example of that.
<html>
<head>
<title<?php echo $title;?></title>
</head>
<body>
<?php echo 'This is some random text;'?>
</body>
<html>

This will show what is php code and what is html code. It makes your code easier to read and follow.

drubin
November 23rd, 2008, 07:05 AM
$body = $_GET['body'];
if( $body == "source" )
{
println("<html>");
println("<head>");
println("<title>Website Source Code</title>");
println("</head>");
println("<body>");
// println("<pre>"); << HERE
$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
$lines = file($pfile);
if( $lines != false )
{
$numbers = "";
$codelines = "";
foreach ($lines as $line_num => $line)
{
$numbers = $numbers . ($line_num+1) . " : <br/>\n";
$codelines = $codelines . htmlspecialchars($line) . "<br/>\n";
}
println("<table>");
println(" <tr>");
println(" <td>" . $numbers . "</td>");
println(" <td><pre>" . $codelines . "</pre></td>");// << HERE
println(" </tr>");
println("</table");
}
// println("</pre>");<< HERE
println("</body>");
println("</html>");
exit;
}

Basically, when the variable body equals "source", I want to display the source code of that PHP script. But, for some reason, whitespace is not being displayed in the browser. Meaning all the code is at one level with no indentations. Looking at the source code, there are the spaces there. But for some reason, firefox is not showing it. What am I doing wrong? I though the pre tage was supposed to preserve white space and display it....

So I have re-looked at it. I think you have the pre in the wrong place. try move it to where I suggested. The pre tags wont exactly work when you have html code in side them.

rbprogrammer
November 26th, 2008, 04:08 PM
Ok, that worked perfectly. For the record, this is what I will be using:

$COURSE = "HIST/STS::151";
$BODY = $_GET['body'];
function println( $str )
{
echo $str . "\n";
}
if( $BODY == "source" )
{
println("<html>");
println("<head>");
println("<title>" . $COURSE . " - Website Source Code</title>");
println("</head>");
println("<body>");
$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
$lines = file($pfile);
if( $lines != false )
{
$numbers = "";
$codelines = "";
foreach ($lines as $line_num => $line)
{
$numbers = $numbers . ($line_num+1) . ": \n";
$codelines = $codelines . htmlspecialchars($line);
}
println("<table>");
println(" <tr>");
println(" <td><pre>" . $numbers . "</pre></td>");
println(" <td><pre>" . $codelines . "</pre></td>");
println(" </tr>");
println("</table");
}
println("</body>");
println("</html>");
exit;
}


Among other things, this is just the source code part that I will be using to display the source code of the php page. Obviously there are other parts to my file.