<?php 
	

	class Usertype{
		
		public $db;
		public function __construct(){
			$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
		
			if(mysqli_connect_errno()) {
	 
				echo "Error: Could not connect to database.";
	 
			exit;
 
			}
		}

		
public function subcategories_dropdown($catid,$c,$editid){
	$sql_subcategories=mysqli_query($this->db,"select * from tbl_user_type where parent_id='$catid'");
	if(mysqli_num_rows($sql_subcategories)>0){
		while($result_list_subcategories=mysqli_fetch_array($sql_subcategories)){
			$subcategories[]=$result_list_subcategories;
		}
	}
	//return $subcategories;
	$c=$c+1;
		
		foreach($subcategories as $subcategory){
				$d="-";		
						for($i=0;$i<$c;$i++){
						$d.=$d;	
						}
				    ?>
                    
                    <option value="<?php echo $subcategory['typeid'];?>" <?php if( $editid==$subcategory['typeid']){ echo "selected"; } ?>><?php echo $d." ". $subcategory['title'];?></option>
<?php
$subcategories=$this->subcategories_dropdown($subcategory['typeid'],$c,$editid);
}
					
	
}
    	
public function list_product_categories_dropdown() { 
 
	$sql_list_category=mysqli_query($this->db,"select * from tbl_user_type where parent_id=0 order by typeid DESC");	
	 
	if(mysqli_num_rows($sql_list_category)>0){
		while($result_list_categories=mysqli_fetch_array($sql_list_category)){
			$result[]=$result_list_categories;
		}
	}
	
	return $result;
}
public function subcategories($catid,$c,$page,$site_url,$limitpage){
	$sql_subcategories=mysqli_query($this->db,"select * from tbl_user_type where parent_id='$catid'");
	if(mysqli_num_rows($sql_subcategories)>0){
		while($result_list_subcategories=mysqli_fetch_array($sql_subcategories)){
			$subcategories[]=$result_list_subcategories;
		}
	}
	//return $subcategories;
	    $c=$c+1;
		foreach($subcategories as $subcategory){
				$d="-";		
						for($i=0;$i<$c;$i++){
						$d.=$d;	
						}
						$valexist=$this->datapresent("tbl_user_type","parent_id",$subcategory['typeid']);
				        $valexist2=$this->datapresent("tbl_members","typeid",$subcategory['typeid']);
				    ?>
                     <tr>
                  <td> </td>
                  <td><?php echo $d." ". $subcategory['title'];?></td>
                  <td>
                  <?php
				  if($subcategory['status']==1){
				  ?>
<a href="<?php echo $site_url;?>admin/<?php echo $page;?>?mode=pending&contentid=<?php echo $subcategory['typeid'];?>&start=<?php echo $limitpage;?>"><span class="label label-success">Approved</span></a>
                  <?php
				  }
				  else {
				  ?>
				  <a href="<?php echo $site_url;?>admin/<?php echo $page;?>?mode=approve&contentid=<?php echo $subcategory['typeid'];?>&start=<?php echo $limitpage;?>"><span class="label label-danger">Pending</span></a>
                  <?php
				  }
				  ?>
                  </td>
                  <td><a href="<?php echo $site_url;?>admin/<?php echo $page;?>?mode=edit&contentid=<?php echo $subcategory['typeid'];?>&start=<?php echo $limitpage;?>"><span class="label label-primary"><i class="fa fa-edit"></i> Edit</span></a>
                  <?php
				  if($valexist==0 && $valexist2==0){
				  ?>
                   <a href="<?php echo $site_url;?>admin/<?php echo $page;?>?mode=delete&contentid=<?php echo $subcategory['typeid'];?>&start=<?php echo $limitpage;?>" onclick="return getConfirmation()"><span class="label label-danger"><i class="fa fa-times"></i> Delete</span></a>
                   <?php
				  }
				  ?>
                   </td>
                </tr>
                    
                    
					<?php
					$subcategories=$this->subcategories($subcategory['typeid'],$c,$page,$site_url,$limitpage);
					}
					
	
}
   public function datapresent($table,$wherefield,$fieldvalue) {
	    $sql = mysqli_query($this->db, "select count(*) as total from ".$table." where ".$wherefield."='$fieldvalue'");
        $result= mysqli_fetch_array($sql);
        return $result['total'];
	 }

	}
?>