<?php
include("../include/config.php"); 
if(isset($_POST['add_blogs_details'])){
    
    $akudate=date("d-M-Y");
    $title=mysqli_real_escape_string($conn,$_POST['blogs_title']);
    $slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($title)));
   
   $query = "SELECT g_code FROM g_category WHERE g_code LIKE '$slug%'";
   $query_execute=mysqli_query($conn,$query);
   $total_row =mysqli_num_rows($query_execute);
   if($total_row > 0){
       
      $result=mysqli_fetch_all($query_execute, MYSQLI_ASSOC);
      foreach($result as $row)
   {
    $data[] = $row['g_code'];
   }
   
   if(in_array($slug, $data))
   {
    $count = 0;
    while( in_array( ($slug . '-' . ++$count ), $data) );
    $slug = $slug . '-' . $count;
   }
   }
  
  $insert_title="insert into g_category(`g_category_name`,`g_code`,`postDate`,`status`)values('$title','$slug','$akudate','active')";
 
  $insert_title_query_execute=mysqli_query($conn,$insert_title);
  if($insert_title_query_execute){
      
     echo "<script>alert('news title created successfully')</script>";
     echo "<script>window.open('add-news.php','_self')</script>";
  }else{
      
       echo "<script>alert('Oops Sometthing is Wrong')</script>";
     echo "<script>window.open('add-news.php','_self')</script>";
  }
}
?>