Chousho
Mon 18th Sep '06, 9:11pm
Hey there!
I set up a database for some testing with the book I bought a while ago from Sitepoint. Anyway, I have a table named "page" with 2 rows: pageid and content.
I am having some trouble creating a good way to extract the content via php.
This is what I'm currently working with. Keep in mind, I have a seperate file for the DB connection. Why won't it show the content at the end?
<?php
// extract the content from the database
$id = $_GET['id'];
$raw_content = @mysql_query("SELECT content FROM page WHERE pageid = '".mysql_real_escape_string($id)."' limit 1");
if (!$raw_content) {
exit ('Page does not exist or can not be found.');
}
if (mysql_fetch_array($raw_content) < 1) {
exit ('That id is not appropriate.');
}
$raw_content = mysql_fetch_array($raw_content);
$content = $raw_content['content'];
// Filter out HTML
$content = htmlspecialchars($content);
// If no page specified, default to the first page ($page = 0)
if(!isset($_GET['page'])) {
$page = 0;
} else {
$page = $_GET['page'];
}
// Split text into array of pages
$textarray = spliti('\[PAGEBREAK]', $content);
// Select the page we want
$content = $textarray[$page];
// BBcode to HTML
// Text style - Bold, Italic, Underline
$content = str_replace (array ('', ''), '<strong>', $content);
$content = str_replace (array ('', ''), '</strong>', $content);
$content = str_replace (array ('', ''), '<em>', $content);
$content = str_replace (array ('', ''), '</em>', $content);
$content = str_replace (array ('', ''), '<span style="text-decoration: underline;">', $content);
$content = str_replace (array ('', ''), '</span>', $content);
// Super and subscript
$content = str_replace (array ('', ''), '<sup>', $content);
$content = str_replace (array ('', ''), '</sup>', $content);
$content = str_replace (array ('', ''), '<sub>', $content);
$content = str_replace (array ('', ''), '</sub>', $content);
// BBlinks to HTML
$content = eregi_replace ('\\([-_./a-z0-9!&%#?+,\'=:;@~]+)\\', '<a href="\\1">\\1</a>', $content);
$content = eregi_replace ('\\+)]([^\\[]+)\\ (([-_./a-z0-9!&%#?+,\'=:;@~)', '<a href="\\1">\\2</a>', $content);
// Paragraphs and line breaks
$content = ereg_replace("\r\n", "\n", $content);
$content = ereg_replace("\r", "\n", $content);
$content = ereg_replace("\n\n", '</p><p>', $content);
$content = ereg_replace("\n", '<br />', $content);
$PHP_SELF = $_SERVER['PHP_SELF'];
if ($page != 0) {
$prevpage = $page -1;
echo "<p><a href=\"$PHP_SELF?id=$id&page=$prevpage\">".'Previous Page</a></p>';
}
echo "<p>$content</p>";
if ($page < count($textarray) - 1) {
$nextpage = $page + 1;
echo "<p><a href=\"$PHP_SELF?id=$id&page=$nextpage\">".'Next Page</a></p>';
}
?>
I set up a database for some testing with the book I bought a while ago from Sitepoint. Anyway, I have a table named "page" with 2 rows: pageid and content.
I am having some trouble creating a good way to extract the content via php.
This is what I'm currently working with. Keep in mind, I have a seperate file for the DB connection. Why won't it show the content at the end?
<?php
// extract the content from the database
$id = $_GET['id'];
$raw_content = @mysql_query("SELECT content FROM page WHERE pageid = '".mysql_real_escape_string($id)."' limit 1");
if (!$raw_content) {
exit ('Page does not exist or can not be found.');
}
if (mysql_fetch_array($raw_content) < 1) {
exit ('That id is not appropriate.');
}
$raw_content = mysql_fetch_array($raw_content);
$content = $raw_content['content'];
// Filter out HTML
$content = htmlspecialchars($content);
// If no page specified, default to the first page ($page = 0)
if(!isset($_GET['page'])) {
$page = 0;
} else {
$page = $_GET['page'];
}
// Split text into array of pages
$textarray = spliti('\[PAGEBREAK]', $content);
// Select the page we want
$content = $textarray[$page];
// BBcode to HTML
// Text style - Bold, Italic, Underline
$content = str_replace (array ('', ''), '<strong>', $content);
$content = str_replace (array ('', ''), '</strong>', $content);
$content = str_replace (array ('', ''), '<em>', $content);
$content = str_replace (array ('', ''), '</em>', $content);
$content = str_replace (array ('', ''), '<span style="text-decoration: underline;">', $content);
$content = str_replace (array ('', ''), '</span>', $content);
// Super and subscript
$content = str_replace (array ('', ''), '<sup>', $content);
$content = str_replace (array ('', ''), '</sup>', $content);
$content = str_replace (array ('', ''), '<sub>', $content);
$content = str_replace (array ('', ''), '</sub>', $content);
// BBlinks to HTML
$content = eregi_replace ('\\([-_./a-z0-9!&%#?+,\'=:;@~]+)\\', '<a href="\\1">\\1</a>', $content);
$content = eregi_replace ('\\+)]([^\\[]+)\\ (([-_./a-z0-9!&%#?+,\'=:;@~)', '<a href="\\1">\\2</a>', $content);
// Paragraphs and line breaks
$content = ereg_replace("\r\n", "\n", $content);
$content = ereg_replace("\r", "\n", $content);
$content = ereg_replace("\n\n", '</p><p>', $content);
$content = ereg_replace("\n", '<br />', $content);
$PHP_SELF = $_SERVER['PHP_SELF'];
if ($page != 0) {
$prevpage = $page -1;
echo "<p><a href=\"$PHP_SELF?id=$id&page=$prevpage\">".'Previous Page</a></p>';
}
echo "<p>$content</p>";
if ($page < count($textarray) - 1) {
$nextpage = $page + 1;
echo "<p><a href=\"$PHP_SELF?id=$id&page=$nextpage\">".'Next Page</a></p>';
}
?>