Hello friends
Iam unabing to add subcategory
and iam trying to add in the same table in which the categories are there can anyone please help me in adding subcategories and categories in a same table please

Thank u
KNR

Recommended Answers

All 5 Replies

please post your code, so we can see what you are doing

Thanks for the reply sir
can u please check the code once and see please

<?php
//ob_start();
//include_once('login_check.php');
include_once('functions.php');
if($_SERVER['REQUEST_METHOD']=='POST')
{
	if(!isset($_GET['fid']))
	  {
		$qry1="SELECT * FROM categories  WHERE catname='".$_POST['cname']."'";
		$res1=mysql_query($qry1) or die(mysql_error());
		$num1=mysql_num_rows($res1);
		if($num1!=0)
			{$msg1="This Category with this name already exists ";			}
			if($num1==0)
			/*{
			if($HTTP_POST_FILES["cimage"]["size"] >1) 
			{		
		$ext = substr($HTTP_POST_FILES['cimage']['name'],strpos($HTTP_POST_FILES['cimage']['name'],'.')+1);
			$ext_arr=array("jpeg","ping","jpg","gif","bmp");
			//if (in_array($ext,$ext_arr)) 
			{ 		
				$prlimage=substr($HTTP_POST_FILES['cimage']['name'],0,strpos($HTTP_POST_FILES['cimage']['name'],'.'));
				$prlimage.=time();
				$prlimage.=strstr($HTTP_POST_FILES['cimage']['name'],'.'); 	
				$prlimage="../uploadedimages/categories/".$prlimage;			
				if(!move_uploaded_file($HTTP_POST_FILES['cimage']['tmp_name'],$prlimage)) { $prlimage=""; }
				chmod($prlimage,0777); 				
			} else 	 
			{		
				$prlimage="";
			}*/
	 $qry2="INSERT INTO categories (catname,cimg,parentid,status,added_date) VALUES ('".$_POST['cname']."','".$prlimage."','".$_POST['cat_id']."','1',now())";
			 $res2=mysql_query($qry2) or die(mysql_error());
			 $ic_id=mysql_insert_id();
			 header("location:addsubcat.php?msg=1");
			}
					  }
	if(isset($_GET['fid']))
		{
		
		$qry1="SELECT * FROM categories WHERE catname='".$_POST['cname']."' AND id!='".$_GET['fid']."'";
		$res1=mysql_query($qry1) or die(mysql_error());
		$num1=mysql_num_rows($res1);
			//if($num1==0)
			/*{
			if($HTTP_POST_FILES["cimage"]["size"] >1) 
			{
		
		$ext = substr($HTTP_POST_FILES['cimage']['name'],strpos($HTTP_POST_FILES['cimage']['name'],'.')+1);
			$ext_arr=array("jpeg","ping","jpg","gif","bmp");
			//if (in_array($ext,$ext_arr)) { 			
				$prlimage=substr($HTTP_POST_FILES['cimage']['name'],0,strpos($HTTP_POST_FILES['cimage']['name'],'.'));
				$prlimage.=time();
				$prlimage.=strstr($HTTP_POST_FILES['cimage']['name'],'.'); 	
				$prlimage="../uploadedimages/categories/".$prlimage;			
				if(!move_uploaded_file($HTTP_POST_FILES['cimage']['tmp_name'],$prlimage)) { $prlimage=""; }
				chmod($prlimage,0777); 				
			} else {		
				$prlimage=$_POST['oldimage'];
			}}*/
			
		 $qry2="UPDATE `categories` SET catname='".$_POST['cname']."',cimg='".$prlimage."',parentid='".$_POST['cat_id']."' WHERE id='".$_GET['fid']."'";
			 $res2=mysql_query($qry2) or die(mysql_error());
			 echo '<script language="javascript">window.location.href="addsubcat.php?msg=2"</script>';			
			  //header("location:addsubcat.php?msg=2");
			//}
}

?>
<?php
if(!empty($_GET['fid']))
{
$qry3="SELECT * FROM categories WHERE parentid!=0 and id='".$_GET['fid']."'";
$res3=mysql_query($qry3) or die(mysql_error());
$row3=mysql_fetch_array($res3);
$show=" Edit ";
}
else
{
$show =" Add ";
}
if(!empty($_GET['start'])){ $start=$_GET['start']; } 
else{ $start=0; }
$len=10;
if(!empty($_GET['start']))
{
$link="addsubcat.php?a=a&start=".$_GET['start']."";
}else{
$link="addsubcat.php?a=a";
}

