Building an online-publication application with PHP; producing a table of contents


People have always wanted to publish books and yet getting them published is not easy. One answer is to publish part of it online; these sample chapters would help sell the book to a publisher or perhaps could be used in a scheme to sell self-published copies to customers who happen along to your website. One way to present the samples elegantly is to present a table of contents that present each chapter title with the available titles being links to the sample chapters. This application permits you to persent different sample chapters at different times as the actual presentation of the chapter titles as links or not is done automatically. My own example can be found here.

My publishing application includes several components but for now I will discuss only the table of contents. It is programmed in PHP and in the object-oriented style. That is the method of programming that tries to envisage applications as objects made up of collections of objects working together, as physical applications do. The programmer sets up the blueprint for the object, called a "class" and describes the physical state of the object by setting variables and the things that the object does, the methods. Methods are the object-oriented counterpart to functions in procedural programming. This method of programming normally allows people to reuse code more easily.

The class will be contained in a file called publisher.php. The book itself will consist of the application, book.php, and a series of text files, one for each chapter. They will be named chapter1.txt, chapter2.txt”… The role of book.php will be to make it possible to call and display each text file in a window generated by that PHP file. The text files will be called by clicking on a URL that appends a query string to the name of the file; that query string sets a variable $action that determines which of the text files will be called. Thus, a query string that sets action=chapter1 will set the $action variable to chapter1 and thus call chapter1.txt into the window.

A query string is found on a URL following a question mark. Many URL’s are as follows; http://rat.com/book?action=mouse&word=shrew.

The query string is action=mouse&word=shrew. Its purpose is to set variables; the variable action is now equal to mouse. The variable word is now equal to shrew. These variables can be used by a script programmed in languages such as PHP, Perl or Python to help execute the web page. The web page can be programmed to display the page in different ways according to the values of these variables; thus, different web pages can be created from the same PHP or Perl file by manipulating variables on the query string. In this case, the manipulation of variables leads to different text being called into the text window created by our book.php file.

The window itself will be set up in any way you want; you can set up a table to include the window and the sidebar containing the chapter links. You can use Cascading Style Sheets to do the same thing, if you want. You can put your table of contents to the left or to the right of your text window, if you want.

