Dynamic Dropdown Category Subcategory List in PHP MySQL using ajax — Laratutorials.COM

Category Subcategory Dropdown in PHP + MySQL + Ajax

PHP + MySQL + Ajax category subcategory dropdown; Through this tutorial, i am going to show you how to create dynamic dependent category subcategory dropdown list in in PHP + MySQL + Ajax. And how to display subcategories by category in PHP + MySQL + Ajax.

Dynamic Category Subcategory Dropdown in PHP + MySQL + Ajax

  • Step 1 – Create PHP Project
  • Step 2 – Create Database Table And Connect App to DB
  • Step 3 – HTML Markup For Category & Subcategory Dropdown
  • Step 4 – Get Subcategory List By Category Id in PHP + MySQL

Step 1 – Create PHP Project

In step 1, Navigate to your local web server directory. And inside this directory, create one directory. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Create Database Table And Connect App to DB

In step 2, you need to create database and table. So run the following sql query to create database and table:

CREATE TABLE `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `parent_id` int(11) NOT NULL DEFAULT '0', `category` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `categories` (`id`, `parent_id`, `category`) VALUES (1, 0, 'General'), (2, 0, 'PHP'), (3, 0, 'HTML'), (4, 3, 'Tables'), (5, 2, 'Functions'), (6, 2, 'Variables'), (7, 3, 'Forms');

Then create a php file that named db.php. Which is used to connect phpmyadmin mysql database to project (demo).

So, now create db.php file and add the below given code into your file:

Step 3 – HTML Markup For Category & Subcategory Dropdown

In step 3, create a php file that named index.php. This file will display country state city dropdown list from MySQL database using jQuery Ajax. Now, you need to add the following html Markup code into index.php file:

         html, body < background-color: #fff; color: #636b6f; font-family: 'Nunito', sans-serif; font-weight: 200; height: 100vh; margin: 0; >.full-height < height: 100vh; >.flex-center < align-items: center; display: flex; justify-content: center; >.position-ref < position: relative; >.top-right < position: absolute; right: 10px; top: 18px; >.content < text-align: center; >.title < font-size: 84px; >.links > a < color: #636b6f; padding: 0 25px; font-size: 13px; font-weight: 600; letter-spacing: .1rem; text-decoration: none; text-transform: uppercase; >.m-b-md  
Category
Sub Category

The following ajax code will fetch subcategories list and display in html markup dropdown by category id in PHP + MySQL + Ajax:

  

Step 4 – Get Subcategory List By Category Id in PHP + MySQL

In step 4, create one php file, which name fetch-subcategory-by-category.php. And add the following code into it:

The fetch-subcategory-by-category.php file code will fetch all subcategories list in dropdown form data into MySQL database using PHP + AJAX.

Conclusion

PHP + MySQL + Ajax category subcategory dropdown; Through this tutorial,You have learned how to create dynamic dependent category subcategory dropdown list in in PHP + MySQL + Ajax. And how to display subcategories by category in PHP + MySQL + Ajax.

Источник

How to Create Category and Subcategory in PHP

Hello Developers, I have shared the best tutorial to learn about displaying categories and subcategories on web pages. In Most projects, you need to create a Dynamic category and subcategory in PHP using MySQL. But you don’t know the simple way to implement it. So, Don’t worry, you will get the best & simple script to use in your projects.

As you know that a category is created in another category is called nested or child or subcategory. So, you need to care about its relationship. Every subcategory is always created with the id of the parent category. So, I will write the PHP MySQL Scrip with this concept.

add category and subcategory in php

Dynamic Category and Subcategory in PHP Using MySQL

You have to go through the following steps to understand and create categories and subcategories in PHP and MySQL. Therefore you must not skip any one of the given points.

1. Configure Basic Requirement

First of all, you should have to create the following folder structure for testing purposes. Otherwise, you can directly use the given script in your project.

myproject/ |__catsubcat-form.php |__catsubcat-script.php |__database.php |__style.css

Create a table with the name of categories in PHPMyAdmin.

CREATE TABLE `categories` ( `id` int(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT, `parent_id` int(10) DEFAULT 0, `category_name` varchar(255) DEFAULT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Create another table with the name of subcategories in PHPMyAdmin

Table Name – subcategories

CREATE TABLE `subcategories` ( `id` int(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT, `parent_id` int(10) DEFAULT 0, `subcategory_name` varchar(255) DEFAULT NULL, )

Write the script to connect PHP to MYSQL Database.

2. Create a category and subcategory Form

  • Write the HTML code to create the category form.
  • Now, Write the HTML code to create a subcategory form.
  • You have to write the HTML code to create a category and subcategory list to open those forms.

File Name – catsubcat-form.php

3. Create Category and Subcategory in PHP

To create category and subcategory, you have to use the following functions –

  • create_category($conn) can insert category data into the database
  • create_subcategory($conn) can insert subcategory data into the database
  • fetch_categories($conn) can fetch category data from the database
  • fetch_subcategories($conn,$parent_id) can fetch subcategory data from the database
  • legal_input($value) can validate category and subcategory data before inserting it into the database

File Name – catsubcat-script.php

 if(isset($_POST['addsubcat'])) < $msg=create_subcategory($conn); >function create_category($conn)< $category_name= legal_input($_POST['category_name']); $query=$conn->prepare("INSERT INTO categories (category_name) VALUES (?)"); $query->bind_param('s',$category_name); $exec= $query->execute(); if($exec)< $msg=" Category was created successfully"; return $msg; >else< $msg= "Error: " . $query . "
" . mysqli_error($conn); > > function create_subcategory($conn)< $parent_id= legal_input($_POST['parent_id']); $subcategory_name= legal_input($_POST['subcategory_name']); $query=$conn->prepare("INSERT INTO subcategories (parent_id,subcategory_name) VALUES (. )"); $query->bind_param('is',$parent_id,$subcategory_name); $exec= $query->execute(); if($exec)< $msg="Subcategory was created sucessfully"; return $msg; >else< $msg= "Error: " . $query . "
" . mysqli_error($conn); > > // fetch query $catData=fetch_categories($conn); function fetch_categories($conn)< $parent_id=0; $query = $conn->prepare('SELECT * FROM categories WHERE parent_id=?'); $query->bind_param('i',$parent_id); $query->execute(); $exec=$query->get_result(); $catData=[]; if($exec->num_rows>0)< while($row= $exec->fetch_assoc()) < $catData[]=[ 'id'=>$row['id'], 'parent_id'=>$row['parent_id'], 'category_name'=>$row['category_name'], 'subcategories'=>fetch_subcategories($conn,$row['id']) ]; > return $catData; >else < return $catData=[]; >> // fetch query function fetch_subcategories($conn,$parent_id)< $query = $conn->prepare('SELECT * FROM subcategories WHERE parent_id=?'); $query->bind_param('i',$parent_id); $query->execute(); $exec=$query->get_result(); $subcatData=[]; if($exec->num_rows>0)< while($row= $exec->fetch_assoc()) < $subcatData[]=[ 'id'=>$row['id'], 'parent_id'=>$row['parent_id'], 'subcategory_name'=>$row['subcategory_name'], ]; > return $subcatData; >else < return $subcatData=[]; >> // convert illegal input to legal input function legal_input($value) < $value = trim($value); $value = stripslashes($value); $value = htmlspecialchars($value); return $value; >?>

Don’t forget to include the database connection file database.php in the above file.

This PHP script can work to create any kind of category and subcategory view. You need not change anything into its script. But you can change the table name based on your project.

4. Display category and subcategory

Now, we have to display category and subcategory data in the HTML list. So you will have to follow the below points –

  • First of all, include script file category-script.php
  • Create an HTML unordered list
  • Print category and subcategory by running $catData with a foreach loop

File Name – catsubcat-list.php

My Suggestion

I have shared an example to create a category and subcategory in PHP. Now, you can easily create more than two-level categories with the same concept. If you have any questions, ask me through the below box.

You can learn more PHP Coding in the blog. I will share more web technology coding tutorials in the best and simple way. So, continue to visit my website.

Hey there, Welcome to CodingStatus. My Name is Md Nurullah from Bihar, India. I’m a Software Engineer. I have been working in the Web Technology field for 4 years. Here, I blog about Web Development & Designing. Even I help developers to build the best Web Applications.

Источник

Category SubCategory Dropdown in PHP MySQL with JQuery AJAX

In this blog, I will learn you how to dynamic populate category and subcategory in the dropdown in PHP MySQL using Ajax. This turorial will give you category subcategory dropdown in php mysql ajax.

This tutorial will help you step by step on how to implement a dynamic category and subcategory dropdown list onchange in PHP MySQL using Ajax or populate the second dropdown based on the first PHP. As well as learn, how to fetch data from the database in the dropdown list in PHP using jQuery ajax.

Here i will give you full example for how to dynamic populate category and subcategory in the dropdown in PHP MySQL using Ajax. So let’s see the bellow example step by step.

Step 1 : Create Table

In the first step, You can open your database and run the bellow sql query to create category table into the database.

CREATE TABLE `categories` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`parent_id` int(11) NOT NULL DEFAULT ‘0’,

`category` varchar(255) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2 : Insert some record

In this step you will insert some record in categories table run the following sql query.

INSERT INTO `categories` (`id`, `parent_id`, `category`) VALUES

(1, 0, ‘PHP’),

(2, 0, ‘HTML’),

(3, 1, ‘Laravel’),

(4, 1, ‘WordPress’),

(5, 1, ‘Codeigniter’);

Step 3 : Create DB Connection File

In this step you can create db connection file as config.php and fill the bellow database information.

$servername = ‘localhost’;

$username = ‘root’; // Username

$password = »; // Password

$dbname = «db_name»;

$conn = mysqli_connect($servername,$username,$password,»$dbname»);

if(!$conn) <

die(‘Could not Connect MySql Server:’ .mysql_error());

>

?>

Step 4 : Create Category Dropdown Form

Here you will create two dropdown one is category and second one is sub category create index.php file put the bellow code.

include «config.php»;

?>

Category Subcategory Dropdown in PHP MySQL Ajax — NiceSnippets.com

Category

Select Category

$result = mysqli_query($conn,»SELECT * FROM categories where sub_cat_id = 0″);

while($row = mysqli_fetch_array($result)) <

?>

type: "POST",

data: <

category_id: category_id

>,

cache: false,

success: function(result) <

$("#sub-category-dropdown").html(result);

>

>);

>);

>);

Step 5 : Fetch Sub Category

In this step we have to fetch sub category and create get-subcat.php file and put the bellow code.

get-subcat.php

include "config.php";

$category_id = $_POST["category_id"];

$result = mysqli_query($conn,"SELECT * FROM categories where sub_cat_id = $category_id");

?>

while($row = mysqli_fetch_array($result)) <

?>

>

?>

✌️ Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.

You might also like.

Источник

Читайте также:  Php with debug true
Оцените статью