Friday, 13 November 2015

Php script to create Category & Sub Category

01:32

(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>';


}




?>
===================Display Sub Category using Ajax =============================
(1)write Ajax  code in    "maincatdisp.php"  file:
<html>
<head>
<script>
function ajax_post()
{
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();

    // Create some variables we need to send to our PHP file

    var url = "handle_data.php";

    var fn = document.getElementById("sub_cat").value;

   

    var vars = "sub_cat="+fn;

    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
   if(hr.readyState == 4 && hr.status == 200) {
   var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
   }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<select  name="sub_cat"  id="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"     onclick="ajax_post();">
<div id="status"></div>
</body>

</html>

(2)we use same  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>';


}




?>

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top