Forum Moderators: coopster
I am building a very simple articles based website and I want to create a single page that displays the associated article.
ill let the code do the talking as its difficult to explain.
if(isset($_GET['id']))
{
$articleID = $_GET['id'];
$query = "SELECT * FROM ps_articles WHERE ID='$articleID'";
$result = mysql_query($query);
$num = mysql_num_rows($result);if ($num == 0)
{
echo('<p>The selected article was not found. <a href="index.php">Back to open source</a></p>');
}
else
{
GetArticleSource($articleID, $result);
}
}
else
{
header("Location:index.php");
}
function GetArticleSource($id)
{
$file=fopen("articles/" . $id . "/p1.php","r") or exit("<p>Unable to get the selected article.</p>");
while(!feof($file))
{
echo fgets($file);
}
fclose($file);
}
Ok here goes.
The php file (articles/1/p1.php for example) itself contains php. but when its echo'd, its echo'd as HTML and not as php so it just appears as a load of random text on the page. what I want is for the page to be displayed as is and php code to be parsed as-is. How can I go about doing this?
thanks
here is what ive got
$file=fopen("articles/" . $id . "/p" . $id . "_php.php","r") or exit("<p>Unable to get the selected article.</p>"); $str = "";
while(!feof($file))
{
$str .= fgets($file) . ";";
}
fclose($file);
eval($str);
$file=fopen("articles/" . $id . "/p" . $id . ".php","r") or exit("<p>Unable to get the selected article.</p>");
while(!feof($file))
{
$line = "". fgets($file);
if (strlen(strstr($line,"<?php"))>0)
{
eval ($line . ";");
}
else
{
echo("" . $line);
}
}
fclose($file);
the first loop appears to work correctly.
the second loop is for the actual body of the html code. within, there are little php snippets, here is an example:
<div class="curlycontainer">
<div class="innerdiv">
<?php GetCode3();?>
</div>
</div>
so the second loop looks for the php code, if found, does eval otherwise does echo. although it seems to work kinda (there is no php code in the source of the document) just blank spaces appear where the result should be.
where am i going wrong?
function GetArticleSource($id)
{
$file=fopen("articles/" . $id . "/p1.php","r") or exit("<p>Unable to get the selected article.</p>");
while(!feof($file))
{
echo htmlentities(fgets($file));
}
fclose($file);
}
I think that should also work. Unless it wasn't working for some other reason. But reading the way in my previous code is a lot easier as long as you can base your output on carriage returns within the file. Thats how the file() function works. It creates an array with each line as a separate value. Let me know if you need anymore help, I had to work alot with most of the file functions at my previous job.