Create Google Sitemap With PHP And Mysql

The other day we created a xml sitemap using information retrieved from a database with php, we thought we’d show you the code on how to do it:

  1. <?php
  2. $conn = mysql_connect("dblocation", "username", "password") or die('Error connecting to mysql');
  3. mysql_select_db(dbname);
  4. echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  5. echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
  6. $sql = "select * from tablename GROUP BY fieldname";
  7. $rs = mysql_query($sql, $conn);
  8. while($row=mysql_fetch_array($rs))  {
  9.  echo "<url>\n";
  10.  echo "<loc>http://www.your-url.com/".$row['abbr'].".html</loc>\n";
  11.  echo "<lastmod>2008-09-29</lastmod>\n";
  12.  echo "<changefreq>monthly</changefreq>\n";
  13.  echo "
  14. <priority>0.5</priority>\n";
  15.  echo "</url>\n";
  16. }
  17. echo "</urlset>" ;
  18. mysql_close($conn);
  19. ?&gt;

Save as sitemap.php and submit to search engines. Also in your robots.txt add: sitemap: http://www.your-url.com/sitemap.php

This code will return this xml source output for EACH record in your database:

<url>
<loc>http://www.your-url.com/page-path.html</loc>
<lastmod>2008-09-29</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>

Posted under Web Design

This post was written by Coyol on September 30, 2008

Tags: , , ,

301 permanent redirect in php

When we migrated from a basic php driven website to this new blog style website for Coyol.net we needed to make sure we kept rankings for our pages. We did this by creating a permanent redirect for each page. The original website was really only 8 pages so it really wasn’t difficult to do at all.

Why is this important? Because by doing this we still get the traffic from our original pages AND the Search Engine spiders understand that those are old pages and they will filter them from the Search Results eventually. You’ll basically transfer your rankings from old page -> new page.


Here is the simple PHP redirect code that we used… Remove all content from original page/old page and replace it with this:

  1. <?
  2. header( "HTTP/1.1 301 Moved Permanently" ); //the original page was http://www.coyol.net/seo.php
  3. header( "Location: http://www.coyol.net/search-engine-optimization-seo" );
  4. exit();
  5. ?>

Watch how it works: http://www.coyol.net/seo.php

Posted under SEO/SEM, Web Design

This post was written by Coyol on September 12, 2008

Tags: , , ,

Coyol.net is a DoFollow Blog

With the help of a Wordpress script, Coyol.net is a DoFollow Blog. So now you can benefit from interacting on our Blog.

The settings are:

  • Non-registered users must have two comments before the nofollow is removed
  • Registered users will immediately have nofollow removed
  • We still grant the right to remove comments if we see them as spam

Posted under Coyol News, SEO/SEM

This post was written by Coyol on September 12, 2008

Tags: , ,

Displaying query results in columns

Although it easy fairly easy to pull results from your database and display them, often times you might want to display them in a crisp table format. The below script will help you display query results evenly through columns.

  1. <?php
  2. include 'config.php';
  3. include 'opendb.php';
  4. $table = 'your table here';
  5. $query  = "SELECT name FROM $table WHERE variable='$var' GROUP BY name"; //you can get rid of variable if not needed
  6. $result = mysql_query($query);
  7.  
  8. echo "&lt;table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\"&gt;\n";
  9. $count = 0;
  10. $columns = 3; //column number declaration
  11. if(mysql_num_rows($result)) {
  12. while($myrow = mysql_fetch_array($result)) {
  13. // display list if there are records to display
  14. if($count == 0) echo "&lt;tr&gt;\n";
  15. printf("&lt;td&gt;&lt;a href=\"%s%s.html\"&gt;%s %s&lt;/a&gt;&lt;/td&gt;\n", $php_SELF, $myrow["name"], $myrow["name"], $myrow[""]);
  16. if($count == $columns - 1) echo "&lt;/tr&gt;\n";//here without the!
  17. $count = ++$count % $columns;
  18. }
  19. if($count!= 0) echo "&lt;/tr&gt;\n";
  20. echo "&lt;/table&gt;\n";
  21. } else {
  22. // no records to display
  23. echo "Sorry, no records were found!";
  24. }
  25. include 'closedb.php';
  26. ?>

Posted under Web Design

This post was written by Coyol on September 12, 2008

Tags: , ,

Cool little tag cloud script

Yesterday day we built a pretty cool tag cloud script in php/mysql that gets the referring search keyword and inputs it into a database… and from there it can be pulled for the tag cloud information. We thought this would be useful information for everyone so we’ll share how we did it.

1. Create your database… we created fields: number, term, cityurl, countyurl, abbr IN table tag_cloud

