You are here: Re: Search MySQL db with PHP « All PHP « IT news, forums, messages
Re: Search MySQL db with PHP

Posted by Chris on 10/10/11 11:51

Hi again,

Sorry Lorento - my brain typed Lorenzo :)

I got the search to work finally, however, I am having issues with getting
it to limit to a certain number of results per page. Then, of course,
there's getting the results to show in the content section of each page
since all pages have includes for the header, navigation, and footer
sections. Any guidelines on this would be appreciated.

Thanks,
Chris

"Chris" <designerNOSPAM@centurytel.net> wrote in message
news:e7rrlk$cm$1@news01.intel.com...
> Hi,
>
> Thanks Lorenzo...Actually, that part I saw right after I posted and
> changed it to keyword throughout as it is supposed to search 3 different
> columns, but it still won't pass the value. I assigned a varible to it:
> $keyword = $_POST['keyword']; I also separated the 2 pages, 1 for the
> form, and 1 for the results because at one point the form was coming up
> with 2 items listed below it without even inputting a search keyword.
>
> It still won't work. I thought this would be so easy...
>
> This is what I have now, and it just gives me another error: "You have an
> error in your SQL syntax; check the manual that corresponds to your MySQL
> server version for the right syntax to use near 'LIMIT 0, 25' at line 1".
> I have looked at the MySQL manual/site and there is very little reference
> to LIMIT, as well as researching PHP and Googling the error. I have some
> familiarity with SQL Server, but MySQL has slightly different syntax -
> such as not wanting to recognize alias column names in the WHERE clause.
>
> Form page:
>
> <!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">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> <title>Untitled Document</title>
> </head>
>
> <body>
>
> <p>Simple Search Form:</p>
> <form id="search" name="search1" method="post" action="dwmxsearch.php">
> <label>Keywords:
> <input name="keyword" type="text" size="30"/>
> </label>
> <label>
> <input type="submit" name="Submit" value="Submit" />
> </label>
> <input name="submitted" type="hidden" value="submitted" />
> </form>
> <p>
> </p>
> </body>
> </html>
>
> Results page (includes page generation code):
>
> <?php include('../Connections/website.php'); ?>
> <?php
> $currentPage = $_SERVER['PHP_SELF'];
> $keyword = $_POST['keyword'];
> $maxRows_search = 25;
> $pageNum_search = 0;
> if (isset($_GET['pageNum_search'])) {
> $pageNum_search = $_GET['pageNum_search'];
> }
> $startRow_search = $pageNum_search * $maxRows_search;
>
> mysql_select_db($database_website, $website);
> $query_search = "SELECT DISTINCT docURL as URL, docTitle as Title, docDesc
> as Description, projCode as Project FROM docs, proj WHERE docs.projID =
> proj.projID AND docURL LIKE "%$keyword%" OR docTitle LIKE "%$keyword%"'
> OR docDesc LIKE "%$keyword%"";
> $query_limit_search = sprintf("%s LIMIT %d, %d", $query_search,
> $startRow_search, $maxRows_search);
> $search = mysql_query($query_limit_search, $website) or
> die(mysql_error());
> $row_search = mysql_fetch_assoc($search);
>
> if (isset($_GET['totalRows_search'])) {
> $totalRows_search = $_GET['totalRows_search'];
> } else {
> $all_search = mysql_query($query_search);
> $totalRows_search = mysql_num_rows($all_search);
> }
> $totalPages_search = ceil($totalRows_search/$maxRows_search)-1;
>
> $queryString_search = "";
> if (!empty($_SERVER['QUERY_STRING'])) {
> $params = explode("&", $_SERVER['QUERY_STRING']);
> $newParams = array();
> foreach ($params as $param) {
> if (stristr($param, "pageNum_search") == false &&
> stristr($param, "totalRows_search") == false) {
> array_push($newParams, $param);
> }
> }
> if (count($newParams) != 0) {
> $queryString_search = "&" . htmlentities(implode("&", $newParams));
> }
> }
> $queryString_search = sprintf("&totalRows_search=%d%s", $totalRows_search,
> $queryString_search);
>
> ?>
>
> <!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">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> <title>Untitled Document</title>
> </head>
>
> <body>
> <p>
> <?php do { ?>
> <a href="<?php echo $row_search['URL']; ?>"><?php echo
> $row_search['Title']; ?></a> :<br />
> <strong>Description:</strong> <?php echo $row_search['Description'];
> ?>, <strong>Project:</strong> <?php echo $row_search['Project']; ?><br />
> <?php } while ($row_search = mysql_fetch_assoc($search)); ?>
> </p>
> <p>
> <table border="0" width="50%" align="center">
> <tr>
> <td width="23%" align="center"><?php if ($pageNum_search > 0) { // Show
> if not first page ?>
> <a href="<?php printf("%s?pageNum_search=%d%s", $currentPage, 0,
> $queryString_search); ?>">First</a>
> <?php } // Show if not first page ?>
> </td>
> <td width="31%" align="center"><?php if ($pageNum_search > 0) { // Show
> if not first page ?>
> <a href="<?php printf("%s?pageNum_search=%d%s", $currentPage,
> max(0, $pageNum_search - 1), $queryString_search); ?>">Previous</a>
> <?php } // Show if not first page ?>
> </td>
> <td width="23%" align="center"><?php if ($pageNum_search <
> $totalPages_search) { // Show if not last page ?>
> <a href="<?php printf("%s?pageNum_search=%d%s", $currentPage,
> min($totalPages_search, $pageNum_search + 1), $queryString_search);
> ?>">Next</a>
> <?php } // Show if not last page ?>
> </td>
> <td width="23%" align="center"><?php if ($pageNum_search <
> $totalPages_search) { // Show if not last page ?>
> <a href="<?php printf("%s?pageNum_search=%d%s", $currentPage,
> $totalPages_search, $queryString_search); ?>">Last</a>
> <?php } // Show if not last page ?>
> </td>
> </tr>
> </table>
> </p>
> </body>
> </html>
> <?php
> mysql_free_result($search);
> ?>
>
> "lorento" <laurente1234@yahoo.com> wrote in message
> news:1151406437.726096.182120@p79g2000cwp.googlegroups.com...
>> Chris wrote:
>>> I have tried assigning a variable to the textbox value, but that
>>> doesn't work either.
>> Its because the keyword didn't pass to action page. Try like this :
>>
>> <form id="search" name="search1" method="post" action="search.php">
>> <label>Keywords:
>> <input name="docDesc" type="text" size="30"/>
>> </label>
>> <label>
>> <input type="submit" name="Submit" value="Submit" />
>> </label>
>> </form>
>>
>> --
>> http://blog.deshot.com
>> http://www.deshot.com
>>
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация