/    Sign up×
Community /Pin to ProfileBookmark

All Kinds of Search Form Trouble

[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, ‘&#37;’, 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’] . “&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>[/code]

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[/url]).

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Jul 21.2009 — For figuring out if no rows were returned...

Instead of:

if (!$result2->num_rows())

{

echo "<p class='
[/quote]


Try
[code=php]
if ($result2->num_rows() <=0)
{

echo "<p class='
[/code]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 21.2009 — Thanks?

I've updated the query a bit. But I keep getting an error returned with stmt->bind_param(). Here's the current script:

[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_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', '[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);


// 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 post_id, author_id, author_name, DATE_FORMAT(date_posted, '&#37;W, %M %d, %Y %l:%i %p') AS formatted_date, author_email, a.category_id, c.category_name, title, post_body FROM posts a INNER JOIN categories c ON a.category_id = c.category_id WHERE 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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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_disclaimer');
?>
</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>[/code]
Copy linkTweet thisAlerts:
@criterion9Jul 21.2009 — What error are you getting?
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 21.2009 — 
Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in /home/josephwitchard/uhrebirth.com/news_search_results.php on line 182[/QUOTE]


It leads me to believe I don't have the query structured correctly. Every time I do a search, I get returned the one single same post, no matter what phrase I've searched for, and the above error.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 22.2009 — Thanks for the help, everyone. I've got it working? See this post for the solution.
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...