<?php
require_once("db_conn_class.php");
//creating object of database class
$dbh = new db_conn();
$dbh->set_conn();
$blogs = "";
if(isset($_POST['txtBlogs']) && ($_POST['txtBlogs'] != ""))
{
	$blogs = $_POST['txtBlogs'];
}
//==========================================================================================================
//code for pagination
$num_rec_per_page = 6;
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
//=========================================================================================================
//query for fetching blog categories
$sql_cat = "select category_id, category from tbl_category where category_for = 'TTKolkata' and status = 'ACTIVE' and deleted = 0 ";
$sql_cat = $sql_cat . " order by category ";
//echo "sql_cat=".$sql_cat;
$result_cat = mysqli_query($dbh->link,$sql_cat);

if($result_cat)
{
	$no_of_cat = mysqli_num_rows($result_cat);
	  for($j=0; $j<$no_of_cat; $j++)  
	  {  
	  $rw_cat = mysqli_fetch_array($result_cat);
	  $category_id[$j] = $rw_cat['category_id'];
	  $category[$j] = $rw_cat['category'];
	  }
}
//==================================================================================================================
//fetching latest blogs from database
$sql_blogs = "select * from tbl_blogs ";
$sql_blogs = $sql_blogs . " where status = 'ACTIVE' and blog_type = 'TTKolkata' and deleted = 0 ";
if($blogs != "")
{
	$sql_blogs = $sql_blogs . " and blog_title like '%".$blogs."%'";
}
$sql_blogs = $sql_blogs . " order by posting_dt DESC, blog_id DESC limit $start_from, $num_rec_per_page ";
//echo "sql_blogs=".$sql_blogs;
$result_blogs = mysqli_query($dbh->link,$sql_blogs);
if($result_blogs)
{
	$no_of_blogs = mysqli_num_rows($result_blogs);
	
	for($i=0; $i<$no_of_blogs; $i++)
	{
		$rw_blogs = mysqli_fetch_array($result_blogs);
		
		$blog_id[$i]         = $rw_blogs['blog_id'];
		$blog_title[$i]      = $rw_blogs['blog_title'];
		$blog_pre_desc[$i]   = $rw_blogs['blog_pre_desc'];
		$blog_desc[$i]       = $rw_blogs['blog_desc'];
		$written_by[$i]      = $rw_blogs['written_by'];
		$posting_dt[$i]      = $rw_blogs['posting_dt'];
		$blog_image_path[$i] = $rw_blogs['blog_image_path'];
	}	
}
/*$sql = "select * from tbl_blogs ";
$sql = $sql . " where status = 'ACTIVE' and blog_type = 'TTHyderabad' and deleted = 0 ";
if($blogs != "")
{
	$sql = $sql . " and blog_title like '%".$blogs."%'";
}
$sql = $sql . " order by posting_dt DESC ";
//echo "sql_jobs=".$sql_jobs;
$rs_result = mysqli_query($dbh->link,$sql);
$total_records = mysqli_num_rows($rs_result);  //count number of records
//echo "total_records=".$total_records;
$total_pages = ceil($total_records / $num_rec_per_page);
$msg = "";
if ($no_of_blogs == 0)
{
	$msg = "No Blogs Found";
}
*///==================================================================================================================
//==================================================================================================================
//fetching latest blogs from database
$sql_blogs2 = "select * from tbl_blogs ";
$sql_blogs2 = $sql_blogs2 . " where status = 'ACTIVE' and blog_type = 'TTKolkata' and deleted = 0 ";
//if($blogs != "")
//{
//	$sql_blogs2 = $sql_blogs2 . " and blog_title like '%".$blogs."%'";
//}
$sql_blogs2 = $sql_blogs2 . " order by posting_dt DESC, blog_id DESC limit $start_from, $num_rec_per_page";
//echo "sql_blogs2=".$sql_blogs2;
$result_blogs2 = mysqli_query($dbh->link,$sql_blogs2);
if($result_blogs2)
{
	$no_of_blogs2 = mysqli_num_rows($result_blogs2);
	
	for($i=0; $i<$no_of_blogs2; $i++)
	{
		$rw_blogs2 = mysqli_fetch_array($result_blogs2);
		
		$blog_id2[$i]         = $rw_blogs2['blog_id'];
		$blog_title2[$i]      = $rw_blogs2['blog_title'];
		
	}	
}
//==========================================
//declare starting and ending page no 
$s = 1;
$e = 7;
if($page > 7)
{
	$s = $page - 2;
	$e = $s + 6;
}
else
{
	$s = 1;
	$e = $s + 6;
}
if($e > $total_pages)
{
	$e = $total_pages;
}
//==========================================
?>