Location: PHPKode > scripts > Create Your Own Directory > cms/admin/addcat.php
<?php
include "authheader.php";
if($block != true)
{
?>
<!-- Welcome to the scripts database of HIOX INDIA      -->
<!-- This tool is developed and a copyright             -->
<!-- product of HIOX INDIA.				        -->
<!-- For more information visit http://www.hscripts.com -->
<html>
<head>
</head>
<?php include "heade.php"; ?>

<body style="margin: 0px;">
<table width=790 height=100% border=0 cellpadding=0 cellspacing=0 align=center>
<tr><td align=center>

<tr height=100%><td class=maintext valign=top>
<html>
<br>
<table align=center border=0 bgcolor=#f8f8ff  width=80%>
<form name=fun method=POST action='addcat.php'>
<SCRIPT LANGUAGE='JavaScript'>
function addvalue()
{
document.fun.hidval.value="insert";
}
function editvalue()
{
document.fun.hidval.value="edit";
}
function updatevalue()
{
document.fun.sub.value="update";
}
function deletevalue()
{
document.fun.sub.value="delete";
}

function hide()
{
document.fun.select1.style.visibility="hidden";
}
</script>
<?php
$hidval=$_POST['hidval'];
$addparent=$_POST['addparent'];
$update=$_POST['update'];
$name=$_POST['catname'];
$des=$_POST['desc'];
$select1=$_POST['select1'];
$select2=$_POST['select2'];
$select3=$_POST['select3'];
$select4=$_POST['select4'];
$frm='true';
$sel="";
if($select1==0)
{
  $edit=0;
}
else if($select2 ==0)
{
	$edit=$select1;
	$parentid=$select1;
}else if($select3==0)
{
	$edit=$select2;
	$parentid=$select2;
}else if($select4==0)
{
  	$edit=$select3;
	$parentid=$select3;
}else if($select4 !=0)
{
  	$edit=$select4;
	$parentid=$select3;
}
if($hidval=='insert')
{
if($name != "" && $des !="" )
{
  if($addparent == 'parent')
  {
     $parentid=0;
     $query="insert into type values ('','$name',$parentid,'$des')";
    }else{
     $query="insert into type values ('','$name',$parentid,'$des')";
  }
  $result1=mysql_query($query,$link);
  if($result1)
  {
      $frm='false';
      echo"<div align=center>New category is inserted </div>";
  }else
  {
      echo"<div align=center style=\"color:red\";>Select atleast one category</div>";
  }
}else
{
   echo"<div align=center style=\"color:red\";>Enter proper values for category name and description </div>";
}
}
if($hidval=='edit')
{
$frm='false';
$query="select id,name,des from type where id=$edit"; 
    //$query="update type set name='$name' , des='$des' where id=$edit";
 $result1=mysql_query($query,$link);
 if($result1)
 {
while($line = @mysql_fetch_array($result1, MYSQL_ASSOC))
 {
	$id=$line['id'];
	$des=$line['des'];
	$name=$line['name'];
 }	
 echo"<tr valign=top><td  style='padding-left:20px;' valign=top><strong>Id</strong></td>
	<td><input type=text name='id' value='$id' readonly></td></tr>
	<tr><td  style='padding-left:20px;' ><strong>Category Name</strong></td>
	<td><input type=text name='catname' value='$name'></td></tr>
	<tr><td  style='padding-left:20px;' ><strong> Description</strong></td>
	<td><input type=text name='desc' value='$des'>
	<tr><td colspan=2><div align=center>
	<input type=submit value=Edit  onclick=\"updatevalue()\">
	<input type=submit value=Delete  onclick=\"deletevalue()\"></div>
	<input type=hidden name=sub>
	</td></tr>";
 }else
 {
      echo"<div align=center style=\"color:red\";>Select atleast one category</div>";
 }
}
?>
<?php
$name=$_POST['catname'];
$des=$_POST['desc'];
$id=$_POST['id'];
$sub=$_POST['sub'];

if($sub=='update')
{
$frm='false';
$query="update type set name='$name',des='$des' where id=$id";
$result1=mysql_query($query,$link);
  if($result1)
  {
	echo"<div align=center>Modified values is updated into the database</div>";
  }
}
if($sub=='delete')
{
$frm='false';
$present1="no";
$query="select name from type  where parentid=$id";
//echo"$query";
$result1=mysql_query($query,$link);
while($line = @mysql_fetch_array($result1, MYSQL_ASSOC))
{
$present1=$line['name'];
}
if($present1=='no')
{
$query2="select * from content where tid=$id";
$result2=mysql_query($query2,$link);
while($line2 = @mysql_fetch_array($result2, MYSQL_ASSOC))
{
$present1=$line2['title'];
}
}

  if($present1!='no')
  {
  echo"<div align=center>Content is available for the category to be deleted so, this category cannot be deleted before deleting the content</div>";
  }else if($present1=='no') 
  {

  $query="delete from type where id=$id";
  $result1=mysql_query($query,$link);
    if($result1)
    {
	echo"<div align=center>Category without content is deleted</div>";
  
    }
  }
}

?>
<?php
	echo"<SCRIPT LANGUAGE='JavaScript'>";
	echo"var cat = new Array();";
	$query="select * from type";
	$result=mysql_query($query);
	$j=0;
	while($line = @mysql_fetch_array($result, MYSQL_ASSOC))
	{
	$id=$line['id'];
	$parentid=$line['parentid'];
	$name=$line['name'];
	echo"cat[$j] = new Array($id, $parentid, '$name');";
	$j=$j+1;
	}
	echo"</script>";
?>
<SCRIPT LANGUAGE='JavaScript'>
var check = true;
var val ="<?php echo($tid);?>";
var arr = new Array;
var jj=0;
while(check)
{
    for(var ff=0; ff<cat.length; ff++)
	{
      	if(cat[ff][0]==val)
		{
			val = cat[ff][1];
			tthis = cat[ff][2];
			arr[jj]=cat[ff][0];
			jj=jj+1;
			//alert("THis -"+tthis+"-- Parent-- "+val);
		}
	}
    if(val == 0)
    check = false;
}
function second(now,next)
{	
	var tt=0;
      var from = eval("document.fun.select"+now);
	var dd = from.selectedIndex;
	var res = from[dd].value;
      var ss = eval("document.fun.select"+(next));
	var ss1 = eval("document.fun.select"+(next+1));
	var ss2 = eval("document.fun.select"+(next+2));
	if(now==1)
	{
	ss1.style.visibility="hidden";
	ss2.style.visibility="hidden";
 	}
	for(var ff=0; ff<cat.length; ff++)
	{
      	if(cat[ff][1]==res)
		{
			ss.style.visibility="visible";
			var tt=1;
		}
	}
	if(tt==0)
	{
	    ss.style.visibility="hidden";
	}
	ss.options.length = 0;
	var cc = gg = 0;	
	for(var ff=0; ff<cat.length; ff++)
	{
	    var val = cat[ff];
	    if(val[1] == res)
		{
		  var tobeadded = val[2];
		  var tobeadded2 = val[0];
		  ss.options[gg] = new Option(tobeadded,tobeadded2);
	        gg = gg+1;
     		}
	}
document.fun.addparent.style.visibility="hidden";
}
</script>
<?php
if($frm=='true')
{
echo"<tr><td width=25%  style='padding-left:20px;' ><strong>Parent</strong> </td>
	<td><input type=checkbox name='addparent' value='parent' onclick=\"hide()\">Select to add parent category alone</td></tr>
	<tr><td  style='padding-left:20px;' ><strong>Category<strong></td>
	<td><select name=select1 size=10 onclick=\"second(1,2)\">
	<script language=javascript>
	for(var d=0; d<cat.length; d++)
	{
		if(cat[d][1] == 0)
		document.write(\"<option value=\"+cat[d][0]+\">\"+cat[d][2]+\"</option>\");
	}
	</script>
	</select>
  	<select style='visibility: hidden;' name=select2 size=10 onclick=\"second(2,3)\">
	</select>
   	<select style='visibility: hidden;' name=select3 size=10 onclick=\"second(3,4)\">
	</select>
	<select style='visibility: hidden;' name=select4 size=10 onclick=\"second(4,5)\">
	</select>
	<select style='visibility: hidden;' name=select5 size=10 onclick=\"second(5,6)\">
	</select>
	</td></tr>
	<tr><td height=20 colspan=2></td></tr>
	<tr><td  style='padding-left:20px;' ><strong>Enter Category Name</strong></td>
	<td><input type=text name='catname'></td></tr>
	<tr><td  style='padding-left:20px;' ><strong>Enter Description</strong></td>
	<td><input type=text name='desc' >
	<input type=hidden name=hidval >
	</td></tr>
	<tr><td colspan=2>
		<div align=center><input type=submit name=update value='Add Category' onclick=\"addvalue()\">
		<input type=submit name=Edit value='Edit Category' onclick=\"editvalue()\"></input></div>
	</td></tr>
	<tr><td height=20 colspan=2></td></tr>
</form>";
}


 include "../genscript.php"; 

?>
</table>
<script language=javascript>
var coun=1;
for(i=arr.length-1;i>=0;i--)
{
	var xx = eval("document.fun.select"+coun);
	var dd = xx.options.length;
	for(var fg=0; fg<dd; fg++){
		if(xx[fg].value == arr[i])
			xx[fg].selected = true;
	}
	second(coun,(coun+1));
	coun=coun+1;
}
</script>
</td></tr>
</table>
<tr><td height=20 bgcolor=white>
</td></tr></table>

</body>
</html>
<!-- Welcome to the scripts database of HIOX INDIA      -->
<!-- This tool is developed and a copyright             -->
<!-- product of HIOX INDIA.				        -->
<!-- For more information visit http://www.hscripts.com -->
<?php
}
?>



Return current item: Create Your Own Directory