In a file that I will name publisher, I will set up my blueprint that I will call publisher;. I do so as follows;

   class publisher {

Here I will write the program that creates the table of contents; a method of the publisher object called sidebar; it is so-named because the table of contents appears on either the left or right side of the webpage on every page, permitting easier navigation. It is a sidebar, thus.

   function sidebar($chapternumbers,$includename, $f)

{

$this->f = $f;

$this->includename = $includename;

include $includename;

?>

    <a href=" <?print $f;?>">Introduction

    <?

$this->chapternumbers = $chapternumbers;

$numbers = $chapternumbers+1;

for ($i=1; $i < $numbers; $i++)

{

$filename = "Chapter".$i.".txt";
$title = $filename;

$mooo = "title".$i;

$tittle = $$mooo;

$rat = substr($title,0, (strrpos($title, ".")));
$action = $rat;

if (file_exists($filename)){

print " <a href=\"".$f."?action=".$rat."\">".$tittle."\n";

}

else

print $tittle." \n"; }

}

The first thing to note here is that sidebar() will be called in the object-oriented way. The publication's PHP page will create a publisher object as so; first to include the publisher.php file, then to instantiate the publisher object and then to call the method of that object.

   include "publisher.php";

$p = new publisher;

After creating the object, its sidebar() method can be called so that the table of contents is displayed wherever desired; be it on the right or left of the text window. Thus, it must called inside two td tags in the case of a table structure or two div tags in the case of a Cascading Style Sheets structure.

$p->sidebar(20, "titles.php",$_SERVER['PHP_SELF']);

As you can see here as before, sidebar() takes three parameters. The first is the number of chapters that your book has. The second is the name of a file that contains the titles of your chapters. The third is the name of the file to which a query string will be appended creating the URL to the webpages containing each chapter. Normally, that file is the existing file as is in this case.

   function sidebar($chapternumbers, $includename, $f)

{

$this->f = $f;

$this->includename = $includename;

include $includename;

The publisher object's $f variable is set by grabbing the $f value from the list of parameters and assigning it to the object's $f through $this->f; $this signifying the object's $f. This is also true for $includename; with include $includename, the file of titles is now included in the function; its variables can now be used in executing this function.

  ?>

    <a href=" <?print $f;?>">Introduction

    <?

We close the PHP tag in order to place some HTML code; we reopen the PHP tag in order to enter the value of $f as the URL to the introduction; this is set as the file itself, thus making the Introduction the front page of our book.

The PHP tag is closed to add more HTML after which we reopen the PHP tag for the next bit of code.

   $this->chapternumbers = $chapternumbers;
$numbers = $chapternumbers+1;

The object's $chapternumbers variable is set using the same means used to set $f and $includename. In this case, $chapternumbers is set at twenty. We then add one to $chapternumbers, making it twenty one, as the loop will begin at 1 and loop until 21; thus, loop twenty times as intended. The loop begins at one because the chapters start at chapter one and not at chapter zero.

   for ($i=1; $i < $numbers; $i++)

{ $filename = "Chapter".$i.".txt";
$title = $filename;

$mooo = "title".$i;

$tittle = $$mooo;

$rat = substr($title,0, (strrpos($title, ".")));
$action = $rat;

if (file_exists($filename)){

print "   <a href=\"".$f."?action=".$rat."\">".$tittle."\n";

}

else

print $tittle."  \n"; } }

We loop from 1 to 21; as it passes through the loop, the variable $i starts at 1 and goes up to 20. Variable $filename is set at "Chapter1.txt", "Chapter2.txt" all the way to "Chapter20.txt".

As it loops through, $filename's value is assigned to $title. Next, the variable $mooo is assigned the value of "title" plus the value of $i; "title1", "title2" to "title20"; With

   $tittle = $$moo;

$tittle is assigned the value $title1 and through it, is assigned the value that $title1 has in the "titles.php" file! That was a file that was included. Here, the titles are set;

$title1 = "1. The Euro-Atlantic Empire";

$title2 = "2. The Empire kills Yugoslavia";
$title3 = "3. 'What we say goes!'";

Thus, on the first loop, $tittles is assigned the value "1. The Euro-Atlantic Empire".

The next two lines;

 $rat = substr($title,0, (strrpos($title, ".")));
$action = $rat;

is designed to chop of the ".txt" extension of the filename and then assign that value to $rat and then to $action.

The end part of the code takes the $filename and searches the directory for such a file. If it exists, it prints the chapter title $tittle as a link. If not, it is printed as simple text.

   if (file_exists($filename))

{

print "

    <a href=\"".$f."?action=".$rat."\">".$tittle." \n";

} else

print $tittle." \n"; }

}

The URLs that are produced feature a query string that assigns the value of the filename (minus the .txt extension) to the $action variable. The book's webpage features the code that will take that $action value and use it to call its file as an include on that PHP page. The publisher object's get_file() method deals with that. Call this method between the td tags or div tags that delineate the text window of your book application.

get_file() is very simple. This is it here.

function get_file($index)

{

$this->index = $index;

if ($_GET['action']) {

$pagename = $_GET['action'].".txt";

include "$pagename";

}

else {

include "$index";

}

}

$index is the name of the file that will be the front page/introduction file. The publisher object’s $index is set using $this->index = $index, with the latter $index taken from the parameter. Then, an if statement is presented; if the HTTP header includes a variable called “action” obtained using GET, the value of that variable is combined with “.txt” and is assigned to the variable $pagename. That is the name of the file to be included. A query string (the part of the URL following the filename plus a question mark) sets the $_GET[‘action’]; if in the query string, “action” is declared to be equal to something. An example would be as follows.

http://www.rat.ca/book.php?action=rat.

get_file() would take rat, append “.txt” to it, creating “rat.txt” to be included.

Finally, if there is no $_GET[‘action’] set, the filename assigned to the variable $index is included instead. Thus, in the event of the URL http://www.rat.ca/book.php , $_GET[‘action’] is not set in the query string and thus the filename $index is included.