Jump to content

[SOLVED] Categories and Sub categories in PHP


nitation

Recommended Posts

I am working on a project that requires categories and subcategories. I can successfully insert and retrieve from my table. My question is the HTML alignment/structure when outputted. Currently, this is the structure when i output.

 

-Root (1)

    -Category-1 (2)

        -Subcat-1 (3)
        -Subcat-2 (4)

    -Category-2 (5)

        -Sub-1 (6)
        -Sub-1 (6)

 

What am trying to achieve is something like this:

 

Root:            Category1                                    Category2

                    Subcat1                                      Subcat1

                    Subcat2                                      Subcat2

 

In an HTML Table. Just outputting. My coding is fine and working alright

Link to comment
Share on other sites

<?php error_reporting(E_ALL); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Categories Class Test .. By Shadi Ali</title>
<style>
.code_div{
border:dashed #006600 1px;
background:#E8FFEB;
font-family:Georgia, "Times New Roman", Times, serif;
font-size:11px;
width:90%;
padding:5px;
}
</style>
</head>
<body style="font-family:Tahoma">
<p>
  <?php


// connect to database
include("connect.php");

require_once("categories.class.php");

// Simple Usage
if(!isset($_GET["id"])) $_GET["id"] =0;

$ctg_id = $_GET["id"];

$categories = new categories;
$categories->name_prefix = "  ";

?>
</p>
<p>Here is it a Html Menu:</p>
<br>
<div class="code_div"> <code><font color="#000000"><font color="#0000bb">$categories </font><font color="#007700">= new </font><font color="#0000bb">categories</font><font color="#007700">; </font></font><br><font color="#000000"><font color="#ff8000">        </font><font color="#0000bb">$output          </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">html_output</font><font color="#007700">(</font><font color="#0000bb">$ctg_id</font><font color="#007700">); <br>
        echo </font><font color="#0000bb">$output</font><font color="#007700">;</font></font></code> </div>
<p>  
  <?
// lets display the categories

	$output 		 = $categories->html_output($ctg_id);
	echo $output;	
?>
</p>
<hr>
<p>now I'll modify the $HtmlTree value a bit.</p>
<div class="code_div"> <code><font color="#000000"><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">HtmlTree </font><font color="#007700">= array( <br>
</font><font color="#dd0000">"header" </font><font color="#007700">=> </font><font color="#dd0000">"<table width=200px border=0 cellpadding=2 cellspacing=2>"</font><font color="#007700">, <br>
</font><font color="#dd0000">"BodyUnselected" </font><font color="#007700">=> </font><font color="#dd0000">'<tr><td  bgcolor=#C4D9FD >[prefix]&raquo;<a  href="?id=[id]"><font  color=#53507A>[name]</font></a></td></tr>'</font><font color="#007700">, <br>
</font><font color="#dd0000">"BodySelected" </font><font color="#007700">=> </font><font color="#dd0000">'<tr><td  bgcolor="#E4DB2C">[prefix]&bull;<a  href="?id=[id]"><strong><font  color="#000000">[name]</font></strong></a></td></tr>'</font><font color="#007700">, <br>
</font><font color="#dd0000">"footer" </font><font color="#007700">=> </font><font color="#dd0000">'</table>'</font><font color="#007700">, <br>
); <br>
<br>
</font><font color="#0000bb">$output </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">html_output</font><font color="#007700">(</font><font color="#0000bb">$ctg_id</font><font color="#007700">); <br>
echo </font><font color="#0000bb">$output</font><font color="#007700">; </font></font></code> </div>


<?php

$categories->HtmlTree = array(
"header" => "<table width=200px border=0 cellpadding=2 cellspacing=2>", 
"BodyUnselected" => '<tr><td bgcolor=#C4D9FD >[prefix]»<a href="?id=[id]"><font color=#53507A>[name]</font></a></td></tr>',
"BodySelected" => '<tr><td bgcolor="#E4DB2C">[prefix]•<a href="?id=[id]"><strong><font color="#000000">[name]</font></strong></a></td></tr>',
"footer" => '</table>',
);

