
![]() |
gnarlyhat
I have a sitemap creator which spits out a static sitemap with all the pages. I would like to split them into ... say 50 urls per page. How do I go about doing this? Instead of just spitting out one file, I want it to split the sitemap into multiple pages with links to each one. I tried a loop and end up having multiple files of the same content
![]() ![]() Here's the source of the original file. <B><CENTER><font color="green" name="arial" size="4">Main Sitemap</font></CENTER></B>
<? $path = "."; $URL = "http://"; $URL .= $HTTP_SERVER_VARS["HTTP_HOST"]; $var = "<B><CENTER><font color="green" name="arial" size="4">Main Sitemap</font></CENTER></B>"; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if(is_dir($file) && $file != "." && $file != ".." ![]() { $checkpath = $path."/".$file."/include/conf. php";$checkpath1 = $path."/".$file."/sitemap. php";if (file_exists($checkpath1)) { $sitename = ucwords(str_replace("-", " ", $file)); $dir_handle2 = @opendir($file) or die("Unable to open $file"); //running the while loop while ($file2 = readdir($dir_handle2)) { if(!is_dir($file2) && strrchr($file2,'.') == ". php"![]() { $sitename = ucwords(str_replace("-", " ", $file2)); $sitename = str_replace(". php", "", $sitename);echo "<br><a href=$URL/$file/$file2>$sitename</a><br> "; $var .= "<br><a href=$URL/$file/$file2>$sitename</a><br> "; } } } if (file_exists($checkpath)) { $sitename = ucwords(str_replace("-", " ", $file)); $var .= "<br><a href=$URL/$file/>$sitename</a><br> "; $lines = file($file."/admin/keywords.txt"); foreach ($lines as $line_num => $line) { $line = trim($line); $link = trim(str_replace(" ", "-", $line)); $link = $link.".htm"; $var .= "<br><a href=$URL/$file/$link>$line</a><br> "; } } } } //closing the directory closedir($dir_handle); $fp = fopen("fullsitemap.htm", "w"); fwrite($fp, $var, strlen($var)); fclose($fp); ?> <B><CENTER><font color="green" name="arial" size="4">PROCESS COMPLETED...</font></CENTER></B> perkiset
Without looking at your code, I can offer that probably no one here's going to write that one for you... this is a logical exercise that you'll need to go through.
Some thoughts tho:
Some baby steps towards doing this would be to re-write this page so that it only shows 50 records, based on a $var that is the starting place - once you've got that working then you'd tackle the variable issue - perhaps you have a master /sitemap. phpthat points to /sitemapdetail.php?start=51 ... although this is not effective if you're doing this for spiders. If you are doing this for easy-easy scraping, then you'll want your URLs to look more like this: /sitemaps/51/index.html - in this case you'll need a mod_rewrite inApacheto make this work.Hope this pushes you off in the right direction, /p gnarlyhat
Thanks perk. I wasn't expecting anyone to write it for me but just to point me to the right direction. As this is producing a static html paged sitemap, I think I can only take in half of your advice. I wasn't thinking right when I tried to insert some kind of loop and counter where it's supposed to spit out the link. That got me nowhere and bad results. I've thought over it while sleeping and I think it's just best to play with the $var at the end of the code, when it's all filled up. Read write 50 links plus the link to next page and loop it till it's done. I will post the code when I'm done with it ... hopefully
![]() perkiset
Here's a couple little functions that I use to great affect. If you wrote a little reentrant directory walker, these two functions could be used to create a complete tree of your site very quickly.
<? phpfunction getDirs($inDir) { $out = array(); $h = opendir($inDir); while (! is_bool($file=readdir($h))) { if (! is_dir($inDir . '/' . $file)) { continue; } if (( $file=='.') || ($file=='..')) {continue;} $out[] = $file; } closedir($h); sort($out); return $out; } function getFiles($inDir) { $out = array(); if (!is_dir($inDir)) { die("ERR-01: Invalid Directory '$inDir'"); } $h = opendir($inDir); while (! is_bool($file=readdir($h))) { if (is_dir($inDir . '/' . $file)) {continue;} if ($file[0] == '.') { continue; } $out[] = $file; } closedir($h); sort($out); return $out; } ?> gnarlyhat
Thanks perk. I'm sure this will come in handy in time to come
![]() ![]() ![]() gnarlyhat
It took me a long time and I was even dreaming of this simple task at night but finally got it. Well ... at least part of it. Here's my problem now. After running it on my setup, the script found 246 links to be processed. Since I put 50 links per page, I should have 5 pages. The problem now is I only have 4 pages. Where did I go wrong? TIA
![]() <?
$path = "."; $URL = "http://"; $URL .= $HTTP_SERVER_VARS["HTTP_HOST"]; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if(is_dir($file) && $file != "." && $file != ".." ![]() { $checkpath1 = $path."/".$file."/sitemap. php";if (file_exists($checkpath1)) { $sitename = ucwords(str_replace("-", " ", $file)); $dir_handle2 = @opendir($file) or die("Unable to open $file"); //running the while loop while ($file2 = readdir($dir_handle2)) { if(!is_dir($file2) && strrchr($file2,'.') == ". php" && $file2 != ".php"![]() { $sitename = ucwords(str_replace("-", " ", $file2)); $sitename = str_replace(". php", "", $sitename);$var .= "<a href="$URL/$file/$file2">$sitename</a><br> "; } } } } } //closing the directory closedir($dir_handle); $links = explode(" ", $var); echo count($links)." links processed<br>"; echo "<a href="sitemap1.htm">Main Sitemap</a>"; $page = 1; $url = "<html><title>Sitemap ".$page."</title><body> <h1>Sitemap ".$page."</h1> "; foreach ($links as $link) { $count++; $url .= $count; $url .= $link." "; if ($count==50) { $url .= "<br><a href="sitemap".($page+1).".htm>Next Page</a><br> </body></html>"; $fp = fopen("sitemap".$page.".htm", "w"); fwrite($fp, $url, strlen($url)); fclose($fp); $count = 0; $page++; $url = "<html><title>Sitemap ".$page."</title><body> <h1>Sitemap ".$page."</h1> "; } } ?> perkiset
quote author=gnarlyhat link=topic=621.msg4243#msg4243 date=1195203997 ...the script found 246 links to be processed. Since I put 50 links per page, I should have 5 pages. The problem now is I only have 4 pages. Where did I go wrong? TIA ![]() Well somewhere in there, your math is wrong. ![]() Here endeth the lesson ![]() gnarlyhat
You're wrong Perk ....
My maths is correct. My problem solving/logic is wrong. This endeth the lesson ![]() <? $path = "."; $URL = "http://"; $URL .= $HTTP_SERVER_VARS["HTTP_HOST"]; //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //running the while loop while ($file = readdir($dir_handle)) { if(is_dir($file) && $file != "." && $file != ".." ![]() { $checkpath1 = $path."/".$file."/sitemap. php";if (file_exists($checkpath1)) { $sitename = ucwords(str_replace("-", " ", $file)); $dir_handle2 = @opendir($file) or die("Unable to open $file"); //running the while loop while ($file2 = readdir($dir_handle2)) { if(!is_dir($file2) && strrchr($file2,'.') == ". php" && $file2 != ".php"![]() { $sitename = ucwords(str_replace("-", " ", $file2)); $sitename = str_replace(". php", "", $sitename);$var .= "<a href="$URL/$file/$file2">$sitename</a><br> "; } } } } } //closing the directory closedir($dir_handle); $links = explode(" ", $var); echo count($links)." links processed<br>"; echo "<a href="sitemap1.htm">Main Sitemap</a>"; $page = 1; $url = "<html><title>Sitemap ".$page."</title><body> <h1>Sitemap ".$page."</h1> "; foreach ($links as $link) { $count++; $url .= $link." "; if ($count==50) { $url .= "<br><a href="sitemap".($page+1).".htm>Next Page</a><br> </body></html>"; $fp = fopen("sitemap".$page.".htm", "w"); fwrite($fp, $url, strlen($url)); fclose($fp); $count = 0; $page++; $url = "<html><title>Sitemap ".$page."</title><body> <h1>Sitemap ".$page."</h1> "; } } $url .= " <br><a href="sitemap1.htm>First Page</a><br> </body></html>"; $fp = fopen("sitemap".$page.".htm", "w"); fwrite($fp, $url, strlen($url)); fclose($fp); ?> perkiset
![]() Good for you lad. Someday when you are a father you may realize the benefit of a throwing of the guantlet and dismissal as the most powerful and empowering lesson of all ![]() gnarlyhat
I am a father
![]() perkiset
CONGRATS. It's a great journey, you're gonna love it.
|

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads