<?php
require_once 'Mysql.php'; // include this file so we can use the Mysql Class inside this class as well.

class Unit
{
    // In this class we'll have a few variables to be used by the class.
    var $mysql; // the mysql variable that will hold the Mysql Object.
    
    public function __construct()
    {
        $this->mysql = new Mysql(); // Connect to mysql and the database so we can use it.
    }
    // This function does what it says, it gets all of the current news records from the db.
    public function getUnit($table,$order_by)
    {
        $rset = $this->mysql->select('*',$table,$order_by);
        return $rset;
    }
     // You should update this function how you see fit. You could add more params to return only
    // the fields you want.
       
    // This will get a single record in the database.
    public function getUnitById($id,$table)
    {
        $rset = $this->mysql->select('*',$table,$id);
        return $rset;
    }
	/******************************************************************************************************/
	 public function getGroupUnit($group_id,$table)
    {
        $rset = $this->mysql->select($group_id,$table,"","");
        return $rset;
    }
    
    // Your going to need some way to add and edit your news entries right?
    // well here ya go :P
    
    // This function will save you lots of time writing out the fields variable everytime.
    public function buildFields($post, $sep=" ") // $post comes in as an array of variables.
    {    
        $fields = ""; // This makes sure we don't run into any past fields.
        foreach($post as $key => $value)
        {
            // foreach will take each element of the $post array and seperate
            // each of the values with its key $post[key] = value;
            $value = mysql_escape_string($value); // We'll do a small security check here.
            // I'll explain that in another tutorial. Basically it protect mysql from hackers.
            if($i == 0)
                $fields .= "$key='$value'";
            else
                $fields .= $sep . "$key='$value'";
            // This will create your fields string based on each element in the post array.
            $i++;
        }
        return $fields; // Return the string, $fields.
    }
    public function addNews($post,$t_name)
    {
        $fields = $this->buildFields($post, ", "); // take the post array and break it into a string.
        if( $this->mysql->insert($t_name,$fields) ) // This is pretty basic. Inserts the new news record.
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    // This function is just the same as addnews, except that it updates an existing record.
    public function editNews($post,$t_name,$where)
    {
        $fields = $this->buildFields($post, ", ");
       // $newsId = $post['newsId']; // retreive the newsId we need to update
        if( $this->mysql->update($t_name,$fields,$where) )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public function deleteNews($newsId,$t_name)
    {
        if( $this->mysql->delete($t_name,$newsId) )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
?>