Jump to content

Recommended Posts

Hi,

I have a search form for my website, it is meant to search 3 different options and get you matching results, so you can search for a specific country (or all countries) a specific type of article (or all articles) and a keyword if you choose to put one in. All options also search to columns in the databse.

 

The problem is that the first two work but when you put a keyword in it doesn't find any results.

 

The script is,

<?php

$targetb = $_POST['menuFilesDMA'];
$targetb = str_replace(' ','_', $targetb);

mysql_connect("localhost","agder","cligdfd"); 

mysql_select_db("real") or die("Unable to select database"); 

$targetb = mysql_real_escape_string($targetb);

$type = mysql_real_escape_string($_POST['Type']);

$keywords = preg_split("/[\s,]+/", trim($_POST['keyword']));

$sql = "SELECT * FROM items WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') AND (Abstract LIKE '%$keyword%' OR town LIKE '%$keyword%') ORDER BY id DESC";

$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "<img src=http://www.myroho.com/sorry_no_match.png>";
} else {
while($row = mysql_fetch_array($result)) {
$Country = $row['country']; 
$Type = $row['type'];
$More = $row['id'];
$Title = $row['Title'];
$Abs = $row['Abstract'];
$Auth = $row['town'];
echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
  <tr>
    <td width=\"14\" height=\"28\" background=\"line_left_corner_top.png\"> </td>
    <td height=\"28\" colspan=\"4\" background=\"line_top.png\"> </td>
    <td height=\"28\" background=\"line_right_corner_top.png\"> </td>
  </tr>
  <tr>
    <td width=\"14\" rowspan=\"3\" background=\"line_left.png\"> </td>
    <td width=\"250\" height=\"21\"><span class=\"style42\">$Title</span></td>
    <td width=\"154\" height=\"21\"> </td>
    <td width=\"14\"> </td>
    <td width=\"128\" height=\"128\" rowspan=\"3\"><img src=\"/searchthumbs/$Country$Type.png\" width=\"128\" height=\"128\" /></td>
    <td width=\"14\" rowspan=\"3\" background=\"line_right.png\"> </td>
  </tr>
  <tr>
    <td height=\"86\" colspan=\"2\"><span class=\"style41\">$Abs</span></td>
    <td width=\"14\"> </td>
  </tr>
  <tr>
    <td width=\"250\" height=\"19\"><span class=\"style43\">$Auth</span></td>
    <td width=\"154\" height=\"19\" align=\"left\" valign=\"bottom\"><span class=\"style39\">>></span> <span class=\"style40\"><a href=more.php?id=$More>Read More</a> </span> <span class=\"style39\">>></span></td>
    <td width=\"14\"> </td>
  </tr>
  <tr>
    <td width=\"14\" height=\"19\" background=\"line_left_corner.png\"> </td>
    <td height=\"28\" colspan=\"4\" background=\"line_base.png\"> </td>
    <td width=\"14\" height=\"19\" background=\"line_right_corner.png\"> </td>
  </tr>
   <tr>
    <td width=\"14\" height=\"19\"> </td>
    <td height=\"28\" colspan=\"4\"> </td>
    <td width=\"14\" height=\"19\"> </td>
  </tr>
</table>";

}
}
?>

 

If anyone can help it would be very much appriciated,

 

Thank you,

Colin

Link to comment
https://forums.phpfreaks.com/topic/70962-solved-help-searching/
Share on other sites

keywords is an array not a string

 

try this

 

$keywords = preg_split("/[\s,]+/", trim($_POST['keyword']));
$keys = "";
foreach($keywords as $K)
{
$keys .= "AND (Abstract LIKE '%$K%' OR town LIKE '%$K%')";
}

$sql = "SELECT * FROM items WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') $keys ORDER BY id DESC";

 

its a quick untested draft

Link to comment
https://forums.phpfreaks.com/topic/70962-solved-help-searching/#findComment-356759
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.