Added to Tutorials on 3/7/09.

How to count all the pages in your CakePHP web site using a sitemap.

treasure map
Website pages everywhere—but how to count the total up?

Arrrr, I've got to count the booty!

After you start putting some serious work into a web site, you can't help but check the Google search results to see how many times your pages are showing up. It's a kind of treausre—if you will. Just in case you forgot, you can use the "site:" prefix in order to find all the pages of your site that Google has indexed. There's more of these useful operators like "link:" and "define:" if you hunt around for help.

The problem is that the Google "site" command only shows you how many pages are indexed, not how many pages are actually in your site. In the old days, when all pages were HTML, all you had to do was count the amount of files in your file directory. Now we have content management systems. By dynamic design, the site usually only has a hand full of templates serving up rows and rows of table data.

With my new site here at Central 183 Web Design and Development, I'm determined to add new information daily in a bid to create a truly useful site, not just another collection of pages called "about us", "contact", "portfolio" and "estimate"

I hunted around this morning and found a fantastic tutorial on CakePHP sitemaps written by Cristian Deluxe over at the CakePHP site. I was so used to the neat Google Sitemap extension while using Wordpress sites, that I was wishful for something similar for my Cake site. A lightbulb went off. Now I had a way to add a useful sitemap and count all the pages up.

As far as the sitemap tutorial over and the Bakery, you just need to go over there and follow it. I had it up and running in an hour or two. It's pretty straight forward. Once your done, take a look at your /views/sitemaps/index.ctp. You'll see a loop that runs for the dynamic pages. All you have to do is set up a '$count' variable that increases with each loop. Once you have that number, you can play around with dates until your hearts content. I have a mantra that I want to write 365 pages in 365 days for this site, so I played with the date of the site launch and then compared that against the current pages. The code I used is below. You can look at it as a guide.

<?php
$this->pageTitle = 'Sitemap';
?>
<h1>
	Sitemap
</h1>

<table cellpadding="0" cellspacing="0" id='sitemap'>
<?php
$count=0;
if( isset($dynamics) && !empty($dynamics) ):
foreach ($dynamics as $dynamic): ?>
<tr>
	<th>
		<?php echo $html->link(
			$dynamic['options']['controllertitle'],
			array(
				'controller' => $dynamic['options']['url']['controller'], 
				'action' => $dynamic['options']['url']['index']
				)); ?>
			</th>
		</tr>
		<?php foreach ($dynamic['data'] as $section):?>
			<tr>
				<td>
					<?php echo $html->link(
						$section[$dynamic['model']][$dynamic['options']['fields']['title']],
						array(
							'controller' => $dynamic['options']['url']['controller'], 
							'action' => $dynamic['options']['url']['action'], 
							$section[$dynamic['model']][$dynamic['options']['fields']['id']]
							) ); ?>
						</td>
					</tr>

					<?php $count++; endforeach;?>
					<tr>
						<td class="clear">&nbsp;</td>
					</tr>
					<?php
				endforeach;
				endif;

				if(isset($statics) && !empty($statics) ):?>
				<tr>
					<td class="title">
						Misc
					</td>
				</tr>
				<?php foreach ($statics as $static): ?>
					<tr>
						<td>
							<?php echo $html->link(
								$static['title'],
									$static['url']); ?>
								</td>
							</tr>
						<?php endforeach;?>
						<tr>
							<td class="clear">&nbsp;</td>
						</tr>
					<?php endif; ?>   
				</table>
					<?php

				$startDate = date(mktime(0, 0, 0, 6, 29, 2009));
				$curDate = date(mktime());
				$difDate = date('z',$curDate) - date('z',$startDate);
				$difYear = date('Y',$curDate) - date('Y',$startDate);
				$difDate = $difDate + ($difYear * 365);

				if ($count > $difDate)
					$progress = "<span style='color:green'>" . ($count - $difDate) . " page(s) ahead</span>.";
				else
					$progress = "<span style='color:red'>" . ($difDate - $count) . " page(s) behind</span>.";

				echo "<p>Just in case you're interested, there are now <b>" . $count . " </b>pages on this site. My goal is to <b>create 365 pages in 365 days</b>. The site launched on <b>June 29, 2009</b>. Today is <b>" . date('F d, Y',$curDate) ."</b>. That means that <b>" . $difDate . " </b>days have past. My progress shows that I am <b>" .  $progress . "</b>" ?>

The final sitemap page for this site is found here.

blog comments powered by Disqus

No soup for you!

No Soup For You!You are still using Internet Explorer 6, so you can't have soup—or look at this site. I would really prefer you download Firefox or perhaps Google Chrome. If you're on a Mac, chances are you have Safari already, and that's good too!

If you're on Windows, and you must have a version of Internet Explorer, please go get the latest version of Internet Explorer.

If you want to know why you have to go without lunch today, please visit this link to learn more about Internet Explorer 6 and why web developers are determined to eradicate it.