You Are Here Home > A PHP Competition 2

A PHP Competition 2

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 ;)

A PHP Competition 2
Filed under: Fun, PHP, Programming   Posted by: Codehead

Got a Question?

Get answers here.

9 Comments »

  1.  

    .9-.1*floor($count++/1000)

    Comment

  2. Codehead:
     

    I will approve the responses all at once 2 weeks from now…

    Comment

  3. David:
     

    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

  4. Codehead:
     

    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

  5. Turbo:
     

    $priority = sprintf(‘%01.1f’, ((5000 – $count++)/5000));

    Comment

  6. Turbo:
     

    err… $count++, that is

    Comment

  7. Vince:
     

    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

  8.  

    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

  9. Codehead:
     

    I didn’t approve the responses so they won’t effect anybody’s solution…

    Comment

RSS feed for comments on this post. TrackBack URL

Leave a comment