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;
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.