<?php
if(isset($_POST['add']))
{
$category_id = $_POST['category_id'];
$name = mysqli_real_escape_string($cn,$_POST['name']);
$sort_order = $_POST['sort_order'];
$status = $_POST['status'];
$slug_url = mysqli_real_escape_string($cn,$_POST['slug_url']);
$sqlUrl = mysqli_query($cn, "select * from tbl_subcategory where slug_url='$slug_url'");
if(mysqli_num_rows($sqlUrl)>0)
{
echo '<div class="alert alert-danger"><strong>Danger!</strong> Slug Url Already Exist.</div>';
}
else
{
$sql = mysqli_query($cn, "select * from `tbl_subcategory` where `name`='$name'");
if(mysqli_num_rows($sql)>0)
{
echo '<div class="alert alert-danger"><strong>Danger!</strong> Subcategory name already exists.</div>';
}
else
{
$sqlSubject=mysqli_query ($cn, "insert into `tbl_subcategory` set `category_id`='$category_id', `name`='$name', `sort_order`='$sort_order', `status`='$status', `slug_url`='$slug_url'");
if ($sqlSubject)
{
echo '<div class="alert alert-success"><strong> Success!</strong> Subcategory Added Successfully.</div>';
}
else
{
echo '<div class="alert alert-danger"><strong> Danger!</strong> Subcategory Not Added. Try Again.</div>';
}
}
}
}
if(isset($_POST['edit']))
{
$id = $_POST['id'];
$category_id = $_POST['category_id'];
$name = $_POST['name'];
$sort_order = $_POST['sort_order'];
$status = $_POST['status'];
$slug_url = mysqli_real_escape_string($cn,$_POST['slug_url']);
$sql = mysqli_query($cn, "select * from tbl_subcategory where id='$id'");
$sqlData = mysqli_fetch_array($sql);
$sqlUrl = mysqli_query($cn, "select * from tbl_subcategory where slug_url='$slug_url' and id!='$id'");
if(mysqli_num_rows($sqlUrl)>0)
{
echo '<div class="alert alert-danger"><strong>Danger!</strong> Slug Url Already Exist.</div>';
}
else
{
$sqlSubject = mysqli_query ($cn,"update `tbl_subcategory` SET `category_id`='$category_id', `name`='$name', `sort_order`='$sort_order', `status`='$status', `slug_url`='$slug_url' where `id`='$id'");
if($sqlSubject)
{
echo '<div class="alert alert-success"><strong>Success!</strong> Sub Category Page Update Successfully.</div>';
mysqli_query($cn, "update `tbl_product` SET `subcategory_order`='$sort_order' where `subcategory_id`='$id'");
}
else
{
echo '<div class="alert alert-success"><strong>Warrning!</strong> Not Updated try again.</div>';
}
}
}
if(isset($_GET['edit_id']))
{
$id = $_GET['edit_id'];
$sql = mysqli_query($cn, "select * from `tbl_subcategory` where id='$id'");
$sqlData = mysqli_fetch_array ($sql);
}
?>
<div class="card">
<div class="card-body">
<h4 class="card-title">Subcategory</h4>
<form class="forms-sample" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label for="exampleInputPassword4">Category</label>
<input type="hidden" value="<?php if(isset($_GET['edit_id'])) {echo $sqlData['id'];}?>" name="id">
<input type="hidden" class="tableName" value="tbl_subcategory">
<select class="form-control" id="exampleFormControlSelect1" name="category_id" >
<option value="">Select Category</option>
<?php
$sqlCategory = mysqli_query($cn, "select * from `tbl_category` where `status`=1 order by `sort_order`");
while($sqlCategoryData = mysqli_fetch_array($sqlCategory)){?>
<option value="<?php echo $sqlCategoryData['id'];?>" <?php if(isset($_GET['edit_id'])){ if($sqlData['category_id']==$sqlCategoryData['id']){ echo "selected";}}?>><?php echo $sqlCategoryData['name'];?></option>
<?php }?>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="exampleInputName1">Subcategory Name</label>
<input type="text" class="form-control setslugUrl" value="<?php if(isset($_GET['edit_id'])) {echo $sqlData['name'];}?>" name="name" id="exampleInputName1" placeholder="Name" autocomplete="off" required>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="exampleInputName1">Sort Order</label>
<input type="text" class="form-control" value="<?php if(isset($_GET['edit_id'])) {echo $sqlData['sort_order'];}?>" name="sort_order" placeholder="Sort Order" autocomplete="off" required>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Status</label>
<div class="row">
<div class="col-md-4">
<div class="form-radio">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="status" id="membershipRadios1" value="1" <?php if(isset($_GET['edit_id'])){ if($sqlData['status']==1){ echo "checked";}}else{ echo "checked";}?>> Yes
</label>
</div>
</div>
<div class="col-md-4">
<div class="form-radio">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="status" id="membershipRadios2" value="0" <?php if(isset($_GET['edit_id'])){if($sqlData['status']==0){ echo "checked";}}?>> No
</label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="exampleInputName1">Slug Url</label>
<input type="text" class="form-control slugUrl" value="<?php if(isset($_GET['edit_id'])) {echo $sqlData['slug_url'];}?>" name="slug_url" id="exampleInputName1" placeholder="Slug Url" autocomplete="off" required>
</div>
</div>
</div>
<button type="submit" class="btn btn-success mr-2" name="<?php if(isset($_GET['edit_id'])) {echo "edit";}else{ echo "add";}?>">Submit</button>
</form>
</div>
</div>
<br><br>
<?php
if(isset($_GET['delete_id']))
{
$id = $_GET['delete_id'];
$sql = mysqli_query($cn, "update `tbl_subcategory` SET `status`=2 where id='$id'");
if($sql)
{
echo '<div class="alert alert-success"><strong>Success!</strong> SubCategory Deleted Successfully.</div>';
}
else
{
echo "error: " . $sql . "<br>" . mysqli_error($conn);
}
}
if(isset($_GET['status']))
{
$id = $_GET['status'];
$val = $_GET['val'];
$sql = mysqli_query($cn, "update `tbl_subcategory` SET `status`='$val' where id='$id'");
}
?>
<div class="col-lg-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<h4 class="card-title">Subcategory List</h4>
<div class="table-responsive">
<table class="table table-striped" id="example">
<thead>
<tr>
<th>Sr. No.</th>
<th>Category</th>
<th>Sub Category Name</th>
<th>Sort Order</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$sqlEmployee = mysqli_query($cn, "select * from `tbl_subcategory` where `status` IN(0,1) order by `sort_order`");
while($sqlEmployeeData = mysqli_fetch_array($sqlEmployee))
{
$sqlCategory = mysqli_query($cn, "select * from `tbl_category` where `id`='".$sqlEmployeeData['category_id']."'");
$sqlCategoryData = mysqli_fetch_array($sqlCategory)
?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $sqlCategoryData['name'];?></td>
<td><?php echo $sqlEmployeeData['name'];?></td>
<td><?php echo $sqlEmployeeData['sort_order'];?></td>
<td>
<?php
if($sqlEmployeeData['status']==1){?>
<a href="home.php?pages=subcategory&status=<?php echo $sqlEmployeeData['id'];?>&val=0">YES</a>
<?php }else{?>
<a href="home.php?pages=subcategory&status=<?php echo $sqlEmployeeData['id'];?>&val=1">NO</a>
<?php }?>
</td>
<td><a href="home.php?pages=subcategory&edit_id=<?php echo $sqlEmployeeData['id'];?>">Edit</a> | <a href="home.php?pages=subcategory&delete_id=<?php echo $sqlEmployeeData['id'];?>" onclick="return deleteConfirm();">Delete</a></td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div>
</div>
</div>