Paul Nicholls Stuff

7Jan/100

Exporting Contacts from the Durham Uni Database

Quick bit of example php code to pull people's details out of the Durham University user database (not LDAP, this is a separate database with slightly different details) - this database is just sitting on the mysql server without a password!

The applications of this database are endless, in this simple example the script grabs everyone from one college and turns it into a handy CSV file you could import into your address book, etc...

Quick example code to dump mildert student data into a csv
  1. function fixcase($string){
  2.         $words = explode(" ", $string);
  3.  
  4.         for($i=0; $i<count($words); $i++) {
  5.             $s = strtolower($words[$i]);
  6.             $s = substr_replace($s, strtoupper(substr($s, 0, 1)), 0, 1);
  7.             $result .= "$s ";
  8.         }
  9.  
  10.         $string = trim($result);
  11.         return $string;
  12.  }
  13.  
  14.     mysql_connect("mysql.dur.ac.uk", "nobody", "") or die(mysql_error());
  15.  
  16.     $sql = "SELECT * FROM UserDetails WHERE (college = 'Van Mildert College' OR department = 'Van Mildert College')";
  17.  
  18.     $res = mysql_db_query("Pdcl0www_userdata", $sql) or die(mysql_error());
  19.  
  20.     echo "First Name, Surname, Display Name, Email, Department, Company, Website, Year";
  21.  
  22.     while($row = mysql_fetch_array($res)){
  23.      $name = array();
  24.      $name = split(" ", str_replace(",", " ",$row['firstnames']));
  25.     echo fixcase($name[0]).','.fixcase($row['surname']).','.fixcase($name[0]).' '.fixcase($row['surname']).','.$row['email'].','.$row['department'].','.$row['college'].',http://www.dur.ac.uk/vm.jcr/index.php?option=com_content&task=view&id=48&Itemid=55&person='.$row['username'].','.$row['studyyear']."";
  26.     }
  27.  
  28.   echo $res;
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.