Say that you are generating an XML sitemap for your site and there are 5,000 URLs.
Here is the code:
<?php $buffer = '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> '; $results = fetch_5000_urls_order_by_importance(); $count = 0; while ($row = $results->fetch_next_url()) { $priority = ##########################################; $url = $row['url']; $date = date('Y-m-d', $row['date']); $buffer .= " <url> <loc>$url</loc> <lastmod>$date</lastmod> <changefreq>weekly</changefreq> <priority>$priority</priority> </url> "; } $buffer .= ' </urlset> '; echo $buffer; ?>
What you want to do is to give each URL a priority; URLs between 0 to 1000 will have a priority of 0.9
1000 to 2000 => 0.8
2000 to 3000 => 0.7
3000 to 5000 => 0.6
4000 to 5000 => 0.5
Replace ########################################## with the code that calculates the priorities, and remember not to use ; in it
Hamid Alipour is a partner in Codehead, LLP with his wife, Tess. Hamid speaks 12 markup and programming languages [Yes, 12: PHP, CSS, Ajax, JavaScript, HTML/XHTML, Java, Python, C/C++, ASP, Visual Basic, Scheme and Action Script]; has a penchant for solving the unsolvable; an affinity for clean, hand-written code and is a Zend Certified 
.9-.1*floor($count++/1000)
Comment — March 30, 2009 @ 4:54 pm
I will approve the responses all at once 2 weeks from now…
Comment — March 30, 2009 @ 5:17 pm
Your post is very ill-worded. If all URLs were given a priority of 0.9, then we’ve technically fulfilled your requirements. However, I think I get what you’re trying to say…
I get that the ############# has to be a mathematical one-liner (presumably not a custom function call)… My question is: Is the priority of the URLs is based solely on the order in which they are encountered within the loop?
For more clarity, is this what you mean?:
URLs 1-1000: Priority between 0.9 and 1
URLs 1001-2000: Priority between 0.8 and 0.89
URLs 2001-3000: Priority between 0.7 and 0.79
URLs 3001-4000: Priority between 0.6 and 0.69
URLs 4001-5000: Priority between 0.5 and 0.59
David
Comment — April 8, 2009 @ 4:27 pm
David, I honestly think that the post explains it clearly but yes, what you are asking is right and yes in order they are encountered in the loop.
Again yes, it’s must be a mathematical operation, maybe function calls, you can nest as many function calls as you want but not ;
Thanks!
Comment — April 8, 2009 @ 5:07 pm
$priority = sprintf(‘%01.1f’, ((5000 – $count++)/5000));
Comment — April 15, 2009 @ 6:31 pm
err… $count++, that is
Comment — April 15, 2009 @ 6:31 pm
1 – ( .1 * intval( ( $count++ + 1000 ) / 1000 ) )
or if you wanted people to be a bit more confused change the increment around.
1 – ( .1 * intval( ( ++$count + 999 ) / 1000 ) )
Comment — April 29, 2009 @ 9:48 am
Not much of a competition considering how easy it was. Must be why there were no responses.
Well, since your specification ranges overlapped I’ll just go by groups of 1000 starting with 0.
… drum roll …
abs((floor($codeheads++/1000)/10)-0.9)
If you want code that does not generate a NOTICE error:
abs((floor((isset($codeheads)?$codeheads++:$codeheads=1)/1000)/10)-0.9);
Comment — June 10, 2009 @ 1:24 am
I didn’t approve the responses so they won’t effect anybody’s solution…
Comment — July 31, 2009 @ 4:06 pm