durian Posted April 18, 2011 Share Posted April 18, 2011 I'm working on a text-based web site with hundreds of chapters. I need a header/footer on most pages with a link to "previous chapter" and "next chapter". I don't want to code all these chapter links manually, it'll take forever. Is there any simple way of creating multiple internal links? Using CSS or Javascript perhaps? Link to comment Share on other sites More sharing options...
trevelluk Posted April 18, 2011 Share Posted April 18, 2011 With CSS, no, as that is only used to control the presentation of what's already on the page, rather than adding new things to the page.Javascript is a possibility, provided there is consistency in how the HTML files for each chapter are named. E.g. if the files are called something like "chapter1.html", "chapter2.html" then it shouldn't be too tricky. If this is the case then I can provide a few pointers (and I'm sure others will be along to help as well). If there isn't any consistency in the naming then I think you're out of luck with JS as well. Link to comment Share on other sites More sharing options...
jeffman Posted April 18, 2011 Share Posted April 18, 2011 FWIW, it's a bit risky to rely on JavaScript for your entire navigation system. A user with JavaScript disabled cannot use your site. A better solution would be to generate the links on the server side. As with JavaScript, the amount of code required to do this is minimal. (And, yes, the naming system must be consistent.) Link to comment Share on other sites More sharing options...
durian Posted April 19, 2011 Author Share Posted April 19, 2011 A better solution would be to generate the links on the server side.Pardon my ignorance, but how do I do this? Yes, most of the chapters are named ab01.htm, ab02.htm, etc. Link to comment Share on other sites More sharing options...
jeffman Posted April 20, 2011 Share Posted April 20, 2011 I wrote this in PHP. Hopefully that's on your server. The rules are like this.Every chapter needs to be named in this pattern: ab01.phpThe .php extension is required to execute the code.In the exact location where you want the links to appear in each document, add this line of code. It will be the same for every chapter: <?php include 'links.php'; ?> The code below must be in a file in the same directory as the chapters. I called it links.php for convenience. Nothing else goes in that file.links.php <?php $myname = basename($_SERVER['PHP_SELF']); $files = glob('ab*.php'); $last = count($files) - 1; for ($i = 0; $i <= $last;) { if ($myname == $files[$i]) { break; } $i++; } if ($i == 0) { $url = $files[$last]; } else { $url = $files[$i - 1]; } $str = "<a href='$url'>previous chapter</a> | "; if ($i == $last) { $url = $files[0]; } else { $url = $files[$i + 1]; } $str .= "<a href='$url'>next chapter</a>"; echo $str;?> This is really bare bones. It can be tweaked. For example, I have it looping, so that if you're in the first chapter, "previous" takes you to the last chapter, and vice versa. If need that changed, or something else, ask.Even if you don't know PHP, you should be able to edit the HTML parts to suit you. Link to comment Share on other sites More sharing options...
durian Posted April 20, 2011 Author Share Posted April 20, 2011 Many thanks, that PHP code is exactly the sort of thing I was looking for. :)Out of curiosity, what needs to be changed to prevent looping? Link to comment Share on other sites More sharing options...
jeffman Posted April 20, 2011 Share Posted April 20, 2011 All the if-else stuff. You might want to change that so that if the current file is the first one, say, think that normally says "previous" goes to the table of contents instead. Or maybe it just vanishes. Same idea for the last file. Link to comment Share on other sites More sharing options...
durian Posted May 3, 2011 Author Share Posted May 3, 2011 All the if-else stuff. You might want to change that so that if the current file is the first one, say, think that normally says "previous" goes to the table of contents instead. Or maybe it just vanishes. Same idea for the last file.Cool, thanks. Oh, and if I want to have a footer like:Previous Chapter | Homepage | Next Chapter... Can I just insert a plain HTML link (e.g. www.myhomepage.com) in the middle of that PHP code? Or do I have to insert the homepage link in the middle of two separate PHP scripts? Link to comment Share on other sites More sharing options...
jeffman Posted May 3, 2011 Share Posted May 3, 2011 You could easily hardcode the homepage link like this. (I'm going off the code I last showed you.) $str .= " | <a href='home.html'>Hompage</a> | ";$str .= "<a href='$url'>next chapter</a>"; Now it's all part of one big string that gets echoed just once. Link to comment Share on other sites More sharing options...
durian Posted May 7, 2011 Author Share Posted May 7, 2011 You could easily hardcode the homepage link like this. (I'm going off the code I last showed you.)$str .= " | <a href='home.html'>Hompage</a> | ";$str .= "<a href='$url'>next chapter</a>"; Now it's all part of one big string that gets echoed just once. Thanks again, but I'm afraid I must be doing something wrong. The footer stopped working properly after I tried to remove the looping 'if/else' bit and insert the Homepage link. :)So, if we could put everything together and have one PHP code without looping and including the "Previous Chapter | Homepage | Next Chapter" links what should it all look like? Link to comment Share on other sites More sharing options...
jeffman Posted May 7, 2011 Share Posted May 7, 2011 Better post your current code. Explain what you want and what's not happening. Even better if you can post a link to alive page, if you have one up yet.(I'm not sure why you would remove the loop or the if-else structure.) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.