$output = $categories->html_output($ctg_id); 
echo $output;
?>
<hr>
<?

// lets get an array of the categories for our works
$categories = new categories;
$categories_list = $categories->build_list();

// lets do some actions
if(!isset($_REQUEST['act'])) $_REQUEST['act'] = "";

$act     = $_REQUEST["act"];

switch($act)
{
case "add":

	// lets add new category
	//  $categories->add_new( category parent ,  category name , description , icon path , group )

	$categories->add_new($_POST['parent'] , $_POST["name"] , $_POST["desc"] , $_POST["icon"] );
	echo '<script>alert("Category was inserted successfully into database");
	 	 location="class_categories_test.php";	
  		</script>';
break;

case "delete":
	$categories->delete($_GET["id"]);
		echo '<script>alert("Category and Sub-Categories was successfully deleted");
	 	 location="class_categories_test.php";	
  		</script>';

break;

case "_update":
	$cat = $categories->fetch($_GET["id"]);

	?>
	<form name="form1" method="post" action="">
          <p> the form below will execute the following. <br>
</p>
          <div class="code_div"> <code><font color="#000000"><font color="#007700"> </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">update</font><font color="#007700">(</font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"id"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"parent"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"name"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"desc"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"icon"</font><font color="#007700">] );</font></font></code> </div>
          <br>
          <table width="90%"  border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td colspan="3"><div align="center">Edit Category </div></td>
    </tr>
    <tr>
      <td width="13%">Child Of : </td>
      <td width="1%">:</td>
      <td width="86%">
  <select name="parent" id="parent">
        <option value="0" selected>ROOT</option>
	<?
	foreach($categories_list as $c)
	{
		if($cat["id"] == $c["id"]) continue; // we don't list the category to be child of itself !!
		?>
		<option value="<?=$c["id"]?>" <? if($c["id"] == $cat["parent"]) echo "selected"; ?> ><?=$c["prefix"]?>»<?=$c["c_name"]?></option>
		<?	
	}
	?>
      </select>
  </td>
    </tr>
    <tr>
      <td>Name:</td>
      <td>:</td>
      <td><input name="name" type="text" id="name" value="<?=$cat["c_name"]?>" size="20"></td>
    </tr>
    <tr>
      <td>Description:</td>
      <td>:</td>
      <td><textarea name="desc" cols="40" rows="3" id="desc"><?=$cat["c_desc"]?>
      </textarea></td>
    </tr>
    <tr>
      <td>Icon:</td>
      <td>:</td>
      <td><input name="icon" type="text" id="icon" value="<?=$cat["c_icon"]?>" size="30"></td>
    </tr>
    <tr>
      <td colspan="3"><div align="right">
        <input name="act" type="hidden" value="update">
	<input name="id" type="hidden" value="<?=$ctg_id?>">
	<input type="submit" name="Submit" value="Save">
      </div></td>
    </tr>
  </table>
</form>
	 <?
	 die();

break;

case "update":
	$categories->update($_POST["id"] , $_POST["parent"] , $_POST["name"] , $_POST["desc"] , $_POST["icon"] );

	echo '<script>alert("Category was updated successfully!");
	 	 location="class_categories_test.php";	
  		</script>';
break;

}



?>

         <p align="center"> </p>
         <p align="center"><strong>You may list all your categories expanded like this</strong></p>
<div class="code_div">
  <code><font color="#000000"><font color="#0000bb">$categories </font><font color="#007700">= new </font><font color="#0000bb">categories</font><font color="#007700">; <br>
      </font><font color="#0000bb">$categories_list </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">build_list</font><font color="#007700">(0);</font></font></code>
  <br>// $categories_list is now an array and you should use foreach command to print something like the following list.
