Login   Register  
PHP Classes
elePHPant
Icontem

File: Example/Example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Thomas  >  SQL Object  >  Example/Example.php  >  Download  
File: Example/Example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: SQL Object
Retrieve MySQL query results as array objects
Author: By
Last change:
Date: 2012-01-15 12:03
Size: 1,790 bytes
 

Contents

Class file image Download
<?php
require "SQL_Object.php";

// Example extended class
class forumObject extends SQL_Object {
    function 
buildForum() {
        
$sql "SELECT * FROM `forum_cats` WHERE `PID` = '{$this->data[$this->pointer]['ID']}'";
        
$this->object[$this->pointer]['Forum'] = new forumObject($sql);
    }
}

// Make sure you connect to your database first.
$id_link mysql_connect("localhost""user""password");
mysql_select_db("database");

// ------------------------------------------------

// Example Recursive Forum Builder.
function buildForumCategories($forums) {
    
// Build a container so the child boards indent.
    
echo "<div style=\"margin-left: 10px;\">";
    
    
// We can use foreach to increment the internal pointer on the class.
    
foreach ($forums as $key => $forum) {
        
// Echo the forum name. (The field name is the MySQL row or whatever you specified in the query.
        
echo $forums->name "<br />";
        
        
// We can see if there are any child forums using count.
        // $forums->Forum will invoke PHP 5's magic method "__get"
        // SQL_Object also implements countable interface so we can
        // override the count function with our own.
        
if (count($forums->Forum) > 0) {
            
// Call child boards.
            
buildForumCategories($forums->Forum);
        }
    }
    
// We can also manually do it with a for:
    //         Example: for ($forums->rewind(); $forums->valid(); $forums->next())
    //
    // We could use the internal objects, but the methods work just fine.
    
    
echo "</div>";
}

// Do you're normal query for the initial boards.
$sql "SELECT * FROM `forum_cats` WHERE `PID` = 0";

// Pass the query to the extended class.
$forums = new forumObject($sql);

// Build the forum
buildForumCategories($forums);