For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > September 2007 > search script problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author search script problem
mbarbs

2007-08-27, 7:37 pm

I've written my first search script and I have 2 problems. First, I'm
getting duplicate results because I have identical questions related
to different topic_id's (aka categories). Second, if a search term
isn't found, it's
not displaying the "No search results" text on the last line. Any help
would be greatly appreciated.

<?php

$searchTerms = $_POST['search_term']; $searchSection =
$_POST['search_type'];

if($searchSection == 1) {

require_once 'db_connector.php';

$connector = new DB_Connector();

$result = $connector -> query("SELECT * FROM 1_questions WHERE
question LIKE '%$searchTerms%'");

echo '<span class="title">Search Results</span>';
echo '<div class="title_rule"></div>';

while($row = $connector -> fetchArray($result)) {

echo '<div class="question">'.$row['question'].'</div>';
echo nl2br('<div class="answer">'.$row['answer'].'</div>');
echo '<br />';

} }

elseif($searchSection == 2) {

require_once 'db_connector.php';

$connector = new DB_Connector();

$result = $connector -> query("SELECT * FROM 2_questions WHERE
question LIKE '%$searchTerms%'");

echo '<span class="title">Search Results</span>';
echo '<div class="title_rule"></div>';

while($row = $connector -> fetchArray($result)) {

echo '<div class="question">'.$row['question'].'</div>';
echo nl2br('<div class="answer">'.$row['answer'].'</div>');
echo '<br />';

} }

else{

exit('<span class="title">Search Results</span><div
class="title_rule"></div><div class="question">No search resluts
found.</div>'); }

?>

Captain Paralytic

2007-08-28, 4:54 am

On 28 Aug, 00:54, mbarbs <mbarbs.m...@gmail.com> wrote:
> I've written my first search script and I have 2 problems. First, I'm
> getting duplicate results because I have identical questions related
> to different topic_id's (aka categories). Second, if a search term
> isn't found, it's
> not displaying the "No search results" text on the last line. Any help
> would be greatly appreciated.
>
> <?php
>
> $searchTerms = $_POST['search_term']; $searchSection =
> $_POST['search_type'];
>
> if($searchSection == 1) {
>
> require_once 'db_connector.php';
>
> $connector = new DB_Connector();
>
> $result = $connector -> query("SELECT * FROM 1_questions WHERE
> question LIKE '%$searchTerms%'");
>
> echo '<span class="title">Search Results</span>';
> echo '<div class="title_rule"></div>';
>
> while($row = $connector -> fetchArray($result)) {
>
> echo '<div class="question">'.$row['question'].'</div>';
> echo nl2br('<div class="answer">'.$row['answer'].'</div>');
> echo '<br />';
>
> } }
>
> elseif($searchSection == 2) {
>
> require_once 'db_connector.php';
>
> $connector = new DB_Connector();
>
> $result = $connector -> query("SELECT * FROM 2_questions WHERE
> question LIKE '%$searchTerms%'");
>
> echo '<span class="title">Search Results</span>';
> echo '<div class="title_rule"></div>';
>
> while($row = $connector -> fetchArray($result)) {
>
> echo '<div class="question">'.$row['question'].'</div>';
> echo nl2br('<div class="answer">'.$row['answer'].'</div>');
> echo '<br />';
>
> } }
>
> else{
>
> exit('<span class="title">Search Results</span><div
> class="title_rule"></div><div class="question">No search resluts
> found.</div>'); }
>
> ?>


The way I read it, the "No search resluts (SIC) found" text is
triggered by the posted input value of search_type being something
other than 1 or 2. It has nothing to do with finding anything in the
searches themselves.

Tom

2007-08-28, 7:04 pm

On Tue, 28 Aug 2007 02:35:08 -0700, Captain Paralytic wrote...
>
>On 28 Aug, 00:54, mbarbs <mbarbs.m...@gmail.com> wrote:
>
>The way I read it, the "No search resluts (SIC) found" text is
>triggered by the posted input value of search_type being something
>other than 1 or 2. It has nothing to do with finding anything in the
>searches themselves.
>



Depending which Database you are using, such as MySQL or PostreSQL, there's
usually some PHP functions you can find at http://www.php.net, that would help
you get a count on your query results. As an example PosgreSQL would use the
"pg_num_rows()" function whereas MySQL would usually use "mysql_num_rows()".

Tom
--
NewsGuy Takes Usenet Cellular!
Download newsgroup MP3's to your Cell or PDA
Free Trial - http://newsguy.com/cellphone.htm

larry@portcommodore.com

2007-09-09, 7:02 pm

for duplicate in the SQL query use DISTINCT


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com