</div>
        <br>
         <table width="90%"  border="0" align="center" cellpadding="2" cellspacing="0">
	<tr>
		<td bgcolor="#4180BE"><font color="#FFFFFF"><strong>
		Categories List:
		</strong></font></td>
	</tr>
	<?
		foreach($categories_list as $c)
		{
			?>

			<tr>
				<td>

				<?=$c["prefix"]?>»<?=$c["c_name"]?> - [<a href="?act=_update&id=<?=$c["id"]?>">Edit</a> - <a href="?act=delete&id=<?=$c["id"]?>">Delete</a>]
				</td>
			</tr>

			<?
		}
	?>
	</table>



<p align="center"> </p>
<p align="center"><strong>Or you may also use it in a combo box</strong></p>
<table width="0" border="0" align="center" cellpadding="2" cellspacing="0">
  <tr>
    <td width="72"><select name="category_id" id="category_id">
    
      <?
	foreach($categories_list as $c)
	{
		?>
		 <option value="<?=$c["id"]?>"><?=$c["prefix"]?>»<?=$c["c_name"]?> </option>
      <?	 
	}
	?>
            </select></td>
    <td width="0"><input type="button" name="Button" value="Edit" onClick="location='class_categories_test.php?act=_update&id='+document.getElementById('category_id').value;"></td>
    <td width="0"><input type="button" name="Button" value="Delete" onClick="location='class_categories_test.php?act=delete&id='+document.getElementById('category_id').value;"></td>
  </tr>
</table>
<p>  </p>
<p><hr></p>
<form name="form1" method="post" action="">
the form below will execute the following.
  <br>
