session_start(); include_once($_SERVER['DOCUMENT_ROOT']."/includes/config.php"); include_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connect.php"); // establish database connection include_once($_SERVER['DOCUMENT_ROOT']."/includes/inc_functions.php"); // load functions $thisSection = "News Blog"; $thisSub = "Main"; $thisTime = time(); $currentTime = date("Y-m-j H:i:s", $thisTime); // start paging functionality // how many rows to show per page $rowsPerPage = 5; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; if (isset($_GET['entrycatID'])) { $entrycatID = $_GET['entrycatID']; // get all the latest 20 entries $query_entries = "SELECT DISTINCT e.entryID, e.entryTitle, e.entryContent, e.entryMedia, e.entryDate, date_format(e.entryDate, '%b %e, %Y') as entryDateShow, e.entryURI, u.userID, u.firstName, u.lastName FROM Entries e, EntriesCatLinker ecl LEFT JOIN Users u ON u.userID = e.userID WHERE e.entryStatus = 'Published' AND ecl.entrycatID = '".$entrycatID."' AND e.entryID = ecl.entryID AND e.entryDate <= '".$currentTime."' ORDER BY e.entryDate DESC LIMIT ".$offset.", ".$rowsPerPage.""; $result_entries = mysql_query($query_entries) or die ("Entries Query Failed: " .mysql_error()); $num_entries = mysql_num_rows($result_entries); // get the total for paging purposes $query_total = "SELECT DISTINCT e.entryID FROM Entries e, EntriesCatLinker ecl LEFT JOIN Users u ON u.userID = e.userID WHERE e.entryStatus = 'Published' AND ecl.entrycatID = '".$entrycatID."' AND e.entryID = ecl.entryID AND e.entryDate <= '".$currentTime."' ORDER BY e.entryDate DESC"; $result_total = mysql_query($query_total) or die ("Total Entries Query Failed: " .mysql_error()); $num_total = mysql_num_rows($result_total); } elseif (isset($_GET['authorID'])) { $userID = $_GET['authorID']; $query_entries = "SELECT e.entryID, e.entryTitle, e.entryContent, e.entryMedia, e.entryDate, date_format(e.entryDate, '%b %e, %Y') as entryDateShow, e.entryURI, u.userID, u.firstName, u.lastName FROM Entries e LEFT JOIN Users u ON u.userID = e.userID WHERE e.entryStatus = 'Published' AND u.userID = '".$userID."' AND e.entryDate <= '".$currentTime."' ORDER BY e.entryDate DESC LIMIT ".$offset.", ".$rowsPerPage.""; $result_entries = mysql_query($query_entries) or die ("Entries Query Failed: " .mysql_error()); $num_entries = mysql_num_rows($result_entries); // get the total for paging purposes $query_total = "SELECT e.entryID FROM Entries e LEFT JOIN Users u ON u.userID = e.userID WHERE e.entryStatus = 'Published' AND u.userID = '".$userID."' AND e.entryDate <= '".$currentTime."' ORDER BY e.entryDate DESC"; $result_total = mysql_query($query_total) or die ("Total Entries Query Failed: " .mysql_error()); $num_total = mysql_num_rows($result_total); } else { $query_entries = "SELECT e.entryID, e.entryTitle, e.entryContent, e.entryMedia, e.entryDate, date_format(e.entryDate, '%b %e, %Y') as entryDateShow, e.entryURI, u.userID, u.firstName, u.lastName FROM Entries e LEFT JOIN Users u ON u.userID = e.userID WHERE e.entryStatus = 'Published' AND e.entryDate <= '".$currentTime."' ORDER BY e.entryDate DESC LIMIT ".$offset.", ".$rowsPerPage.""; $result_entries = mysql_query($query_entries) or die ("Entries Query Failed: " .mysql_error()); $num_entries = mysql_num_rows($result_entries); // get the total for paging purposes $query_total = "SELECT e.entryID FROM Entries e LEFT JOIN Users u ON u.userID = e.userID WHERE e.entryStatus = 'Published' AND e.entryDate <= '".$currentTime."' ORDER BY e.entryDate DESC"; $result_total = mysql_query($query_total) or die ("Total Entries Query Failed: " .mysql_error()); $num_total = mysql_num_rows($result_total); } // build paging navigation // total number of pages $maxPage = ceil($num_total/$rowsPerPage); // print the link to access each page if ((strlen($entrycatID) > 0)) { $self = $_SERVER['PHP_SELF']."?entrycatID=".$entrycatID."&"; } elseif ((strlen($userID) > 0)) { $self = $_SERVER['PHP_SELF']."?authorID=".$userID."&"; } else { $self = $_SERVER['PHP_SELF']."?"; } $nav = ''; if ($maxPage > 1) { for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= "".$page.""; // no need to create a link to current page } else { $nav .= " ".$page." "; } } // creating previous and next link plus the link to go straight to the first and last page if ($pageNum > 1) { $page = $pageNum - 1; $prev = "« Prev "; $first = " « First Page "; } else { $prev = ''; // we're on page one, don't print previous link $first = ''; // nor the first page link } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " Next »"; $last = " Last Page »"; } else { $next = ''; // we're on the last page, don't print next link $last = ''; // nor the last page link } } // end if pageNum > 1 meaning, display navigation if there are more than 1 page worth of results /* keep this query SELECT DISTINCT e.entryID, e.entryTitle, e.entryContent, e.entryMedia, e.entryDate, date_format(e.entryDate, '%b %e, %Y') as entryDateShow, u.userID, u.firstName, u.lastName, ec.entrycatID, ec.entrycatName FROM Entries e, EntryCategories ec, EntriesCatLinker ecl LEFT JOIN Users u ON u.userID = e.userID WHERE ec.entrycatID = ecl.entrycatID AND ec.entrycatID = (SELECT ec.entrycatID FROM EntriesCatLinker ecl, EntryCategories ec, Entries e WHERE e.entryID = ecl.entryID AND ecl.entrycatID = ec.entrycatID) ORDER BY e.entryDate DESC */ ?>