Jump to content

Creating multiple internal links


Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

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.

Edited by Deirdre's Dad
Link to post
Share on other sites

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 post
Share on other sites
  • 2 weeks later...
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? Edited by durian
Link to post
Share on other sites

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 post
Share on other sites
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? Edited by durian
Link to post
Share on other sites

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.)

Edited by Deirdre's Dad
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...