Location: PHPKode > scripts > Adjacency Model > example.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<title>Adjacency List Demo</title>
	
	<link rel="stylesheet" href="/css/styles.css" type="text/css" />
	
	<link rel="stylesheet" type="text/css" href="/css/timepicker_plug/style.css">
	<link rel="stylesheet" type="text/css" href="/css/smothness/jquery_ui_datepicker.css">
	
	
	<script src="/js/jquery-1.4.2.min.js" type="text/javascript"></script>
	<script src="/js/jquery-ui-1.8.custom.min.js" type="text/javascript"></script> 
	<script src="/js/i18n/ui.datepicker-de.js" type="text/javascript"></script>
	<script src="/js/timepicker_plug/jquery.timepicker.js" type="text/javascript"></script>
	
	<script type="text/javascript">
	/* <![CDATA[ */
		$(function() {
				  $('#pickerfield').datetime({
									userLang	: 'en',
									americanMode: true,
								});
				  $('#pickerfield2').datetime({
									userLang	: 'fr',
									americanMode: false,
								});								
			});
	/* ]]> */
	</script>


	
	
</head>
<body>

<div id="container">
	<div id="header">
    	<h1><a href="/">OPCMF</a></h1>
        <h2>The Open Portal Content Module Framework</h2>
    </div>
    
    <div id="body">
		<div id="content">
		
		<h1>Adjacency List PHP/MySQL Demo (version 2)</h1>
		<br>
		By Peter Drinnan
		<p>
		<p>
		
		
This is a simple class to handle adjacency hierarchy list operations within a MySQL database. 
This is a sanity saving alternative to Nested Sets (Joe Celco Sets) which can be problematic when your application requires 
a lot of node moves ending up with a tangled mess of left/right ids (YES I have been there).
<p>
<p>
Note that in this demo every time you reload the page, you will see the group ids increase. This is a live demo actually creating, moving and deleting notes. It isn't static.
<p>
<p>
For a full explanation of the Adjacency List Model, please see: http://en.wikipedia.org/wiki/Adjacency_list
<p>
<a href="adjacency_demo.zip">Click here</a> to download a working demo (requires MySQL).

<br>
<br>
<hr>

		
<?

/**

NOTE: You will need to create a data table for this demo. If the sql file is not attached with this file, please use the SQL query below:

CREATE TABLE IF NOT EXISTS `adjacency_demo_groups` (
  `ID` int(6) NOT NULL AUTO_INCREMENT,
  `parent_id` int(6) NOT NULL DEFAULT '0',
  `Group_Name` varchar(255) NOT NULL DEFAULT '',
  `Color` varchar(25) DEFAULT NULL,
  `position` int(6) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;


*/

///////////////////////////
// Change these parameters
$db_host = 'localhost';
$db_user = 'opcmf_adminz';
$db_pass = 'karissa202';
$db_name = '3daadmin_opcmf';
//
////////////////////




$dbConnectionID = @mysql_pconnect($db_host, $db_user, $db_pass);
	
if(!$dbConnectionID){
	echo(mysql_errno().":".mysql_error());
	exit;
}else{
	$status = @mysql_select_db($db_name, $dbConnectionID);
				
	if(!$status){
				
		echo "$db_name, $dbConnectionID";
		echo(mysql_errno().":".mysql_error());
		exit;
	}
} 

require_once("adjacency.class.php");

$adjtree = new adjacencyTree();

$adjtree->setidfield('ID');
$adjtree->setparentidfield('parent_id');
$adjtree->setpositionfield('position');
$adjtree->setdbtable('adjacency_demo_groups');
$adjtree->setDBConnectionID($dbConnectionID );



?>



