| | |
Using a Serch option on a mag site.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2006
Posts: 3
Reputation:
Solved Threads: 0
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 = ' '; // we're on page one, don't print previous link
$first = ' '; // 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 = ' '; // we're on the last page, don't print next link
$last = ' '; // 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>
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 = ' '; // we're on page one, don't print previous link
$first = ' '; // 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 = ' '; // we're on the last page, don't print next link
$last = ' '; // 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>
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: Then display the results the same way the Articles page does.
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)
"select * from Articles where Articles.Keyword like '%$userquery%'"
•
•
Join Date: Jul 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
[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.
![]() |
Similar Threads
- Nice Arcade site for sale (Websites for Sale)
- submiting info into a database for retrieval! (PHP)
- Very annoying problem with flickering screen... (Windows NT / 2000 / XP)
- German to English (Geeks' Lounge)
- port forwarding? (Network Security)
- In need of Menu over Frames (JavaScript / DHTML / AJAX)
- Error getting forms to send information (PHP)
Other Threads in the PHP Forum
- Previous Thread: Php Mail
- Next Thread: get rid of blank lines with strreplace()
| Thread Tools | Search this Thread |
ajax apache api array arrays beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external file files folder form forms forum function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert integration ip java javascript joomla limit link login loop mail malfunction menu method mlm multiple mysql neutrality oop paypal pdf php phpmysql play problem query question radio random recursion regex remote root script search select server sessions sms soap source space sql syntax system table tutorial update upload url validator variable video web xml youtube





