<?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'];
}

//==========================================================================================================
//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 = 'TesolCourseIndia' 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;
//return;
$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 = 'TesolCourseIndia' and deleted = 0 ";
if($blogs != "")
{
	$sql = $sql . " and blog_title like '%".$blogs."%'";
}
$sql = $sql . " order by posting_dt DESC ";
//echo "sql=".$sql;
//return;
$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 for sidebar
$sql_blogs2 = "select * from tbl_blogs ";
$sql_blogs2 = $sql_blogs2 . " where status = 'ACTIVE' and blog_type = 'TesolCourseIndia' 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;
}
//==========================================


?>