|
|
|
PHP code to generate a Dtree javascript treeview output of a Mysql tree table
DTREE FROM http://www.destroydrop.com/javascript/tree
| [code]
<?php
/*
Name Program : datatreeview.php
Author : Aitor Solozabal Merino (Spain)
Email : aitor-3@euskalnet.net
Date : 02-04-2008
Type : php program code utility
Description : Dtree javascript treeview output of Mysql tree table
: FROM www.destroydrop.com/javascript/tree
*/
function send_query($db, $link, $sql)
{
/*This function returns a query result from a sql command query execution*/
global $MYSQLPHPEXTENSION;
$result = false;
if ($MYSQLPHPEXTENSION == "mysqli") {
if (mysqli_select_db($link, $db)) {
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)) {
echo "MySQL error " . mysqli_errno($link) . ": " . mysqli_error($link) . "\n<br>When executing:<br>\n$sql\n<br>";
}
} else {
echo "MySQL error " . mysqli_errno($link) . ": " . mysqli_error($link) . "\n<br>When selecting database $db\n<br>";
}
} else {
if (mysql_select_db($db, $link)) {
$result = mysql_query($sql, $link);
if (mysql_errno($link)) {
echo "MySQL error " . mysql_errno($link) . ": " . mysql_error($link) . "\n<br>When executing:<br>\n$sql\n<br>";
}
} else {
echo "MySQL error " . mysql_errno($link) . ": " . mysql_error($link) . "\n<br>When selecting database $db\n<br>";
}
}
return $result;
}
function show_table($result, $aligntable = "center", $aligncells = "center")
{
/*This function shows an HTML Table with data from rows of a query result argument parameter*/
global $MYSQLPHPEXTENSION;
$header_fields = false;
echo "<table border=2 align=" . $aligntable . " cellpadding=2 cellspacing=2>";
echo "<tr><td align=right>DATABASE--></td><td bgcolor=BLACK align=center><Font COLOR=ORANGE>" . $GLOBALS['DATABASE'] .
"<td align=right>TABLE--></td><td bgcolor=BLACK align=center><Font COLOR=ORANGE>" . $GLOBALS['TABLE'] . "</td></tr>";
if ($result) {
if ($MYSQLPHPEXTENSION == "mysqli") {
while ($row = mysqli_fetch_assoc($result)) {
if (!$header_fields) {
echo "<tr bgcolor=ORANGE>";
while (list($k, $v) = each($row)) {
echo "<th>$k</th>";
}
;
echo "</tr>";
$header_fields = true;
reset($row);
}
echo "<tr bgcolor=BLACK>";
while (list($k, $v) = each($row)) {
echo "<td align=" . $aligncells . "><Font COLOR=ORANGE>$v</td>";
}
;
echo "</tr>";
}
} else {
while ($row = mysql_fetch_assoc($result)) {
if (!$header_fields) {
echo "<tr bgcolor=ORANGE>";
while (list($k, $v) = each($row)) {
echo "<th>$k</th>";
}
;
echo "</tr>";
$header_fields = true;
reset($row);
}
echo "<tr bgcolor=BLACK>";
while (list($k, $v) = each($row)) {
echo "<td align=" . $aligncells . "><Font COLOR=ORANGE>$v</td>";
}
;
echo "</tr>";
}
}
} else {
echo "<tr>";
echo "<td>No hay registros en la consulta para mostrar</td>" . "\n";
echo "</tr>\n";
}
echo "</table>";
}
function fill_the_tree($result)
{
global $MYSQLPHPEXTENSION,$cod,$parent,$name;
/*This function fills the nodes of the DTREE tree with data from rows of a query result argument parameter*/
if ($result) {
if ($MYSQLPHPEXTENSION == "mysqli") {
while ($row = mysqli_fetch_array($result)) {
echo "d.add(" . $row[$cod] . "," . $row[$parent] . ",'" . addslashes($row[$name]) .
"');";
}
} else {
while ($row = mysql_fetch_array($result)) {
echo "d.add(" . $row[$cod] . "," . $row[$parent] . ",'" . addslashes($row[$name]) .
"');";
}
}
}
}
//======================================================================
/*
* M A I N P R O G R A M *
*/
//======================================================================
/* SQL CODE TO CREATE AND POPULATE THE SAMPLE MYSQL TABLE
drop table if exists `tab_treeview`;
CREATE TABLE `tab_treeview` (
`treeview_cod` int(11) NOT NULL auto_increment,
`treeview_parent_cod` int(11) NOT NULL default '0',
`treeview_name` text NOT NULL,
`treeview_desc` text NOT NULL,
PRIMARY KEY (`treeview_cod`)
) ENGINE=MyISAM;
INSERT INTO `tab_treeview` VALUES (1,0,'root','folder');
INSERT INTO `tab_treeview` VALUES (2,1,'parent1','folder');
INSERT INTO `tab_treeview` VALUES (3,1,'parent2','document');
INSERT INTO `tab_treeview` VALUES (4,1,'parent3','folder');
INSERT INTO `tab_treeview` VALUES (5,2,'child1','folder');
INSERT INTO `tab_treeview` VALUES (6,2,'child2','document');
INSERT INTO `tab_treeview` VALUES (7,5,'grandchild1','document');
INSERT INTO `tab_treeview` VALUES (8,5,'grandchild2','document');
INSERT INTO `tab_treeview` VALUES (9,4,'child3','folder');
INSERT INTO `tab_treeview` VALUES (10,4,'child4','document');
INSERT INTO `tab_treeview` VALUES (11,9,'grandchild3','folder');
INSERT INTO `tab_treeview` VALUES (12,11,'grandgrandchild1','document');
INSERT INTO `tab_treeview` VALUES (13,11,'grandgrandchild2','document');
*/
//======================================================================
// ONLY THESE VARIABLES MUST BE SET BY THE PROGRAMMER
$MYSQLPHPEXTENSION = "mysql";// or "mysqli"
$HOST = "localhost"; // or whatever
$USER = "root"; // or whatever
$PASSWORD = "lbsb2vb"; // or whatever
$DATABASE = "test"; // or whatever
$TABLE = "tab_treeview"; // or whatever
$cod="treeview_cod"; // or whatever
$parent="treeview_parent_cod";// or whatever
$name="treeview_name"; // or whatever
$desc="treeview_desc"; //not neccesary to display the tree
//======================================================================
if ($GLOBALS['MYSQLPHPEXTENSION'] == "mysqli") {
$LINK = mysqli_connect($HOST, $USER, $PASSWORD, $DATABASE) or die("No se ha conectado");
} else {
$LINK = mysql_connect($HOST, $USER, $PASSWORD, $DATABASE) or die("No se ha conectado");
}
//======================================================================
$QUERY = "select ".$cod.",".$name.",".$desc.",".$parent." from " . $TABLE; //for output the data in a html table
show_table(send_query($DATABASE, $LINK, $QUERY), "left", "center");
?>
<!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>
<title>MySql DataFile Treeview with dTree 2.05 | www.destroydrop.com/javascript/tree</title>
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js">
</script>
</head>
<body>
<h3>DATAFILE TREEVIEW PHP with dTree 2.05<br />
<a href="http://www.destroydrop.com/javascripts/tree/default.html">www.destroydrop.com/javascript/tree</a></h3>
<div class="dtree">
<p><a href="javascript:d.openAll();">open all</a> | <a href="javascript:d.closeAll();">close all</a> | <a href="<?echo $_SERVER['PHP_SELF'];?>"><img src="img/tree_refresh1.gif" border="0" alt="rebuild tree" /> refresh</a></p>
<script type="text/javascript">
//<![CDATA[
<!--
d = new dTree('d');
d.config.target="mytarget";
d.config.folderLinks =false;
d.config.useSelection =true;
d.config.useCookies =true;
d.config.useLines=true;
d.config.useIcons=true;
d.config.useStatusText=true;
d.config.closeSameLevel=false;
d.config.inOrder =false;
d.icondir="img3/";
d.add(0,-1,'Example DB tree');
<?php
// Node(id, pid, name, url, title, target, icon, iconOpen, open)
$QUERY = "select ".$cod.",".$name.",".$parent." from " . $TABLE; //without $desc field for output the data in a javascript tree
fill_the_tree(send_query($DATABASE, $LINK, $QUERY));
//======================================================================
if ($GLOBALS['MYSQLPHPEXTENSION'] == "mysqli") {
IF ($QUERY_RESULT) mysqli_free_result($QUERY_RESULT);
mysqli_close($LINK);
} else {
IF ($QUERY_RESULT) mysql_free_result($QUERY_RESULT);
mysql_close($LINK);
}
//======================================================================
?>
document.write(d);
//-->
//]]>
</script>
</div>
</body>
</html> | | |
|
| Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | A Complete table(ADD,EDIT,VIEW,DELETE) management System PHP,MYSQL, JAVASCRIPT Categories : PHP, MySQL, Java Script, Databases | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | A Simple sign up script with PHP and JavaScript validations. Categories : PHP, Java Script, MySQL, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | AJAX Data Grid System using php and mysql. A complete login system with the ability to display data in a grid using ajax. Add , update and delete the records without reloading the page. Categories : PHP, AJAX, Databases, MySQL, Java Script | | | Building dynamic menus with PHP & MySQL (ADO), JavaScript and CSS Categories : PHP, Databases, MySQL, Java Script, User Interface | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | |
|
|
|