if(!empty($_GET['cn']))
{ 
	if($_GET['cn']==1){ $con=1; $m=4; } 
	else { $con=0; $m=5;}
	mysql_query(" update categories  set status='".$con."' where id='".$_GET['id']."' ");
	header("location:addsubcat.php?msg=".$m."");
}
if(!empty($_GET['delid'])){  
	$t="delete from categories WHERE id='".$_GET['delid']."'";
	mysql_query($t);
	mysql_query("delete from categories where id='".$_GET['cat_id']."'");
	if(!empty($_GET['start']))
    {
	header("location:addsubcat.php?msg=del&start=".$_GET['start']."");
	}else{
	header("location:addsubcat.php?msg=del");
	}
}

$fres=mysql_query("SELECT * FROM categories  where parentid!=0 ORDER BY id");
$fcunt=mysql_num_rows($fres);
$fres=mysql_query("SELECT * FROM categories where parentid!=0  ORDER BY id DESC LIMIT $start,$len");

$cat_arr=array();
$ar_res=mysql_query("select * from categories order by id");  
while($ar_rw=mysql_fetch_assoc($ar_res)) {
	$cat_arr[$ar_rw['catname']]=$ar_rw['id'];
}



if(!empty($_GET['fid']))
{
$qry3="SELECT * FROM categories WHERE id='".$_GET['fid']."'";
$res3=mysql_query($qry3) or die(mysql_error());
$num3=mysql_num_rows($res3);
	if($num3>0)
	{
	$catid=mysql_result($res3,0,'parentid');
		
	}
	}