<br>Initialize the Class <br>
<code>
<br>
<br>$dbConnectionID = @mysql_pconnect($db_host, $db_user, $db_pass);
<br>
<br>if(!$dbConnectionID){
<br>    echo(mysql_errno().":".mysql_error());
<br>     exit;
<br>}else{
<br>     $status = @mysql_select_db($db_name, $dbConnectionID);
<br>				
<br>     if(!$status){
<br>				
<br>     echo "$db_name, $dbConnectionID";
<br>     echo(mysql_errno().":".mysql_error());
<br>     exit;
<br>  }
<br>} 
<br>
<br>require_once("adjacency.class.php");
<br>
<br>$adjtree = new adjacencyTree();
<br>
<br>$adjtree->setidfield('ID');
<br>$adjtree->setparentidfield('parent_id');
<br>$adjtree->setpositionfield('position');
<br>$adjtree->setdbtable('adjacency_demo_groups');
<br>$adjtree->setDBConnectionID($dbConnectionID );
</code>



<br> Adding Nodes <br>
<code>
<br>$catinsertarray = array('Group_Name'=>'admin','Color'=>'AADDEE');
<br>$root_id = $adjtree->addNode(0,$catinsertarray);
<br>$catinsertarray = array('Group_Name'=>'managers','Color'=>'BBAAEE');
<br>$first_child_id = $adjtree->addNode($root_id,$catinsertarray);
<br>$catinsertarray = array('Group_Name'=>'advisors','Color'=>'BBAAFF');
<br>$first_child_sibling = $adjtree->addNode($root_id,$catinsertarray);
<br>$catinsertarray = array('Group_Name'=>'general staff','Color'=>'CCAAFF');
<br>$second_child_id = $adjtree->addNode($first_child_id,$catinsertarray);
<br>$catinsertarray = array('Group_Name'=>'field workers','Color'=>'99BBEE');
<br>$last_child_id  = $adjtree->addNode($second_child_id,$catinsertarray);
</code>
<?


$catinsertarray = array('Group_Name'=>'admin','Color'=>'AADDEE');

$root_id = $adjtree->addNode(0,$catinsertarray);

$catinsertarray = array('Group_Name'=>'managers','Color'=>'BBAAEE');

$first_child_id = $adjtree->addNode($root_id,$catinsertarray);

$catinsertarray = array('Group_Name'=>'advisors','Color'=>'BBAAFF');

$first_child_sibling = $adjtree->addNode($root_id,$catinsertarray);

$catinsertarray = array('Group_Name'=>'general staff','Color'=>'CCAAFF');

$second_child_id = $adjtree->addNode($first_child_id,$catinsertarray);

$catinsertarray = array('Group_Name'=>'field workers','Color'=>'99BBEE');

$last_child_id  = $adjtree->addNode($second_child_id,$catinsertarray);


?>
<br><br> Now showing full list <br>
<code>
<br>$groups = $adjtree->getFullNodes();
</code>
<?

$groups = $adjtree->getFullNodes();


echo "<h3>FULL LIST</h3><table>" . tabledorderlist($groups) . "</table>";


?>
<br> Now showing child list <br>
<code>
<br>$groups = $adjtree->getChildNodes($root_id);
</code>
<?

$groups = $adjtree->getChildNodes($root_id);


echo "<br><br><h3>CHILD LIST</h3><table>" . tabledorderlist($groups) . "</table>";

?>
<br> Now reparenting last child to root <br>
<code>
<br>$groups = $adjtree->reparentNode($last_child_id,$root_id);
<br>$groups = $adjtree->getFullNodes();
</code>
<?

$groups = $adjtree->reparentNode($last_child_id,$root_id);

$groups = $adjtree->getFullNodes();

echo "<h3>FULL LIST WITH LAST CHILD SET TO ROOT PARENT </h3><table>" . tabledorderlist($groups) . "</table>";

?>


<br> Add a sibling to the previous child <br>
<code>
<code>
<br>$catinsertarray = array('Group_Name'=>'new sibling','Color'=>'FF953F');
<br>$sibling_id = $adjtree->addSiblingNode($last_child_id,array());
<br>$groups = $adjtree->getSiblingNodes($sibling_id);
</code>
<?

$catinsertarray = array('Group_Name'=>'new sibling','Color'=>'FF953F');

$sibling_id = $adjtree->addSiblingNode($last_child_id,$catinsertarray);

$groups = $adjtree->getSiblingNodes($sibling_id);

echo "<h3>CHILD LIST WITH NEW SIBLING SHOWN AT TOP OF LIST </h3><table>" . tabledorderlist($groups) . "</table>";

?>


<br> Reposition the new sibling in the list <br>
<code>
<code>
<br>$adjtree->repositionSibling($sibling_id,"down");
<br>$adjtree->repositionSibling($sibling_id,"down");
<br>$groups = $adjtree->getSiblingNodes($sibling_id);
</code>
<?

$adjtree->repositionSibling($sibling_id,"down");
$adjtree->repositionSibling($sibling_id,"down");

$groups = $adjtree->getSiblingNodes($sibling_id);

echo "<h3>CHILD LIST WITH NEW SIBLING MOVED DOWN TWO POSITIONS IN LIST </h3><table>" . tabledorderlist($groups) . "</table>";

?>





<br> Now deleting second child <br>
<code>
<br>$groups = $adjtree->deleteNode($second_child_id);
<br>$groups = $adjtree->getFullNodes();
</code>

<?

$groups = $adjtree->deleteNode($second_child_id);

$groups = $adjtree->getFullNodes();

echo "<h3>FULL LIST WITH SECOND CHILD GONE </h3><table>" . tabledorderlist($groups) . "</table>";

?>
<br> Now deleting everything 

<code>
<br>$groups = $adjtree->deleteNode($root_id);
<br>$groups = $adjtree->getFullNodes();
</code>

<br>

<?

$groups = $adjtree->deleteNode($root_id);
$groups = $adjtree->getFullNodes();

echo "<h3>FULL LIST IS NOW EMPTY</h3><table>" . tabledorderlist($groups) . "</table>";



/**
* loop throught the adjacency array to build a view
*/
function tabledorderlist($items) {
	    
		global $opcmf, $dispitem, $group_indent, $Image_Color;
		
		$managegroupspageitems = '';

		if (count($items) && is_array($items)) {
			
			$group_indent ++;
			
			foreach ($items as $cat_id=>$catvals) {
			

		
				$dispitem['bgcolor'] = $catvals['Color'];
				
				if($dispitem[bgcolor] == "#CFCFCF"){ $dispitem[bgcolor]="#DDDDDD"; }
				else { $dispitem[bgcolor]="#CFCFCF"; }

				$this_group_Name = $catvals['Group_Name'];

				if($catvals['Color'] != "") $dispitem[bgcolor] = "#" . $catvals['Color'];

		
				$dispitem[group_name] = $this_group_Name;
		
				$dispitem[group_indent] = str_repeat("--",($group_indent-1));
		
				$dispitem[this_group_id] = $cat_id;
		
						
				$managegroupspageitems .= "<tr style=\"background-color:".$dispitem[bgcolor].";\">\n";
				$managegroupspageitems .= "<td style=\"background-color:".$dispitem[bgcolor].";\">[" . $cat_id . "]".$dispitem[group_indent]." ".$dispitem[group_name]."</td>\n";
				$managegroupspageitems .= "</tr>\n";
	
					
			
				if (count($catvals['children'])) {
				
					$managegroupspageitems .= tabledorderlist($catvals['children']);
				
				}
				
				
				
				    
			}
			
			$group_indent --;
			
			
		}
		
		
		return $managegroupspageitems;

}///////
	


?>   
	   </div>
        
        <div class="sidebar">
            <ul>	
               <li>
                    <h4>Snippets</h4>
                    <ul class="blocklist">
                        <li><a href="/adjacency_demo/">Adjacency List PHP Class</a></li>
                        <li><a href="/datetimepicker/">jQury Date/Time Picker</a></li>
                    </ul>
                </li>
                
           

      
            </ul> 
        </div>
    	<div class="clear"></div>
    </div>
    <div class="body-end"></div>
    <div id="footer">
    	<div class="footer-top"></div>
        <div class="footer-content">
			<p>&copy; 3dA Multimedia Inc. <a href="http://www.3da.com">3da Multimedia Inc</a> | <a href="http://www.3da.com">Web Application Development</a></p>
		</div>
        <div class="footer-bottom"></div>
    </div>
</div>
</body>
</html>
Return current item: Adjacency Model