944,080 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1431
  • PHP RSS
Jul 5th, 2006
0

Using a Serch option on a mag site.

Expand Post »
I am new here and having a big headache. I have inherited a web site at my job. It is a site for a history magazine and I need to be able to have a search and advanced search option that allows users to search the article table by keywords. The database is already set up with an article table with a keyword row. I have no idea how to go about this. This is the script I have for calling articles up by category how can I modify it to search using that darn search box? the connection string is in my bin/html/common-header.php but I can provide it if needed.
Thanks in advance for any help!

<?
$path = "";
$pageID = "categories";

$CategoryID = $_REQUEST['ID'];

$self = $_SERVER['PHP_SELF'];

// how many rows to show per page
$rowsPerPage = 20;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
} else {
// by default we show first page
$pageNum = 1;
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
?>

<HTML>
<HEAD>
<TITLE>Current History</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL=StyleSheet HREF="current-history-style.css" TYPE="text/css">

</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>

<? require("bin/html/common-header.php"); ?>

<?
$categoryHeader_result = mysql(currenthistorydb, "select * from Categories where (CategoryID = $CategoryID)");
$num = mysql_numrows($categoryHeader_result);
$i = 0;
$Title = mysql_result($categoryHeader_result, $i, "CategoryName");

$article_result = mysql(currenthistorydb, "select Articles.ArticleID, Articles.Title, Articles.Author, Articles.Month, Articles.Year, Articles.Abstract, CategoryArticles.ArticleID, CategoryArticles.CategoryID from Articles, CategoryArticles where ((Articles.display = 1) and (CategoryArticles.ArticleID = Articles.ArticleID) and ( CategoryArticles.CategoryID = $CategoryID)) order by Year desc, Issue desc, page asc LIMIT $offset, $rowsPerPage");

$query = "SELECT COUNT(Articles.ArticleID) AS numrows FROM Articles, CategoryArticles where ((Articles.display = 1) and (CategoryArticles.ArticleID = Articles.ArticleID) and ( CategoryArticles.CategoryID = $CategoryID))";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// echo "<br>numrow" . $numrows;
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// echo "<br>maxpage" . $maxPage;


if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?ID=$CategoryID&page=$page\">[Prev]</a> ";

$first = " <a href=\"$self?ID=$CategoryID&page=1\">[First Page]</a> ";
}
else
{
$prev = '&nbsp;'; // we're on page one, don't print previous link
$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?ID=$CategoryID&page=$page\">[Next]</a> ";

$last = " <a href=\"$self?ID=$CategoryID&page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = '&nbsp;'; // we're on the last page, don't print next link
$last = '&nbsp;'; // nor the last page link
}


$num = mysql_numrows($article_result);
$k = 0;


// print the page navigation link

echo "<table border=0 cellpadding=0 cellspacing=0 width=\"100%\"><tr><td><h1 align=left style=\"margin:0px;\">$Title</h1><td>";
echo "<td align=right nowrap valign=bottom>Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages<br> " . $first . $prev . $next . $last . "</td></tr></table>";
echo "<hr>";


while ($k < $num) {
echo "<p><b>" . mysql_result($article_result, $k, "Month") . " " . mysql_result($article_result, $k, "Year") . "</b><br>
<a href=\"Article.php?ID=" . mysql_result($article_result, $k, "ArticleID") . "\" class=\"title\">" . mysql_result($article_result, $k, "Title") . "</a><br>";
if ( mysql_result($article_result, $i, "Author") <> "") {
echo "<a href=\"Article.php?ID=" . mysql_result($article_result, $i, "ArticleID") . "\" class=\"author\">by " . mysql_result($article_result, $i, "Author") . "</a><br>";
}
echo mysql_result($article_result, $k, "Abstract") ."</p>";
$k++;
}






// print the page navigation link
echo "<div align=right>Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages<br> " . $first . $prev . $next . $last . "</div>";

?>



<? require("bin/html/common-footer.php"); ?>

</BODY>
</HTML>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krhelter is offline Offline
3 posts
since Jul 2006
Jul 8th, 2006
0

Re: Using a Serch option on a mag site.

To search the Articles table for the user query:
Create the search form giving the search field a name. You can set the form method to GET or POST. I'd use GET.
[html]<form name="sitesearch" action="search.php" method="get">
<input type="text" name="userquery">
</form>[/html]Then in the searh.php file assign the value from the input field to a variable:[php]$userquery = $_GET['userquery'];[/php]Then search the Keyword row for that userquery:
PHP Syntax (Toggle Plain Text)
  1. "select * from Articles where Articles.Keyword like '%$userquery%'"
Then display the results the same way the Articles page does.
Reputation Points: 10
Solved Threads: 0
Light Poster
KeithMcL is offline Offline
34 posts
since Oct 2004
Jul 14th, 2006
0

Re: Using a Serch option on a mag site.

this is the new script:
[php]
$name = $_GET['keywords'];
$name_arr = explode(' ', $name);
$search_result = '';

foreach($name_arr as $key => $name)
{
$query="SELECT * FROM Articls WHERE";
$query.=" Keywords LIKE '%$name%'";
$query.=" OR Title LIKE '%$name%'";
$query.=" OR Author LIKE '%$name%' ORDER BY title";
$result_arr[$key]=mysql_db_query("currenthistorydb", $query);
$num_rows_arr[$key] = mysql_num_rows($result_arr[$key]);
$search_result.= "Found $num_rows_arr[$key] results for the term $name.<br />";
}

mysql_close();

echo($search_result);

echo '<b><center><font size="4" color="#FF0000">Search Result</font></center></b><br><br>';

foreach($result_arr as $key => $result)
{
$i=0;
while ($i < $num_rows_arr[$key])
{

$row = mysql_fetch_row($result);
$search_term = $name_arr[$key];
$authors = $row[1];
$title = $row[2];
$source = $row[3];

echo "<b>Search Term:</b> $search_term<br><b>Author:</b> $authors<br><b>Title:</b> $title<br><b>Source:</b> $source<br><br><hr><br>";

$i++;
}
}[/php]
and i am getting this:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/currenthistory.com/htdocs/preview/search-results.php on line 32
Found results for the term China.
Last edited by krhelter; Jul 14th, 2006 at 1:15 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krhelter is offline Offline
3 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Php Mail
Next Thread in PHP Forum Timeline: get rid of blank lines with strreplace()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC