<?php
// input is $name - the person to highlight in the hierarchy
// connect to the database
   
if(!($dblink=mysql_connect("shares.cems.uwe.ac.uk","cwstudent","cwstudent"))) {
     print(
"mysql_connect failed<br>\n");
     exit;
   }

   if(!(
mysql_select_db("Emp",$dblink))) {
     print(
"cannot connect to database $db");
     exit;
   }
   
$f .= "digraph hier {\n";

// get the hierarchy data
   
$query "select e.ename as ename, m.ename as mname from emp e , emp m where e.mgr = m.empno;";
   if(!(
$dbresult mysql_query($query,$dblink))) {
        print(
"<b>query failed</h4><br>\n");
        print(
mysql_error() . "<br>\n");
   }

   while(
$rep=mysql_fetch_object($dbresult)) {
       
$f .= "$rep->mname  -> $rep->ename; \n"
   }

// get the emps 
   
$query "select ename from emp";
   if(!(
$dbresult mysql_query($query,$dblink))) {
        print(
"<b>query failed</h4><br>\n");
        print(
mysql_error() . "<br>\n");
   }

   while(
$rep=mysql_fetch_object($dbresult)) {
       
$f .= "$rep->ename [URL=\"showemp.php?name=$rep->ename\"] \n"
   }

   
$f .= "}";
// print $f;
   
mysql_close($dblink);
// generate the map 
   
exec("echo '$f' | /usr/local/graphviz/bin/dot -Tcmap "$map);
   
$maphtmlimplode("\n",$map);
// add the target - cant do this with DOT
   
$maphtml=str_replace("<area ""<area target=\"emp\"",$maphtml);
// generate the image
   
$tfn='TEMP/'.uniqid("").'.gif';
   
exec("echo '$f' | /usr/local/graphviz/bin/dot -Tgif > $tfn");

?>

<html>
<head>
<title>Emp Hierarchy - interactive using client-side image map</title>
</head>
<body>
<h1>Emp Hierarchy - interactive using client-side image map</h1>
<p>Click on an employee name to show the employee details</p>
<map id="tags" name="tags">

<?php print $maphtml?>

</map>
<img src="<?php print $tfn?>" border="0" usemap="#tags" Ismap>
</body>
</html>