macfall Posted June 22, 2011 Share Posted June 22, 2011 I am publishing some of my and another writer's fiction at my website, Fledge Press. I use GET_ and include() to bring up the chapter I want to display. Here is a link to the first chapter of the main story, for an example: http://fledgepress.com/book.php?story=1&chapter=1 And here is the code I used for the online reader (book.php): <?php $story = $_GET['story']; switch ($story) { case '1': include("title_01.php"); break; case '2': include("title_02.php"); break; case '3': include("title_03.php"); break; default: include("title_00.php"); break; } ?> <div id="storyText"> <?php $chapter = $_GET['chapter']; switch ($story) { case '1': switch ($chapter) { case '0': include("burningrockranch_latest.php"); break; case '1': include("burningrockranch_01.php"); break; case '2': include("burningrockranch_02.php"); break; case '3': include("burningrockranch_03.php"); break; default: include("burningrockranch_toc.php"); break; } break; case '2': switch ($chapter) { case '0': include("novantchildren_latest.php"); break; case '1': include("novantchildren_01.php"); break; case '2'; include("novantchildren_02.php"); break; default: include("novantchildren_toc.php"); break; } case '3': switch ($chapter) { case '0': include("vigilance_latest.php"); break; case '1': include("vigilance_01.php"); break; case '2': include("vigilance_02.php"); break; default: include("vigilance_toc.php"); break; } default: echo "An error has occured!"; } ?> I also use PHP pagination for the navigation bar at the bottom: <?php $total1 = 3; $total2 = 1; $total3 = 2; $story = $_GET['story']; $chapter = $_GET['chapter']; $chapter = ereg_replace( "[^0-9]", "", $chapter ); if ($story == 1) $total=$total1; elseif ($story == 2) $total=$total2; elseif ($story == 3) $total=$total3; $next = $previous = ''; if ( $chapter > 1 ) { $first = '<a href="http://fledgepress.com/book.php?story='. $story .'&chapter=1"><img src="/images/start.jpg" alt="First chapter" title="Go back to the first chapter"></a>'; $previous = $chapter - 1; $previous = '<a href="http://fledgepress.com/book.php?story='. $story .'&chapter=' . $previous . '"> <img src="/images/lastpage.jpg" alt="Last chapter" title="Skip to the previous chapter"></a>'; } else { $first = '<img src="/images/start.jpg" alt="First chapter" title="Go back to the first chapter">'; $previous = '<img src="/images/lastpage.jpg" alt="Last chapter" title="Skip to the previous chapter">'; } if ( $chapter < $total ){ $next = $chapter + 1; $next = '<a href="http://fledgepress.com/book.php?story='. $story .'&chapter=' . $next . '"><img src="/images/nextpage.jpg" alt="Next chapter" title="Skip to the next chapter"></a>'; } else { $next = '<img src="/images/nextpage.jpg" alt="Next chapter" title="Skip to the next chapter">'; } if ( $chapter != 0 ){ $latest = '<a href="http://fledgepress.com/book.php?story=' . $story .'&chapter=' . $total .'"><img src="/images/latest.jpg" alt="Latest chapter" title="Skip to the newest chapter"></a>'; } else { $latest = '<img src="/images/latest.jpg" alt="Latest chapter" title="Skip to the newest chapter">'; } echo $first . $previous . '<a href="/book.php?story='. $story . '&chapter="><img src="/images/list.jpg" alt="Table of contents" title="Table of contents"></a>' . $next . $latest; ?> Now if you click through the chapters of the Burning Rock Ranch (which corresponds to $story='1' in the URL), there aren't any problems. However, if you go to the other stories, weird things start to happen. Novant Children ($story='2') brings up the desired chapter, but immediately afterwards includes the first chapter of the next story (vigilance_01.php): http://fledgepress.com/book.php?story=2&chapter=1 And the table of contents for Novant Children, included as "novantchildren_toc.php", also includes what appears to be "vigilance_toc.php" from the third story. The third story, after each chapter, includes the default error from the enclosing case of the $story switch: "An error has occured!" http://fledgepress.com/book.php?story=3&chapter=1 And finally, the table of contents for Vigilance does the same thing. I have scoured my code and cannot figure out why this is. I'm pretty new to PHP, so it is probably a stupid syntax mistake, but I sure as heck can't find it. Quote Link to comment https://forums.phpfreaks.com/topic/240149-php-include-errors-including-stuff-that-i-dont-want-and-didnt-tell-it-to/ Share on other sites More sharing options...
LLLLLLL Posted June 22, 2011 Share Posted June 22, 2011 One thing I notice is that there's no "break" after your default case. Quote Link to comment https://forums.phpfreaks.com/topic/240149-php-include-errors-including-stuff-that-i-dont-want-and-didnt-tell-it-to/#findComment-1233524 Share on other sites More sharing options...
macfall Posted June 22, 2011 Author Share Posted June 22, 2011 That did it. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/240149-php-include-errors-including-stuff-that-i-dont-want-and-didnt-tell-it-to/#findComment-1233543 Share on other sites More sharing options...
PFMaBiSmAd Posted June 22, 2011 Share Posted June 22, 2011 This is slightly off-topic, but you should be using a database or some other data structure, such as an array to define the story/chapter relationship so that you DON'T need to go into your program and edit the logic every time you add a story or add a chapter to an existing story. The logic in your code should be general purpose so that it can operate on any amount of data, simply by changing the amount of data. Also, you should be storing the contents itself in a database so that you can use ONE page, with your existing URL's, so that you don't need to keep creating new .php files for every piece of content that makes up your site. By having one page, with only one set of page markup/css, you keep all the page formatting in one place so that you will have a consistent looking site and if you decide to change the appearance or formatting, you only have to change it once instead of on every actual page/file you have created. Short-answer: You are using a programming language, let php do the work for you, instead of you manually editing files to add/change switch/case statements and continually copy/paste files and editing the content in them. Quote Link to comment https://forums.phpfreaks.com/topic/240149-php-include-errors-including-stuff-that-i-dont-want-and-didnt-tell-it-to/#findComment-1233574 Share on other sites More sharing options...
macfall Posted June 22, 2011 Author Share Posted June 22, 2011 I know I should, but I'm still learning about that. And it took me three months to figure out what I'm doing now. I'm not a natural programmer. I failed Q-Basic twice in high school and Java once in college. On the other hand, I'm persistent, so I will keep working to improve what I'm doing: but in the meantime, I'm just going with what works. If you can think of a good tutorial for building the sort of database I need, keeping in mind that this sort of thing does not come naturally to me, I would like to take a look at it. Quote Link to comment https://forums.phpfreaks.com/topic/240149-php-include-errors-including-stuff-that-i-dont-want-and-didnt-tell-it-to/#findComment-1233596 Share on other sites More sharing options...
PFMaBiSmAd Posted June 23, 2011 Share Posted June 23, 2011 Here is some functional database driven code that demonstrates what I suggested and is completely independent of the actual number of stories or chapters. I used different file names for the pagination images from what you used (simply change them back to your names) and I fixed one logic problem in the pagination code. <?php // connect to database server and select database here... mysql_connect('localhost','your_user','your_password'); mysql_select_db('your_db'); function pagination($total,$story,$chapter){ $next = $previous = ''; if($chapter > 1){ // not at the first one, first and previous are active $first = "<a href='?story=$story&chapter=1'><img src='/images/start.gif' alt='First chapter' title='Go back to the first chapter'></a>"; $previous = $chapter - 1; $previous = "<a href='?story=$story&chapter=$previous'><img src='/images/prev.gif' alt='Last chapter' title='Skip to the previous chapter'></a>"; } else { // at the first one, first and previous are not active $first = '<img src="/images/start.gif" alt="First chapter" title="Go back to the first chapter">'; $previous = '<img src="/images/prev.gif" alt="Last chapter" title="Skip to the previous chapter">'; } if($chapter < $total){ // not at the last one, next and last are active $next = $chapter + 1; $next = "<a href='?story=$story&chapter=$next'><img src='/images/next.gif' alt='Next chapter' title='Skip to the next chapter'></a>"; $last = "<a href='?story=$story&chapter=$total'><img src='/images/end.gif' alt='Latest chapter' title='Skip to the newest chapter'></a>"; } else { // at the last one, next and last are not active $next = '<img src="/images/next.gif" alt="Next chapter" title="Skip to the next chapter">'; $last = '<img src="/images/end.gif" alt="Latest chapter" title="Skip to the newest chapter">'; } return $first . $previous . "<a href='?story=$story'><img src='/images/toc.gif' alt='Table of contents' title='Table of contents'></a>" . $next . $last; } // produce main menu, as needed... $top_nav = "<a href='?toc'>List The Available Stories</a>\n"; // if story is not set, list toc of all the stories (gotten from the story table information) // if story (id number) is set, get the story info (title, author, other, ...) // if the requested story does not exist, clear url and request the base page $story = isset($_GET['story']) ? (int)$_GET['story'] : ''; // non-int parameter gives a zero value and redisplays the toc (via redirect to base page) if(is_numeric($story) && $story >= 0){ // get story info (if exists) and get requested chapter (if exists) $query = "SELECT title FROM stories WHERE id = $story"; $result = mysql_query($query); if(!mysql_num_rows($result)){ // requested story was not found header("Location: {$_SERVER['SCRIPT_NAME']}"); // clear the url exit; } else { // story was found, fetch the story information (title) and get the requested chapter information // if the chapter number is not set, display the chapters (gotten from the table information) // if the chapter is a number, get and display the chapter title and content // if the chapter number is not valid, clear the chapter and display the list of chapters $story_info = mysql_fetch_assoc($result); $page_title = "{$story_info['title']}"; //---- start chapter section --------------------------------------------- $chapter = isset($_GET['chapter']) ? $_GET['chapter'] : ''; // non numeric chapter/not empty - remove chapter from the url if(!is_numeric($chapter) && !empty($chapter)){ header("Location: {$_SERVER['SCRIPT_NAME']}?story=$story"); // clear the url exit; } // chapter will be numeric or an empty string at this point if(is_numeric($chapter)){ // get chapter info (if exists) $query = "SELECT title,content FROM chapters WHERE story_id = $story AND chapter = $chapter"; $result = mysql_query($query); if(!mysql_num_rows($result)){ // requested chapter was not found header("Location: {$_SERVER['SCRIPT_NAME']}?story=$story"); // clear the url exit; } else { // chapter was found, fetch the chapter information (title,content) $chapter_info = mysql_fetch_assoc($result); $page_title .= ", {$chapter_info['title']}"; $main_content = "<p>{$story_info['title']}, {$chapter_info['title']} -</p>\n<p>{$chapter_info['content']}</p>\n"; // get count of chapters for pagination $query = "SELECT count(*) as num_chapters FROM chapters WHERE story_id = $story AND chapter > 0"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $bottom_nav = pagination($row['num_chapters'],$story,$chapter); } } else { // chapter was not set, get toc $query = "SELECT chapter,title FROM chapters WHERE story_id = $story ORDER BY chapter"; $result = mysql_query($query); if(!mysql_num_rows($result)){ // no chapters found $toc_content = "<p>{$story_info['title']} -</p><p>Sorry, there are no chapters for this story!</p>\n"; $page_title .= " - no chapters!"; } else { // found one or more chapters, build toc $toc_content = "<p>{$story_info['title']} -</p>\n<ol>\n"; while($row = mysql_fetch_assoc($result)){ if($row['chapter'] == 0){ $toc_content .= "<a href='?story=$story&chapter={$row['chapter']}'>{$row['title']}</a>\n"; } else { $toc_content .= "<li><a href='?story=$story&chapter={$row['chapter']}'>{$row['title']}</a></li>\n"; } } $toc_content .= "</ol>\n"; } } //--- end chapter section ---------------------------------------------- } } else { // story was not set, get main toc $query = "SELECT id,title FROM stories ORDER BY title"; $result = mysql_query($query); if(!mysql_num_rows($result)){ // no stories found $toc_content = "<p>Sorry, there are no stories!</p>\n"; $page_title = "Sorry, there are no stories!"; } else { // found one or more stories, build toc $toc_content = "<p>Stories -</p>\n<ol>\n"; while($row = mysql_fetch_assoc($result)){ $toc_content .= "<li><a href='?story={$row['id']}'>{$row['title']}</a></li>\n"; } $toc_content .= "</ol>\n"; $page_title = "List of Stories"; } } ?> <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title><?php echo isset($page_title) ? $page_title : ''; ?></title> <style type="text/css"> </style> </head> <body> <?php echo isset($top_nav) ? $top_nav : ''; // main navigation menu ?> <?php echo isset($toc_content) ? $toc_content : ''; // if no story selected, display the list of stories or if no chapter selected, display list of chapters ?> <div id="storyText"> <?php echo isset($main_content) ? $main_content : ''; // story title, chapter title, chapter content ?> </div> <?php echo isset($bottom_nav) ? $bottom_nav : ''; // chapter navigation ?> </body> </html> The stories table has an id (int) and title (varchar) columns (you would add any other things you need, such as author, published date,...) The chapters table has an id (int), story_id (int), chapter (int), title (varchar), and content (varchar) columns. I don't think you are going to find any tutorials that would directly help (other than the basics, here is how you connect to a database server, select a database, form a query that selects the data you want in the order that you want it, execute a query, fetch the result from the query, and use the result the way you want to) because this is all pure programming. You define what you want to accomplish, what you want it to look like, and what data you need and then write and test the code until you are done. Edit: Please excuse any of the un-needed double-quotes that are around php variables with nothing else inside of the quotes. I originally had some literal text as part of those strings, but removed it and then left the quotes as is. Quote Link to comment https://forums.phpfreaks.com/topic/240149-php-include-errors-including-stuff-that-i-dont-want-and-didnt-tell-it-to/#findComment-1233700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.