Jump to content

how can i take some normal php functions and put them in to an oop class?


ricky spires

Recommended Posts

hello

 

i have some normal php functions that i want to put into a oop class.

 

i cant seem to make the right changes to make it work. could some one help please. thanks

 

these are the php functions

 

<?php

  function hasChild($parent_id)
  {
    $sql = "SELECT COUNT(*) as count FROM category WHERE parent_id = '" . $parent_id . "'";
    $qry = mysql_query($sql);
    $rs = mysql_fetch_array($qry);
    return $rs['count'];
  }
  
  function CategoryTree($list,$parent,$append)
  {
    $list = '<li>'.$parent['name'].'</li>';
    
    if (hasChild($parent['id'])) // check if the id has a child
    {
      $append++;
      $list .= "<ul class='child child".$append."'>";
      $sql = "SELECT * FROM category WHERE parent_id = '" . $parent['id'] . "'";
      $qry = mysql_query($sql);
      $child = mysql_fetch_array($qry);
      do{
        $list .= CategoryTree($list,$child,$append);
      }while($child = mysql_fetch_array($qry));
      $list .= "</ul>";
    }
    return $list;
  }
  function CategoryList()
  {
    $list = "";
    
    $sql = "SELECT * FROM category WHERE (parent_id = 0 OR parent_id IS NULL)";
    $qry = mysql_query($sql);
    $parent = mysql_fetch_array($qry);
    $mainlist = "<ul class='parent'>";
    do{
      $mainlist .= CategoryTree($list,$parent,$append = 0);
    }while($parent = mysql_fetch_array($qry));
    $list .= "</ul>";
    return $mainlist;
  }
?>

 

this is the class

 

 

<?PHP
require_once(LIB_PATH.DS.'database.php');

class Menu extends DatabaseObject {

protected static $table_name="menu";

protected static $db_fields = array(
'id',
'parent_id',
'name'
);


public $id;
public $parent_id;
public $name;


// "new" is a reserved word so we use "make"(or "build")
public static function make(

$id,
$parent_id,
$name) {

	if(!empty($id)) {

		$kw = new Menu();
		$kw->id = (int)$id;
		$kw->parent_id = (int)$parent_id;
		$kw->name = $name;

		return $kw;
	}else{
		return false;
	}
} //end function make


//PUT FUNCTIONS HERE......

  function hasChild($parent_id)
  {
    $sql = "SELECT COUNT(*) as count FROM category WHERE parent_id = '" . $parent_id . "'";
    $qry = mysql_query($sql);
    $rs = mysql_fetch_array($qry);
    return $rs['count'];
  }
  
  function CategoryTree($list,$parent,$append)
  {
    $list = '<li>'.$parent['name'].'</li>';
    
    if (hasChild($parent['id'])) // check if the id has a child
    {
      $append++;
      $list .= "<ul class='child child".$append."'>";
      $sql = "SELECT * FROM category WHERE parent_id = '" . $parent['id'] . "'";
      $qry = mysql_query($sql);
      $child = mysql_fetch_array($qry);
      do{
        $list .= CategoryTree($list,$child,$append);
      }while($child = mysql_fetch_array($qry));
      $list .= "</ul>";
    }
    return $list;
  }
  function CategoryList()
  {
    $list = "";
    
    $sql = "SELECT * FROM category WHERE (parent_id = 0 OR parent_id IS NULL)";
    $qry = mysql_query($sql);
    $parent = mysql_fetch_array($qry);
    $mainlist = "<ul class='parent'>";
    do{
      $mainlist .= CategoryTree($list,$parent,$append = 0);
    }while($parent = mysql_fetch_array($qry));
    $list .= "</ul>";
    return $mainlist;
  }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.