2. In the head of your php page place the following:

**note: this is based on a state, county, city setup. Your code will most likely vary.

  1. <?php
  2.  
  3. $abbr = $_GET['state'];
  4. $countyurl = $_GET['countyurl'];
  5. $cityurl = $_GET['cityurl'];
  6.  
  7. $parse = parse_url($_SERVER['HTTP_REFERER']);
  8. $se = $parse["host"];
  9. $raw_var = explode("&amp;", $parse["query"] );
  10. foreach ($raw_var as $one_var) {
  11. $raw = explode("=", $one_var);
  12. $var[$raw[0]] = urldecode ($raw[1]);
  13. }
  14. $se = explode (".", $se);
  15. switch ($se[1]) {
  16. case 'yahoo':
  17. $keywords = $var['p'];
  18. break;
  19. case 'aol':
  20. $keywords = $var['query'];
  21. break;
  22. default:
  23. $keywords = $var['q'];
  24. }
  25. unset($parse, $se, $raw_var, $one_var, $var);
  26.  
  27. include 'config.php'; //you need to connect to the db
  28. include 'opendb.php';
  29.  
  30. if($keywords != NULL) { //this says if there is a keyword enter it to the database
  31. mysql_query ("INSERT INTO tag_cloud (term, countyurl, cityurl, abbr) VALUES ('$keywords', '$countyurl', '$cityurl', '$abbr')");
  32. }
  33.  
  34. include 'closedb.php';
  35. ?>


Now where ever on your page that you want to display the tag cloud enter the following code:

  1. <?php
  2. include 'config.php';
  3. include 'opendb.php'; // connect to database
  4.  
  5. $query = "SELECT term AS tag, COUNT(term) AS quantity
  6. FROM tag_cloud
  7. WHERE abbr='$abbr' AND cityurl='$cityurl'
  8. GROUP BY term
  9. ORDER BY term ASC"; //this pulls the tag cloud information from the DB
  10.  
  11. $result = mysql_query($query);
  12.  
  13. // here we loop through the results and put them into a simple array:
  14. // etc. so we can use all the nifty array functions
  15. // to calculate the font-size of each tag
  16. while ($row = mysql_fetch_array($result)) {
  17. $tags[$row['tag']] = $row['quantity'];
  18. }
  19.  
  20. // change these font sizes if you will
  21. $max_size = 200; // max font size in %
  22. $min_size = 100; // min font size in %
  23.  
  24. // get the largest and smallest array values
  25. $max_qty = max(array_values($tags));
  26. $min_qty = min(array_values($tags));
  27.  
  28. // find the range of values
  29. $spread = $max_qty - $min_qty;
  30. if (0 == $spread) { // we don't want to divide by zero
  31. $spread = 1;
  32. }
  33.  
  34. // determine the font-size increment
  35. // this is the increase per tag quantity (times used)
  36. $step = ($max_size - $min_size)/($spread);
  37.  
  38. // loop through our tag array
  39. foreach ($tags as $key =&gt; $value) {
  40.  
  41. // calculate CSS font-size
  42. // find the $value in excess of $min_qty
  43. // multiply by the font-size increment ($size)
  44. // and add the $min_size set above
  45. $size = $min_size + (($value - $min_qty) * $step);
  46. // uncomment if you want sizes in whole %:
  47. // $size = ceil($size);
  48.  
  49. // you'll need to put the link destination in place of the #
  50. // (assuming your tag links to some sort of details page)
  51. echo '&lt;a href="/city/' .$abbr. '-' .$countyurl. '-' .$cityurl. '.html" style="font-size: '.$size.'%"';
  52. // perhaps adjust this title attribute for the things that are tagged
  53. echo ' title="'.$value.' things tagged with '.$key.'"';
  54. echo '&gt;'.$key.'&lt;/a&gt; ';
  55. // notice the space at the end of the link
  56. }
  57.  
  58. include 'closedb.php';
  59.  
  60. ?>

You can view this working here: Grand Forks Real Estate

We’d like to thank Shawn at digitalpoint forums & Prism-Perfect for snippets of code.

Posted under Web Design

This post was written by Coyol on September 11, 2008

Tags: , ,

Coyol.net Re-Design

After debating for quite awhile on whether to re-design Coyol.net we decided to actually do it. We figure if we’re building websites and implementing search marketing strategies under Web 2.0 technologies for our clients it only makes sense that we use these strategies on our own website.

We still have all of the old information that was on our old website, we’ve just changed the look and feel… as well as added this blog. Let us know how you feel about the change in our website AND make sure to bookmark us because we’ll be coming out with new information daily.

Posted under Coyol News

This post was written by Coyol on September 11, 2008

Tags: ,