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: , , ,

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: , ,