<div class="code_div">
  <code><font color="#000000"><font color="#ff8000"> </font><font color="#0000bb">$categories</font><font color="#007700">-></font><font color="#0000bb">add_new</font><font color="#007700">(</font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">'parent'</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"name"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"desc"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"icon"</font><font color="#007700">] ); </font></font></code></div><br>
  <table width="400"  border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td colspan="3" bgcolor="#4180BE"><div align="center"><strong><font color="#FFFFFF">Add New Category </font></strong></div></td>
    </tr>
    <tr>
      <td width="13%">Child Of : </td>
      <td width="1%">:</td>
      <td width="86%"><select name="parent" id="parent">
        <option value="0">ROOT</option>
        <?
	foreach($categories_list as $c)
	{
		?>
        <option value="<?=$c["id"]?>"  >
        <?=$c["prefix"]?>»<?=$c["c_name"]?>
        </option>
        <?	
	}
	?>
      </select></td>
    </tr>
    <tr>
      <td>Name:</td>
      <td>:</td>
      <td><input name="name" type="text" id="name" size="20"></td>
    </tr>
    <tr>
      <td>Description:</td>
      <td>:</td>
      <td><textarea name="desc" cols="40" rows="3" id="desc"></textarea></td>
    </tr>
    <tr>
      <td>Icon:</td>
      <td>:</td>
      <td><input name="icon" type="text" id="icon" value="http://" size="30"></td>
    </tr>
    <tr>
      <td colspan="3"><div align="right">
        <input name="act" type="hidden" value="add">
        <input type="submit" name="Submit" value="Add">
      </div></td>
    </tr>
  </table>

</form>

<p align="center"> </p>

</body>
</html>

 

 

class.php

 

<?php
class categories
{


var $HtmlTree;

var $name_prefix  = "  ";	// this is the prefix which will be added to the category name depending on its position usually use space.
var $table_name   = "categories";
var $itemsTable   = "items";		// this is the name of the table which contain the items associated to the categories
var $CID_FieldName= "category_id";	 // this is the field name in the items table which refere to the ID of the item's category.

// use the following keys into the $HtmlTree varialbe.
var $fields = array(
// field		=> field name in database ( sql structure )
"id"  		=> "id",
"position" 	=> "position",
"name"		=> "c_name",
"desc"		=> "c_desc",
"icon"		=> "c_icon",
"group"		=> "c_group",
);
/**************************************************
--- NO CHANGES TO BE DONE BELOW ---
**************************************************/

var $c_list  = array();  // DON'T CHANGE THIS
var $Group  = 0;		 // DON'T CHANGE THIS

function categories()
{

$this->HtmlTree = array(
"header" 		 => '<table width=300px border=0 cellpadding=2 cellspacing=2>',
"BodyUnselected" => '<tr><td>[prefix]»<a href="?id=[id]">[name]</a></td></tr>',
"BodySelected"	 => '<tr><td>[prefix]»<a href="?id=[id]"><strong>[name]</strong></a></td></tr>',
"footer"		 => '</table>',
);

}

// ********************************************************
//		Add New Category
// ********************************************************


function add_new($parent = 0 , $name , $desc , $icon )  // add new category
{
// lets get the position from the $parent value
$position  = $this->get_position($parent);

// lets insert add the new category into the database.
$sql = "INSERT into ".$this->table_name."(position,c_name,c_desc,c_icon,c_group)
	VALUES('','".$name."','".$desc."','".$icon."','".$this->Group."')";

mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

$position .= mysql_insert_id().">";

$sql = "UPDATE ".$this->table_name."
	SET position = '".$position."'
	WHERE id = '".mysql_insert_id()."'";

mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
}

// ********************************************************
//		Delete Category
// ********************************************************

function delete($id,$items=NULL) // delete this category and all categories under it [set $items=1 if you need to delete associated items too, needs the 2 variables $itemsTable,$CID_FieldName]
{
$position = $this->get_position($id);

if($items==1) // delete associated items
{
if($this->itemsTable == "" OR $this->CID_FieldName==""){
die("<br><storng><u>Class Error:</u></strong><br>Either items Table name Or CID field name is blank!<br><br>");
}

$sql = "SELECT id
	FROM ".$this->table_name."
	WHERE position LIKE '".$position."%'";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

while($category = mysql_fetch_array($res)){

$sql2 = "Delete	FROM  ".$this->itemsTable."
	WHERE ".$this->CID_FieldName." = '".$category["id"]."'";
$res2 = mysql_query($sql2) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
}
}

$sql = "DELETE FROM ".$this->table_name."
	WHERE position
	LIKE '".$position."%'";
mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
}


// ********************************************************
//		Update Category
// ********************************************************

function update($id , $parent = 0 , $name = 0 , $desc = 0 , $icon = 0 ,$group = 0)
{
// lets see if there is a change on the group
if($group == 0){
$this_category = $this->fetch($id);
$group = $this_category['c_group'];
}

// lets get the current position
$position     = $this->get_position($id);
$new_position = $this->get_position($parent).$id.">";

if($position != $new_position){
// then we update all the sub_categories position to be still under the current category
$sql1 = "SELECT id,position
	FROM ".$this->table_name."
	WHERE position	LIKE  '".$position."%'";
$res = mysql_query($sql1) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql1."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

while($sub = mysql_fetch_array($res)){
$new_sub_position = str_replace($position,$new_position,$sub['position']);
$sql2 = "UPDATE ".$this->table_name."
	SET position = '".$new_sub_position."'
	WHERE id =  '".$sub['id']."'";
mysql_query($sql2) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql2."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
}
}
// finally update the category position.
$sql3 = "UPDATE ".$this->table_name."
	SET position = '".$new_position."'
	WHERE position	=  '".$position."'";
mysql_query($sql3) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql3."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

$sql = "UPDATE ".$this->table_name."
	SET ";

// lets see what changes should be done and add it to the sql query.
foreach($this->fields as $field => $field_name){
if ($field 	== 'id') continue;		// no change will be done on the id
if ($field 	== 'position' ) continue; // position change have been done in the section above
$sql .= "".$field_name." = '".$$field."',";
}

$sql = substr_replace($sql,"",-1);
$sql .= "WHERE id=".$id."";

mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
}

// ********************************************************
//		Build Categories Array
// ********************************************************

function build_list($id=0,$collapsed="") //return an array with the categories ordered by position
{
$RootPos = "";
$this->c_list = array();

if($id != 0){
$this_category  = $this->fetch($id);
$positions      = explode(">",$this_category['position']);
$RootPos        = $positions[0];
}

// lets fetch the root categories
$sql = "SELECT *
	FROM ".$this->table_name."
	WHERE position	RLIKE '^([0-9]+>){1,1}$' AND c_group	=      '".$this->Group."'
	ORDER BY c_name";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

while($root = mysql_fetch_array($res)){
$root["prefix"] = $this->get_prefix($root['position']);
$this->c_list[$root['id']] = $root;

if($RootPos == $root['id'] AND $id != 0 AND $collapsed != ""){
$this->list_by_id($id);
continue;

}else{

// lets check if there is sub-categories
if($collapsed == "" AND $id==0){
$has_children = $this->has_children($root['position']);
if($has_children == TRUE) $this->get_children($root['position'],0);
}}}
return $this->c_list;
}


// ********************************************************
//		Check if Category has childrens
// ********************************************************

function has_children($position) // return TRUE if that position has sub-categories otherwise returns FALSE
{
$check_sql = "SELECT id FROM ".$this->table_name." WHERE position RLIKE  '^".$position."[0-9]+>$'";
$check_res = mysql_query($check_sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$check_sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
$check = mysql_fetch_array($check_res);
if($check['id'] != "")return TRUE;
else return FALSE;
}

// ********************************************************
//		Get Childrens
// ********************************************************

function get_children($position , $id = 0){

$sql = "SELECT *
	FROM ".$this->table_name."
	WHERE position	RLIKE '^".$position."[0-9]+>$'
	ORDER BY c_name";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

while($child = mysql_fetch_array($res)){
$child["prefix"] = $this->get_prefix($child['position']);

if($id != 0)
{
$this->c_list_by_id[$child['id']] = $child;
$has_children = $this->has_children($child['position']);
if($has_children == TRUE){
$this->get_children($child['position']);
}
continue;

}else{

// lets check if there is sub-categories
$this->c_list[$child['id']] = $child;
$has_children = $this->has_children($child['position']);
if($has_children == TRUE)$this->get_children($child['position']);
}}
}


// ********************************************************
//		Get childs of Specific Category only.
// ********************************************************

function list_by_id($id) //return an array with the categories under the given ID and ordered by name
{
$this_category  = $this->fetch($id);

$positions = explode(">",$this_category['position']);
$pCount = count($positions);
$i = 0;

// lets fetch from top to center
while($i < $pCount){
$pos_id	   = $positions["$i"];
if($pos_id == ""){$i++; continue;}
$list = $this->browse_by_id($pos_id);

foreach($list as $key=>$value){
$this->c_list["$key"] = $value;
$ni = $i + 1;
$nxt_id = $positions[$ni];
if($key == $nxt_id ) break;
} $i++;
}

//center to end
$i = $pCount-1;

while($i >= 0){
$pos_id	 = $positions["$i"];
if($pos_id == ""){$i--; continue;}
$list = $this->browse_by_id($pos_id);

foreach($list as $key=>$value){
$ni = $i - 1;
if($ni < 0) $ni =0;
$nxt_id = $positions[$ni];
if($key == $nxt_id ) break;
$this->c_list["$key"] = $value;
} $i--;
}

}

/***************************************
    Get array of categories under specific category.
****************************************/

function browse_by_id($id) // return array of categories under specific category.
{
$children 		= array();
$this_category  = $this->fetch($id);
$position       = $this_category['position'];

$sql = "SELECT *
	FROM ".$this->table_name."
	WHERE position	RLIKE '^".$position."(([0-9])+\>){1}$'
	ORDER BY c_name";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

while($child = mysql_fetch_array($res)){
$child["prefix"] = $this->get_prefix($child['position']);
$children[$child['id']] = $child;
}
return $children;
}

// ********************************************************
//		Get Position
// ********************************************************

function get_position($id)
{
if($id == 0)return "";
$sql = "SELECT position
	FROM ".$this->table_name."
	WHERE id = '".$id."'";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
$record =  mysql_fetch_array($res);
return $record['position'];
}

// ********************************************************
//		Get Prefix
// ********************************************************

function get_prefix($position)
{
$prefix = "";
$position_slices = explode(">",$position);
$count = count($position_slices) - 1;
for($i=1 ; $i < $count ; $i++){
$prefix .= $this->name_prefix;
}
return $prefix;
}

// ********************************************************
//		Fetch Category Record
// ********************************************************

function fetch ($id)
{
$sql = "SELECT *
	FROM ".$this->table_name."
	WHERE id = '".$id."'";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

$record = mysql_fetch_array($res);
$record["prefix"] = $this->get_prefix($record['position']);
$position_slices  = explode(">",$record['position']);
$key              = count($position_slices)-3;
if($key < 0) $key = 0;
$record["parent"] = $position_slices["$key"];
return $record;
}

// ********************************************************
//		Build HTML output
// ********************************************************

function html_output($id=0)
{
 $tree  = $this->build_list($id,"collapsed"); // we have selected to view category


$output = "";
$output .= $this->HtmlTree['header'];

		if(is_array($tree))
		{
			foreach($tree as $c)
			{

				if($c['id'] == $id) 	$body = $this->HtmlTree['BodySelected'];
				else   						  	 	$body = $this->HtmlTree['BodyUnselected'];

				foreach($this->fields as $name => $field_name)
				{
					$body = str_replace("[$name]" ,$c["$field_name"],$body);


				}
					$body = str_replace("[prefix]",$c['prefix'],$body);

			$output .= $body;
			}
		}

$output .= $this->HtmlTree['footer'];
return $output;
}

// ********************************************************
//                      get sub-categories count at TOP-Level Category.  ( needs top-level category ID as a param)
// ********************************************************

function count_categories($cat_id)
{
$thisPosition = $this->get_position($cat_id);
$sql = "SELECT *
	FROM ".$this->table_name."
	WHERE position LIKE '".$thisPosition."%'";
$res   = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
$count = mysql_num_rows($res);
$count-= 1; // remove the category itself from the count
return $count;
}

// ********************************************************
//                      get items count under TOP-Level Category/sub-categories.  ( needs top-level category ID as a param)
// ********************************************************

function count_items($cat_id)
{
if($this->itemsTable == "" OR $this->CID_FieldName=="") die("<br><storng><u>Class Error:</u></strong><br>Either items Table name Or CID field name is blank!<br><br>");

$count = 0;
$thisPosition = $this->get_position($cat_id);

$sql = "SELECT *
	FROM ".$this->table_name."
	WHERE position LIKE '".$thisPosition."%'";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

while($category = mysql_fetch_array($res)){

$sql2 = "SELECT *
	FROM ".$this->itemsTable."
	WHERE ".$this->CID_FieldName."	= '".$category["id"]."'";
$res2 = mysql_query($sql2) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));
$count+= mysql_num_rows($res2);
}

return $count;
}

} // Class END
?>

