<?php

require_once("db_conn_class2.php");

//creating object of database class
$dbh = new db_conn();
$dbh->set_conn();

$blogs = "";

if(isset($_POST['txtBlogs']) && ($_POST['txtBlogs'] != ""))
{
	$blogs = $_POST['txtBlogs'];
}

//getting blog category id by explode function
$path = ltrim($_SERVER['REQUEST_URI'], '/');
//echo "path = ".$path;
$b = explode("/", $path);
$id = explode(".", $b[1]);
$b_url = explode("-", $id[0]);
$b_id = $b_url[0];

$blog_category_id = $b_id;

//echo "blog_category_id = ".$blog_category_id;

//if (isset($_GET["cat"])) { $blog_category_id = $_GET["cat"]; } else { $blog_category_id = 0; };

//==========================================================================================================
//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;

//==========================================================================================================

//==================================================================================================================
//fetching latest jobs from database
$sql_blogs = "select * from tbl_blogs ";
$sql_blogs = $sql_blogs . " where status = 'ACTIVE' and blog_type = 'ACT' and deleted = 0 ";
if($blogs != "")
{
	$sql_blogs = $sql_blogs . " and blog_title like '%".$blogs."%'";
}
if($blog_category_id > 0)
{
	$sql_blogs = $sql_blogs . " and blog_category = ".$blog_category_id;
}
$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_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 = 'ACT' and deleted = 0 ";
if($blogs != "")
{
	$sql = $sql . " and blog_title like '%".$blogs."%'";
}
if($blog_category_id > 0)
{
	$sql_blogs = $sql_blogs . " and blog_category = ".$blog_category_id;
}
$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";
}
//==================================================================================================================


//==========================================
//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;
}
//==========================================


?>