• Aucun résultat trouvé

The Code

Dans le document PHP OBJECT-ORIENTED (Page 69-72)

Go ahead and download the code for your page navigator, and look it over.

Notice that there are considerably more data members than in other classes discussed so far. Names have been chosen in an attempt to make the purpose of the variable explicit, and related variables have been grouped. We’ll discuss these variables in the order in which they appear in the class file.

private $pagename;

The variable $pagename is the name of the page that will contain the page navigator control. It could be replaced by $_SERVER['PHP_SELF'], but by using a variable, you can accommodate situations where the Apache module mod_rewrite is being used to rewrite URLs. It’s designed to hold a string.

private $totalpages;

$totalpages is a convenient way to refer to the total number of pages required to display all the items in your list of images. It is calculated from the total number of items and the number of items shown per page. Its value will be an integer.

private $recordsperpage;

private $maxpagesshown;

$recordsperpage is the number of items shown on a page and $maxpagesshown is the maximum number of links to additional pages shown on any one page.

The former affects the length of the page, while the latter affects the width of the navigator. Again, these are intended to be integer variables.

private $currentstartpage;

private $currentendpage;

private $currentpage;

$currentstartpage, $currentendpage, and $currentpage are best understood using a visual example. Refer back to Figure 7-1; these would be 1, 7, and 4, respectively.

The next four variables are string variables that hold the HTML code necessary to display inactive links.

//next and previous inactive private $spannextinactive;

private $spanpreviousinactive;

//first and last inactive private $firstinactivespan;

private $lastinactivespan;

If you are currently on the first page, moving to a previous page or to the first page itself wouldn’t make sense. These variables will be used in place of active hyperlinks. Inactive links will be enclosed by span tags. Assigning a CSS class name to these spans allows their appearance to be manipulated by a style sheet.

$firstparamname and $params are data members that will form the query string in each hyperlink in your navigator.

//must match $_GET['offset'] in calling page private $firstparamname = "offset";

//use as "&name=value" pair for getting private $params;

$firstparamname is assigned a default value of “offset.” While the additional parameters contained in $params may or may not be added to the query string, the use of the “offset” parameter is not optional; this variable’s name must always be matched by a $_GET['offset'] in the page that contains your navi-gator. The $firstparamname will perform the same function as start in a Google query string—you will always need to know where the current page is relative to the start page. The variable $params will hold any other name/value pairs that may be needed as part of a query string. (You’ll learn more about this in Chapter 9.)

The next set of variables are string values that hold the CSS class names for the page navigator and its elements.

//css class names

private $divwrappername = "navigator";

private $pagedisplaydivname = "totalpagesdisplay";

private $inactivespanname = "inactive";

You’ve assigned default values to each of these variables, but they all have set and get methods so a client programmer can change them in order to match existing CSS classes if need be. $divwrappername is the name of the div tag that encloses the complete navigator. $pagedisplaydivname allows you to separately manipulate the display of the message relating the current page and total number of pages, such as page 4 of 12. You only need one class name for all of your inactive spans, because you want them all to have the same look.

B uil di ng t h e Pa g eNa vig a to r Cl ass 51 The remaining four variables are simply text strings that label the con-trols used in the navigator, and they can be changed as the user sees fit:

//text for navigation private $strfirst = "|<";

private $strnext = "Next";

private $strprevious = "Prev";

private $strlast = ">|";

//for error reporting private $errorstring;

The use of variables for the navigation text means that a client program-mer can configure these values—the look of the navigator is not fixed and can be adjusted to accommodate different visual layouts. The final data member is a string variable used for error reporting.

The Constructor

Now let’s see how the class is constructed. The constructor accepts six arguments, two of which have default values. Here is the constructor declaration:

public function __construct($pagename, $totalrecords, $recordsperpage,

$recordoffset, $maxpagesshown = 4, $params = "")

Four of the parameters to the constructor are simply copied into their class equivalents, and all have been discussed in the previous section on the data members.

$this->pagename = $pagename;

$this->recordsperpage = $recordsperpage;

$this->maxpagesshown = $maxpagesshown;

//already urlencoded $this->params = $params;

Note that $params (the variable that contains any additional parameters as a name/value pair) is not URL-encoded within the class. If it is used, it will need to be URL-encoded before it is sent.

The constructor finishes with calls to a number of private class methods:

//check recordoffset a multiple of recordsperpage

$this->checkRecordOffset($recordoffset, $recordsperpage) or die($this->errorstring);

$this->setTotalPages($totalrecords, $recordsperpage);

$this->calculateCurrentPage($recordoffset, $recordsperpage);

$this->createInactiveSpans();

$this->calculateCurrentStartPage();

$this->calculateCurrentEndPage();

Let’s look at each of these method calls in turn.

Dans le document PHP OBJECT-ORIENTED (Page 69-72)