Link to comment
Share on other sites

@ Webent

 

Provide me with a sample code that would have a structure like this into an HTML table:

 

 

                    Category1                                  Category2

      checkbox  Subcat1                      checkbox    Subcat1

      checkbox  Subcat2                      checkbox    Subcat2

 

The checkbox should be for the sub categories. With that, i believe i m done

Link to comment
Share on other sites

Just to accomplish that, if I understand what you are asking, you would use this...

 

echo '
<table>
    <tr>
        <td>Category 1</td>
        <td>Category 2</td>
    </tr>
';
$results = mysql_query ("SELECT * FROM table WHERE field = 'parameter'");
while ($row = mysql_fetch_assoc($results)) {
echo '
    <tr>
        <td>Subcat1<input type="checkbox" name="Cat1Subcat1" id="Cat1Subcat1" value="Cat1Subcat1"></td>
        <td>Subcat1<input type="checkbox" name="Cat2Subcat1" id="Cat2Subcat1" value="Cat2Subcat1"></td>
    </tr>
    <tr>
        <td>Subcat2<input type="checkbox" name="Cat1Subcat2" id="Cat1Subcat2" value="Cat1Subcat2"></td>
        <td>Subcat2<input type="checkbox" name="Cat2Subcat2" id="Cat2Subcat2" value="Cat2Subcat2"></td>
    </tr>
';
}
echo '</table>'; 

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.