|
Posted by J.O. Aho on 06/13/06 16:58
Chris wrote:
> Thanks,
>
> The page will show, and I can get a result. However I now have other issues
> with it. Apparently others have some of the same issues, such as duplicate
> results, so I'm checking out those solutions around the Net. This search
> code has apparently been available for quite a while, so may be based on an
> earlier version of PHP.
I wouldn't have done my code the way as it's in the page you have and to avoid
duplicates you shouldn't use PHP filtering but tell the SQL server to give you
distinct results (in the first place you should make the database to not
accept duplicates and you never will have this trouble).
Think the following sql query would fix the trouble:
SELECT DISTINCT(*) FROM links WHERE pageURL LIKE '%$trimm%' OR
pageDesc LIKE '%$trimm%' OR pageTitle LIKE '%$trimm%' ORDER BY pageLink
DESC
Here is the code I don't like:
$row= mysql_fetch_array ($numresults);
//store record id of every item that contains the keyword in the array
we need to do this to avoid display of duplicate search result.
do{
//EDIT HERE and specify your field name that is primary key
$adid_array[] = $row[ 'pageID' ];
}while( $row= mysql_fetch_array($numresults));
} //end foreach
Here is how I would have done the same:
while($row= mysql_fetch_array($numresults)) {
$adid_array[] = $row[ 'pageID' ];
}
> Also, it doesn't seem to have anything in the code
> to make the results appear as a link (i.e. <a href>, etc.) so will have to
> figure out where that goes.
Think you can modify the
<?php echo $linkhigh; ?><br>
to include the anchor tag, as I'm not sure what data is stored where in the
table, I'm not completely sure and in the first place I wouldn't use a load of
preg_replace() when you can use a simple string to make all this.
> Anyway, now I get an error in the search text box where the search
> keyword(s) are supposed to be after you run the search. It says:
>
> br /><b>Notice</b>: Undefined variable: q in
> <b>c:\Inetpub\wwwroot\search.php</b> on line <b>85</b><br />
>
> I thought it had to do with making it a 'sticky form', so the keyword will
> stay in the box, but that perhaps this code was written when global
> variables were on by default. The only place where the code refers to a $q
> variable is in the form, and they also name the text box 'q'.
>
> <form action="search.php" method="get" name="search">
> <div align="center">
> <input name="q" type="text" value=" <?php echo $q; ?> " size="15">
> <input name="search" type="submit" value="Search">
> </div>
> </form>
>
> So I tried to make it a sticky form using:
>
> <input name="q" type="text" value=" <?php if (isset($_POST['q'])) echo
> $_POST['q']; ?> " size="15">
The from uses GET and not POST, which makes $_POST['q'] always be unset,
either use $_GET['q'] or $_REQUEST['q'] (the later don't care if it comes from
get, post or cookie).
//Aho
Navigation:
[Reply to this message]
|