<?php
/** Coded by: Jeffrey (Joseph Witchard)
** Created on: 07/20/09
** Last modified: 07/21/09
** Purpose: To search for specific
** Rebirth news and display
** it to the user. */
// strip BBCode
require('path_to_BBCode_stripper');
// set up the connection
require('path_to_connection');
$conn = @connect_function();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<title>News Search Results -- Ultimate Hogwarts: The Rebirth</title>
<?php include('path_to_meta_data'); ?>
<link href="/css/general.css" rel="stylesheet" type="text/css"/>
<link href="/css/homepage.css" rel="stylesheet" type="text/css"/>
<link href="/css/main_pages.css" rel="stylesheet" type="text/css"/>
<link href="/css/ticker.css" rel="stylesheet" type="text/css"/>
<link href="/favicon.ico" rel="shortcut icon"/>
<script type="text/javascript" src="/javascripts/scroll.js"></script>
</head>
<body>
<div id="wrapper">
<?php include('path_to_headers'); ?>
<div id="navigation">
<?php include('path_to_navigation'); ?>
</div>
<div id="right">
<?php include('path_to_news_scroller'); ?>
<div id="rightAd">
<?php include('path_to_google_ad'); ?>
</div>
</div>
<div align="center" id="center">
<h3>Search Results</h3>
<div id="news">
<?php
if (mysqli_connect_error())
{
// get ready to mail me the error
define('TO', 'josephwitchard@uhrebirth.com');
define('SUBJECT', 'Rebirth Database Connection Error');
$headers = "From: Rebirth Databases <rebirth_databases@uhrebirth.com> \r\n";
$headers .= "Reply-To: Rebirth Databases <rebirth_databases@uhrebirth.com> \r\n";
$message = "There was a connection error on " . DB_NAME . " at uhrebirth.com" . $_SERVER['PHP_SELF'] . ". The error returned was: " . mysqli_connect_error();
// send the message
mail(TO, SUBJECT, $message, $headers);
echo "<p class='warning'>There was an error when connecting to the database. The webmaster has been notified of this error. Please try again later.</p>";
exit;
}
if (array_key_exists('search', $_POST) && !empty($_POST['search']))
{
// set up required and expected fields
$required = array('searching');
$expected = array('searching');
// set up an empty array for missing fields
$missing = array();
// process the post variables
foreach ($_POST as $key => $value)
{
// assign to a temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
if (empty($temp) && in_array($key, $required))
{
// add to missing
$missing[] = $key;
}
elseif (in_array($key, $expected))
{
// assign to a variable of the same name
${$key} = $temp;
}
}
// go ahead only if missing is empty
if (empty($missing))
{
// we no longer need missing
unset($missing);
// begin preparing the search term for searching
$searching = htmlentities($searching, ENT_QUOTES);
$searching = $conn->real_escape_string($searching);
// start the query
$query1 = "SELECT COUNT(*) FROM posts WHERE post_body LIKE '$searching'";
if ($result1 = $conn->query($query1))
{
$row1 = $result1->fetch_row();
// assign the total to a variable
$total_entries = $row1[0];
// close the first result and set the number of page entries
$result1->close();
$entries_per_page = 10;
if (isset($_GET['page_number']))
{
// assign the page number to a variable
$page_number = $_GET['page_number'];
}
else
{
// give it 1
$page_number = 1;
}
// divide and round total_entries with entries_per_page
$total_pages = ceil($total_entries / $entries_per_page);
// set the number of pages to be displayes
$first_page = max(1, $page_number - 9);
$last_page = min($total_pages, $page_number + 6);
// tell the database where to start fetching by setting an offset
$offset = ($page_number - 1) * $entries_per_page;
// get the entries out and display them
$query2 = "SELECT posts.post_id, posts.author_id, posts.author_name, DATE_FORMAT(posts.date_posted, '%W, %M %d, %Y %l:%i %p') AS formatted_date, posts.author_email, posts.category_id, categories.category_name, posts.title, posts.post_body FROM posts INNER JOIN categories ON posts.category_id = categories.category_id WHERE posts.post_body LIKE '%?%' ORDER BY post_id DESC LIMIT " . $offset . ", " . $entries_per_page;
$stmt = $conn->prepare($query2);
$stmt->bind_param('s', $searching);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows() <= 0)
{
echo "<p class='warning'>We're sorry, but your search did not return any results.</p>";
$stmt->close();
$conn->commit();
$conn->close();
exit;
}
$stmt->bind_result($postID, $authorID, $authorName, $datePosted, $authorEmail, $catID, $catName, $postTitle, $postBody);
while ($stmt->fetch())
{
// prepare the data for display
$postBody = str_replace("\n", "<br/>\n", $postBody);
$postBody = html_entity_decode($postBody, ENT_QUOTES);
$postBody = noBB($postBody);
// DISPLAY THE RESULTS
echo "<div class='news'><a href='http://www.uhrebirth.com/show_news/$postID'>$postTitle</a></div>";
echo "<div class='news'><b>Posted by:</b> <a href='mailto:$authorEmail'>$authorName</a> in <a href='http://www.uhrebirth.com/news_categories.php'>$catName</a></div>";
echo "<div class='news'><b>$datePosted</b></div>";
echo "<div class='news_post'>$postBody <a href='http://www.uhrebirth.com/show_news/$postID'>Read More</a></div>";
echo "<br/><br/><br/>";
}
// now, display the pages
for ($i = $first_page; $i <= $last_page; $i++)
{
if ($i == $page_number)
{
echo $i;
}
else
{
"<a href='http://www.uhrebirth.com/news_search_results/page/$i'>$i</a>";
}
}
// close everything
$stmt->close();
$conn->commit();
$conn->close();
}
}
}
// make sure something was entered
if ($_POST && isset($missing))
{
echo "<p class='warning'>You didn't enter anything into the search field. Please try again.</p>";
}
include('path_to_disclaimerl');
?>
</div>
</div>
</div>
</body>
<!-- Coded by: Jeffrey (Joseph Witchard)
** Created on: 07/21/09
** Last modified: 07/21/09
** Purpose: To display the search results
** to the user. -->
</html>
Judging by the query I ran with this syntax in phpMyAdmin, the syntax of the query is in error. However, I have never used LIKE and INNER JOIN together before; I don't know how it's supposed to look. Any ideas?