开发者

Mathematical equation to link pages

It's kind mathematics query..I want a script in php that will do following

Let's say I've 100 pages page1.php, page2.php, page3.php, page4.php....goes on page100.php

I want to create a php algorithm to link each page with one another. Each page will have 10 page 开发者_开发百科links to other pages but I want a mathematical equation that will link each page.

page1 will have 10 links to other pages, page2 will have 10 links to other pages, this will go on until each page is equally linked in total 100 pages with one another.


One simple way would be to link to the previous- and next-five pages, wrapping around from 100:

  • 1 links to 96, 97, 98, 99, 100, 2, 3, 4, 5, 6
  • 2 links to 97, 98, 99, 100, 1, 3, 4, 5, 6, 7
  • ...
  • 100 links to 95, 96, 97, 98, 99, 1, 2, 3, 4, 5

Every page gets linked to exactly 10 times (from the exact pages it links to itself)


Of course, instead of doing something strange like this, perhaps you should consider simply improving your UI? For instance, having a dropdown, or even better a search-box, for users to find what they are looking for?


why do you have so many php pages. why dont you utilize php's $_GET method.

instead of: page1.php, page2.php, page3.php, page4.php....goes on page100.php

your users would goto: content.php?page=1, content.php?page=2,content.php?page=3...

and in the content.php page you can have an array of pages:

$pages = array (1,2,3,4,...)

and then:

if(in_array($_GET['page'], $pages){
    //do stuff for whatever page is set
}

and then on the bottom of each page you can output all the pages you have:

shuffle($pages); //put pages in a random order
$count = 0;
foreach($pages as $page){
    echo "<a href='content.php?page=$page'>Page $page</a>";
    if($count < 10){
        $count++;
    }
    else break;
}


Similar to manitor's answer, but only showing 10 pages in the neighborhood of the current page.

define("TOTAL_PAGES", 100);
define("FOOTER_LINK_COUNT", 10);
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Range check
if ( $page < 1 || $page > TOTAL_PAGES ) {
  $page = 1;
}

// You can display a header here if you'd like

// Include actual content page
include("page{$page}.php");

// Generate the footer    
echo "<ul>\n";
for (
  // Maths! Start at the current page, minus half of the total number of links to display
  // but don't go below 0 (negative pages) and don't go over the total number of pages
  $i = min(
    max(0, $page - ceil(FOOTER_LINK_COUNT / 2)),
    TOTAL_PAGES - FOOTER_LINK_COUNT
  ), 
  $c = 1; 
  $c <= FOOTER_LINK_COUNT;
  ++$c ) {
  printf("\t<li><a href='?page=%1\$d'>%1\$d</a></li>\n", $i + $c);
}

echo "</ul>";

So for page 1, you see 1,2,3,4,5,6,7,8,9,10

For page 10: 6,7,8,9,10,11,12,13,14,15

For page 95: 91,92,93,94,95,96,97,98,99,100

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