PHP Classes

File: examples/advanced_pdo.php

Recommend this page to a friend!
  Classes of Thomas Björk  >  TTreeList  >  examples/advanced_pdo.php  >  Download  
File: examples/advanced_pdo.php
Role: Example script
Content type: text/plain
Description: Advanced example using PDO
Class: TTreeList
Create a binary tree from array or database result
Author: By
Last change: Initial push to GitHub
Date: 10 months ago
Size: 1,699 bytes


Class file image Download
include '../treelist.php';

// Create a map with a few character combinations
$map = array('as', 'be', 'ca', 'da', 'en', 'fo', 'gu', 'he', 'in', 'ja');

// Create a database (in memory)
$db = new PDO('sqlite::memory:');
$db->exec('CREATE TABLE testdata (id integer primary key, name string, dummy integer);');
// Create 1000 entries in the database by creating random character combinations
// based on the map defined earlier
for($i = 0; $i < 1000; $i++) {
$value = '';
$j = 0; $j < 3; $j++) {
$value .= $map[rand(0, 9)];
$db->exec('INSERT INTO testdata (name,dummy) VALUES (\''.$value.'\', '.rand(0,100).');');

// Execute a query that returns all 1000 entries
$rs = $db->query('SELECT * FROM testdata', PDO::FETCH_ASSOC);

// Create a TreeList
$tree = new TTreeList();
// Assign the result of the database query as the data content for the tree

// Check how many records we have
echo "The treelist has ".count($tree)." nodes".PHP_EOL;
// Loop the tree as an array
foreach($tree as $node) {
' '.$node["id"].' - '.$node["name"];

// Build the tree and index it on the name field
echo "Building tree".PHP_EOL;

// Display the amount of nodes in the tree (this will probably be somwhere
// around 620-640 items)
echo "There is ".$tree->treeCount." nodes in the tree".PHP_EOL;

// Get the first node in the treelist
$node = $tree->GetFirst();
$count = 0;
// Walk through the treelist
while($node !== null) {
$node['name'].' ';
$node = $node->next;
// Display how many nodes we have passed (should be equal to the treeCount)
echo "We walked ".$count." nodes".PHP_EOL;

For more information send a message to info at phpclasses dot org.