[code=php]
<?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_bb_code_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(‘includes/meta_info.html’); ?>
<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’, ‘[email protected]’);
define(‘SUBJECT’, ‘Rebirth Database Connection Error’);
$headers = “From: Rebirth Databases <[email protected]> rn”;
$headers .= “Reply-To: Rebirth Databases <[email protected]> rn”;
$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);
$search_length = strlen($searching) + 2;
$searching = str_pad($searching, $search_length, ‘%’, STR_PAD_BOTH);
// 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 * FROM posts WHERE post_body LIKE ‘$searching’ ORDER BY post_id DESC LIMIT ” . $offset . “, ” . $entries_per_page;
$result2 = $conn->query($query2);
/*$result2->store_result();
if (!$result2->num_rows())
{
echo “<p class=’warning’>Sorry, but your search did not produce any results. <a href=’http://www.uhrebirth.com/’>Return Home</a>.</p>”;
$result2->close();
$conn->close();
exit;
}
else
{*/
while ($row2 = $result2->fetch_array(MYSQLI_ASSOC))
{
// format line breaks
$row2[‘post_body’] = str_replace(“n”, “<br/>n”, $row2[‘post_body’]);
$row2[‘post_body’] = str_replace($searching, html_entity_decode($searching), $searching);
$row2[‘post_body’] = str_replace(‘%’, ”, $searching);
$row2[‘post_body’] = preg_replace(“/(.*b$searchingb.*)/U”, “$1”, $row2[‘post_body’]);
$row2[‘post_body’] = noBB($row2[‘post_body’]);
// display the news
echo “<div class=’news’><a href=’http://www.uhrebirth.com/show_news/” . $row2[‘post_id’] . “‘>” . $row2[‘title’] . “</a> <a href=’http://www.uhrebirth.com/rss.php’><img src=’http://www.uhrebirth.com/images/misc/rss.gif’ hspace=’5′ title=’Subscribe via RSS’ alt=’Subscribe via RSS’/></a><a href=’http://www.uhrebirth.com/atom.php’><img src=’http://www.uhrebirth.com/images/misc/atom.gif’ title=’Subscribe via Atom’ alt=’Subscribe via Atom’/></a></div>”;
echo “<div class=’news’><b>Posted by:</b> <a href=’mailto:” . $row2[‘author_email’] . “>” . $row2[‘author_name’] . ” in <a href=’http://www.uhrebirth.com/news_categories.php’>” . $row2[‘category_name’] . “</a>”;
echo “<div class=’news’><b>” . $row2[‘formatted_date’] . “</b></div>”;
echo “<div class=’news_post’>” . $row2[‘post_body’] . “ <a href=’http://www.uhrebirth.com/show_news/” .$row2[‘post_id’] . “‘>Read More</a>”;
}
echo “<br/><br/><br/>”;
// now, display the pages
for ($i = $first_page; $i <= $last_page; $i++)
{
if ($i == $page_number)
{
echo $i;
}
else
{
echo “<a href=’http://www.uhrebirth.com/news_search_results/page/$i’>$i</a>”;
}
}
//}
$result2->close();
$conn->close();
}
}
}
if ($_POST && isset($missing))
{
echo “<p class=’warning’>Please enter a search phrase.</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>
Where do I begin?
1) I need to do an INNER JOIN, but I don’t know how to do it on this type of query without breaking it.
2) As you can see from the commented out code, I don’t know how to see if I’ve gotten anything from MySQL. Those two functions don’t seem to work on this query.
3) post_body is only being returned with the search value instead of the whole post.
4) author_name is not getting displayed.
5) Even though I’m using the same divs and CSS as my homepage, the news isn’t centering like it does on my homepage ([url]www.uhrebirth.com