PHP Classes

File: myXTree/sample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Tereshchenko Andrey  >  myXTree  >  myXTree/sample.php  >  Download  
File: myXTree/sample.php
Role: Example script
Content type: text/plain
Description: Example
Class: myXTree
myXTree is an interface to a SQL-tree.
Author: By
Last change: -
Date: 10 years ago
Size: 5,098 bytes


Class file image Download
* In front of use of this example necessary to create a database in mySQL.
* SQL queries for creation of tables can be taken from file base.sql in root
* directory. After this necessary to fill the tables by data from file tree.sql
* in root directory. After it need specified the linking up parameters to
* database beneath in this example or in file config.ini.

$user = 'root';
$pass = '';
$host = 'localhost';
$name = 'myXTree';
$prefix = 'xt_';

$config['PATH']['pear'] = $root.'/PEAR';
$config['PATH']['myxml'] = $root.'/myXML';
$config['PATH']['myxtree'] = $root.'/myXTree';

getmicrotime() {
$usec, $sec) = explode(" ", microtime());
   return ((float)
$usec + (float)$sec);
$time_start = getmicrotime();

if (
file_exists('config.ini')) {
$config = parse_ini_file('config.ini', true);
extract($config['DB'], EXTR_OVERWRITE);

$dsn = "mysql://$user:$pass@$host/$name";

if (
substr(PHP_OS, 0, 3) == 'WIN') {
$searchPath = implode(';', $config['PATH']).';';
} else {
$searchPath = implode(':', $config['PATH']).':';
// Set the search path.
ini_set('include_path', $searchPath);



// Create new DOM document.
$oDocument = new Document;
$oDocument->setOption('indent', true);
$oDocument->setOption('method', 'xml');

// Connecting to database.
$db = DB::connect($dsn);
PEAR::isError($db) and

// Set the selecting path.
$path = '/books//';
if (
$_GET['xpath']) {
$path = stripslashes($_GET['xpath']);

// Create object myXTree.
$oXTree = myXTree::create(&$db, $prefix);

// Set method of selecting.
if ($_GET['method'] == 'recursive') {
// Set the reception node.

// Selecting objects from SQL-tree.
// Disconnecting.


    <title>myXTree example</title>
  <body style="margin: 10%; font-family: Courier New;">
      <h1>myXTree example</h1>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/books/book</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/books/book/*[name()!="price"]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book/descendant-or-self::node()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=1 or @id=3 or @id>7]//</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[price > 10]//</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[author="Plato" or author="Sophocles"]//</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/following::node()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/preceding::node()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/following-sibling::node()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/preceding-sibling::node()</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title/parent::book[@id >= 5]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title/../author[.="Sophocles"]//</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//price/ancestor::book[@id!=5]</li>
        <li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title[parent::book/@id=5]/ancestor::node()</li>
      <script language="php">
= getmicrotime();
$time = $time_end - $time_start;
"<br>Time: $time s.";
        if (
$oldtime = $_GET['time']) {
$rate = ($oldtime - $time)/$oldtime*100;
"<br>Rate: $rate %.";
<form name="pathForm">
        <input type="hidden" name="time" value="<?php echo $time; ?>">
        <input type="radio" name="method" value="selfjoin" checked> Selfjoin method
        <input type="radio" name="method" value="recursive"> Recursive method <br>
        <input type="text" name="xpath" style="width: 400;">
        <input type="submit" name="submit" value="Submit">
    <div style="color: blue; background-color: silver; padding: 1%;">
      <script language="php">
print '<pre>'.htmlentities($oDocument->toString()).'</pre>';