Thursday, 12 March 2015

Php script to create Category & Sub Category & Display Sub Category according to Main Category list

(1)create table cat1:

create table cat1

(

c_id int primary key auto_increment,

 c_name  varchar(200),

 p_id     varchar(200)


);


(2) write code for "connect.php" :


<?php


$con=mysql_connect("localhost","root","");

mysql_select_db("cat",$con) or die(mysql_error());

if(!$con)

{

echo "Not connected";


}




?>


Code for adding Main Category:

(3)write   "main.html"   code:


<form    action="add.php"       method="post">

ADD main Category<input type=text name="name">

<input type="submit"    name="submit"     value="submit">


</form>

(4)write code for "add.php" :


<?php



include("connect.php");



$c_name=$_POST['name'];



$p_id='0';


$sql="insert into cat1(c_name,p_id)values('$c_name','$p_id')";



mysql_query($sql) or die(mysql_error());


echo "added successfully";



?>


Now below code for adding subcategory:


(5)write code for "disp.php" :


<form   action="subadd.php"      method="post">


<select name="p_id"  >

<?php


include("connect.php");



$sql="select * from  cat1 where  p_id='0'" ;



$result=mysql_query($sql);



while($row=mysql_fetch_array($result))

{


echo '<option   value="'.$row['c_id'].'">';


echo $row['c_name'];

echo '</option>';



}






?>

</select>

sub category name <input type=text name="name">

<input type="submit"  name="submit"   vlaue="add-sub" />


</form>


(6)now write code for "subadd.php"  file:


<?php

include("connect.php");

$c_name=$_POST['name'];

$p_id=$_POST['p_id'];

$sql="insert into cat1(c_name,p_id)values('$c_name','$p_id')";


mysql_query($sql) or die(mysql_error($sql));


?>


Now code for Display  subcategory on basis of Main category :


(7)write code for  "maincatdisp.php"  file:


<html>



<body>

<form name="myform" action="handle-data.php"  method="post">

<select  name="sub_cat">

<?php


include("connect.php");




$sql="select *   from    cat1  where     p_id='0'" ;




$result=mysql_query($sql);




while($row=mysql_fetch_array($result))

{



echo '<option  value="'.$row['c_id'].'">';

echo $row['c_name'];

echo '</option>';








}



?>

</select>

<input type="submit" name="submit"  value="disp_subcat">

</form>


</body>


</html>


(8)now write code for "handle_data.php file":



<?php


$id=$_POST['sub_cat'];




include("connect.php");


$sql="select c_name  from cat1 where   p_id='$id'";


$result=mysql_query($sql);


echo "sub category";


echo '<br></br>';


while($row=mysql_fetch_array($result))

{


echo $row['c_name'];

echo '<br></br>';


}




?>




No comments:

Post a Comment