?><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>..</title>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<meta name="Powered by BitraNet Pvt.Ltd.," CONTENT="http://www.bitranet.com">
<link rel="stylesheet" type="text/css" href="../site.css">
<script language="javascript">
function chkaddsubcat()
{
a=document.formaddsubcat;
if(a.cat_id.value=='')
{
	alert ("Please Select Category");
	a.cat_id.focus();
	return false;
}
if(a.c_name.value=='')
{
	alert ("Please Select SubCategory");
	a.c_name.focus();
	return false;
}
 if (a.cimage.value==""){    alert("Please Upload Logo");	a.cimage.focus();	return false;} 
 var ftypes=['gif','jpeg','jpg','GIF','JPEG','JPG'];	
    filename =a.cimage.value;
	dots 	= filename.split(".");
	ftype 	= dots[dots.length-1];
	if((ftype =="")||(ftype =="gif")||( ftype == "jpeg" )||( ftype == "jpg" )||( ftype == "GIF" )||( ftype == "JPEG" )||( ftype == "JPG" ))
	  { }
	else
	  {
	alert("Only upload images that end with these file types \n\n" + (ftypes));
	a.cimage.focus();
	return false;  
	  }
}
</script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style></head>
<body >
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td height="50" colspan="3" align="right"></td>
  </tr>
  <tr>
    <td width="17%" valign="top"><? include('left.php');?></td>
    <td width="1" bgcolor="#6B6B6B" ><img src="../images/spacer.gif" width="1" height="1"></td>
    <td width="83%" valign="top" ><table width="100%" border="0" cellspacing="0" cellpadding="1">
      <tr>
        <td colspan="2"><table width="100%" border="0" cellpadding="1" cellspacing="0">
            <tr>
              <td bgcolor="#8B8B8B"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td height="20" bgcolor="#E2E2E2" class="styleforallbold">&nbsp;
                        <?=$show ?>
                      Sub Category</td>
                  </tr>
              </table></td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td height="30" colspan="2" align="center" valign="middle" class="red"></td>
		</tr>
      <tr>
        <td colspan="2" valign="top"><form name="formaddsubcat"  enctype="multipart/form-data" action="" method="post"   onSubmit="return chkaddsubcat();">
          <table width="80%" border="0" align="center" cellpadding="1" cellspacing="0" bgcolor="#FFFFFF">
            <tr>
              <td bgcolor="#8B8B8B"><table width="100%"  border="0" align="center" cellpadding="6" cellspacing="0" bgcolor="#FFFFFF" >
                  <tr >
                    <td colspan="3" align="right" nowrap><span class="alerts">* fields required</span></td>
                  </tr>
                  <tr >
                    <td colspan="3" align="center" nowrap class="alerts"><? if(isset($msg1)) { echo $msg1;}?></td>
                  </tr>
                  
                  
                  <tr align="center" >
                    <td align="right" valign="top" class="style18"><strong class="style20"><span class="star">* </span>Category </strong></td>
                    <td valign="top" class="styleforallbold"><strong>:</strong></td>
					<td align="left"><label>
                    <select name="cat_id" id="cat_id" class="box2" >
                      <option value="" >-Select Category-</option>
                       <?   
						/*if(isset($_POST['cat_id'])){ $catid=$_POST['cat_id'];} 
						else if(!empty($_GET['fid'])) $catid=$catid; else $catid='';
						getcategories($catid);*/
						if(isset($_POST['cat_id'])) { getmaincat($_POST['cat_id']); }
				  	  else
					 { getmaincat();}
				  ?>
					
                    </select>
                    </label></td>
                  </tr>
                  <tr align="center" >
                    <td width="26%" align="right" valign="top" class="style18"><span class="h4"><strong class="star">*</strong></span><strong class="style20"><span class="red">Sub </span>Category </strong></td>
                    <td width="3%" valign="top" class="styleforallbold"><strong>:</strong></td>
                    <td width="71%" align="left"><label>
                      <input type="text" name="cname" id="cname" class="box2" <? if(!empty($_GET['fid'])){?>value="<?=$row3['cname']?>"<? } ?> >
                    </label></td>
                  </tr>
                  <tr align="center" >
                    <td align="right" valign="top" class="style18"><span class="h4"><strong class="star">*</strong></span><strong>Upload Logo</strong></td>
                    <td valign="top" class="styleforallbold"><strong>:</strong></td>
                    <td align="left"><label>
                    <input type="file" name="cimage" id="cimage" class="box2" />
                    <br>
                    <br>
                    <? if(isset($_GET['fid'])){?>
                    <a href="javascript:;" class="mlinks" onClick="window.open('view_cimage.php?vid=<?=$_GET['fid'];?>','','scrollbars=yes,width=500,height=500')" > View </a>
                    <? } ?>
                    <input type="hidden" name="oldimage" value="<?=$row3['cimg'];?>">
                    </label></td>
                  </tr>
                  
                  <tr align="center" >
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td align="left"><input type="submit" name="Submit" value="Submit"></td>
                  </tr>
                  <tr >
                    <td colspan="3">&nbsp;</td>
                  </tr>
              </table></td>
            </tr>
          </table>
        </form></td>
      </tr>
      <tr>
        <td colspan="2" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="1">
          <tr>
            <td colspan="2"><table width="100%" border="0" cellpadding="1" cellspacing="0">
                <tr>
                  <td bgcolor="#8B8B8B"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td height="20" bgcolor="#E2E2E2"  class="styleforallbold">&nbsp;&nbsp;Manage Sub Categories </td>
                      </tr>
                  </table></td>
                </tr>
            </table></td>
          </tr>
          <tr>
            <td height="30" colspan="2" align="center" valign="middle">
			<span class="alerts">
			<? 		  
		if(!empty($_GET['msg']))
		{
		 switch($_GET['msg'])
		  {
		   		   
           case 1: echo "Category Added successfully.";
		   break; 
		   case 2: echo "Category Edited successfully.";
		   break; 
		   case 3: echo "Category Deleted successfully.";
		   break; 
		   case 4: echo "Category Activated ";
		   break;
		   case 5: echo "Category In-activated ";
		   break;
		   
		   default: echo "";
		   }   		  
		  
		 }
		?>      </span>      </td>
          </tr>
            <script language="javascript">
	function del(x)
	{
				if(confirm("Deletion of this record will result to delete all products based on this category!!!\n \t\t\t Are you sure you want to continue..."))
					{
					window.location="addsubcat.php?delid="+x;
					}
			return;
	}
        </script>
            <tr>
              <td colspan="2" valign="top"><table width="80%" border="0" align="center" cellpadding="1" cellspacing="0" bgcolor="#FFFFFF">
                  <tr>
                    <td bgcolor="#8B8B8B"><table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#FFFFFF">
                        <tr bgcolor="#8B8B8B">
                          <td  width="25%"class="style18"><strong class="whitebold">Sub Category</strong></td>
                          <td width="17%" class="style18"><strong class="whitebold">Category</strong></td>
                          <td width="15%"align="center"  class="style18"><strong class="whitebold">Status</strong></td>
                          <td width="8%" align="center" class="style18"><strong class="whitebold">Edit </strong></td>
                          <td width="19%" align="center" class="style18"><strong class="whitebold">View</strong></td>
                          <td   width="2%"align="center" class="style18"><strong class="whitebold">Delete</strong></td>
                        </tr>
                        <?
				  if($fcunt>0){
					while($frow=mysql_fetch_assoc($fres)){ 
						$cat_res=mysql_query("SELECT catname,id FROM categories WHERE id =".$frow['parentid']);
						$cat_row=mysql_fetch_array($cat_res,MYSQL_BOTH);
						if($frow["status"]==1){$stat="Active";$up="inact";$colo="green"; }
						else {$stat="In-Active";$up="act";$colo="Red";}
		        ?>
                        <tr >
						  <td  class="style20"><?=$frow['catname'];?>                          </td>
                          <td  class="style20"><?=array_search($frow['parentid'],$cat_arr);?></td>
                          <? // its for   active/incative
if($frow['status']==0)
{
$fs="In-Active";
$sf=1;
}
else
{
$fs="Active";
$sf=2;
}
?>
                          <td align="center" class="blinks" ><a href="addsubcat.php?act=1&id=<? echo $frow['id']?>&cn=<? echo $sf?>"  class="slinks"><? echo $fs?></a> </td>
                          <td height="22"  align="center" ><a href="addsubcat.php?id=<? echo $frow['id']?>" class="slinks">Edit</a> </td>
						  <td  align="center" ><a href="javascript:;" class="slinks" onClick="window.open('viewsubcat.php?fid=<?=$frow['id'];?>','no','scrollbars=yes,width=550,height=400')" >View</a> </td>
						  <td width="31%" height="22" align="center" ><a href="javascript:del('<? echo $frow['id'];?>')" class="slinks">Delete</a> </td>
                        </tr>
                        <?
					}
					}
				 ?>
                        <tr>
                          <td height="22" colspan="6" align="center" class="brwnbold" ><? if($fcunt>0){ siri_drawNavigation($start,$fcunt,$link);	} else { echo "<span class='brwnbold'>".'No Records Found'."</span>";}?></td>
                        </tr>
                    </table></td>
                  </tr>
              </table></td>
            </tr>
          <tr>
            <td colspan="2" valign="top">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td colspan="3"><img src="../images/spacer.gif" width="1" height="1"></td>
  </tr>
  <tr>
    <td height="1" colspan="3" align="right" bgcolor="#737373"><img src="../images/spacer.gif" width="1" height="1"></td>
  </tr>
  <tr>
    <td  colspan="3" align="center" ></td>
  </tr>
</table>
</body>
</html>

and 1 more thing sir i want the category and subcategories have to be saved in the same table

please sir

ok I'll have to take a look at this later, but it's possible that you might be able to fix this by making a few small changes to your script. for example I suggest that you use <?php for all of your opening tags for php. one more thing is that some of your code is not wrapped in the php tags correctly. if you load this page into a sever will see what I mean. some of the code displays on the page.

also I noticed that your <label> tags are not correct they are used to label an input field like this

<label for="email">Email:</label>
<input name="email" id="email" type="text" class="formbox" />

the input tag is not placed in between the <label> tag

I will take another look at it when I get back unless someone else does it first. I'll be back on the 26'th of December.

Please use CODE tags. And could you post the relevant code so we don't have to wade through all of that? Try posting just where it is getting or defining the categories/subcategories and where it is adding things to a table (in a database, I assume). But without looking at code (sorry, I'm lazy) I think you could add a field for a parent category. It would refer to the id (an id field is also needed, if you haven't already made one) of another row in the table. It could be -1 if it is a top-level catergory, to distinguish from a subcategory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.