FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


HomeWatched TopicsRegisterSearchDirectory
FAQMemberlistUsergroupsLog inStoresItemsBank
Google

Reply to topic Page 1 of 1
Using Multiple Pages for Navigation
Message  

Reply with quote
Post Using Multiple Pages for Navigation 
Recap
Let's start by taking a look at notepad's final script:

<?php
// if a page isn't defined, we're on page one
if($page <= 0)
{
    $page = 1;
}

// create an array of data
$myArray = file("data.txt");

// reverse the order of the data
$myArray = array_reverse($myArray);

// how many lines of data to display
$display = 5;

// where to start depending on what page we're viewing
$start = ($page * $display) - $display;

// the actual news we're going to print
$news = array_slice($myArray, $start, $display);

// printing the data
foreach($news as $key=>$value)
{
    print("line $key: $value<br>\n");
}
?>  

Source: http://codewalkers.com/tutorials/20/2.html

View user's profile Send private message

Reply with quote
Post  
Setting up variables
Now let's jump right into navigation. The variable $start is a place marker in the array. Since we know PHP arrays start with "0," and we really don't want to call the first item "0," we have to get a number your users might feel more comfortable with. We can't assign the variable $start, since it's used further in the script, so since we really start counting at "1," let's just add 1 to the start point:

<?php
$realstart = ($start + 1);
?>  


We'll use $realstart later, but first, we need to set some other variables. We know each page is going to display $display items, so we also know the last item on each page is going to be $start + $display. Remember, $realstart is just the number we are showing our users, not its actual value in the array, therefore we use $start instead of $realstart.

<?php
//set last item number on this page
$finish = ($start + $display);
?>  


Now we find out how many items exist in the file. In order to do this, we use the count() function. It's one of the easiest PHP functions and also one of the must useful.

<?php
//find out how many items are in the array
$keys = count($data);
?>  


Now that we know how many items are in the array ($keys), we can find out how many pages there should be. We can determine this by dividing the number of items by the number per page. Since there are $display items on each page, we can write this:

<?php
//determine number of pages
$newspages = ($keys / $display);
?>  


Now for some error prevention. If there are, say 28 items, page 3 will show 21-30. In order to fix this, we'll simply check the $finish variable. Since we know how many items are in the array, if $finish is greater than the count of the array, well reset $finish.

<?php
//display max number correctly.  
if ($finish > $keys) {
    $finish = $keys; }
?>  

Source: http://codewalkers.com/tutorials/20/3.html

View user's profile Send private message

Reply with quote
Post  
Displaying links
Now it gets a little more complicated. We don't want a "previous" link if we're on page 1. We also know anything less than page 1 has been given a value of 1 by notepad's script. If we are on page 2 or higher, we want a "previous" link. So let's take a look at the script and then break it down:

<?php
if ($page > 1) {
?>
    <a href="<?=$PHP_SELF;?>?page=<?=($page - 1);?>">previous</a> ||
<?PHP }
?>  


What are we looking at? First we're saying to only run our script if the page is above 1, since we don't want a previous link on page 1. If the page is greater than 1, though, we're going to make a link called "previous" than point to this page, or $PHP_SELF, where the page is equal to the $page variable minus one. If $page was set to 2 and the file was, say, news.php, the output would be <a href="news.php?page=1">previous</a>. I like to add a little << before the link to make it graphically pleasing.

Now we do the same for the "next" link. Instead of subtracting 1, we're going to add 1. But when should we run it? Since we've set the number of pages total to $newspages, we should run it only when $page is less than $newspages.

<?php
if ($page < $newspages) {
?>
    <a href="<?=$PHP_SELF;?>?page=<?=($page + 1 );?>">next</a> >>  
<?php }
?>  


Notice I added a >> as mentioned above. Between these links, typically, I like to see what items I'm viewing, so we need another line:

<?php
echo "displaying items " . $realstart . " through " . $finish;
?>  


We've already defined what $realstart is. One page 1, it's going to be 1 and on page 2, (if your $display is set to 5) it will be 6. Sometimes, you might want to add the following line for good measure:

<?php
echo "<br />" . $keys . " items total";
?>  


$keys, of course, is the total number of items in the news file.

Source: http://codewalkers.com/tutorials/20/4.html

View user's profile Send private message

Reply with quote
Post  
Final Script
Now we should have a fully functional navigation system. You might want to add some extra characters to enhance the appearace - I'll put a few in the final script. Note that everything in this tutorial is in between the stars and the rest is notepad's original script. If you've followed along, below is the complete script. I hope you've found it useful. Hopefully, the next tutorial will cover how to modify the news system to make it a more secure, dynamic, truly useful system.

<?php
// if a page isn't defined, we're on page one
if($page <= 0)
{
    $page = 1;
}

// create an array of data
$myArray = file("data.txt");

// reverse the order of the data
$myArray = array_reverse($myArray);

// how many lines of data to display
$display = 5;

// where to start depending on what page we're viewing
$start = ($page * $display) - $display;

/*********************************************************/
    
$realstart = ($start + 1);
$finish = ($start + $display);
$keys = count($data);
$newspages = ($keys / 10);
if ($finish > $keys) {
    $finish = $keys; }

    if ($page > 1) {
?>
    << <a href="<?=$PHP_SELF;?>?page=<?=($page - 1);?>">previous</a> ||
<?php }
    echo "displaying items " . $realstart . " through " . $finish;
    if ($page < $newspages) {
?>
    || <a href="<?=$PHP_SELF;?>?page=<?=($page + 1 );?>">next</a> >>
<?php }
    echo "<br />" . $keys . " items total";
    
/*********************************************************/

// the actual news we're going to print
$news = array_slice($myArray, $start, $display);

// printing the data
foreach($news as $key=>$value)
{
    print("line $key: $value<br>\n");
}
?>  

Source: http://codewalkers.com/tutorials/20/5.html

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
  



Google

FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


Web Design by PlatinumShore.com & Web Hosting by TradeWebHosting.com