Jump to content

Recommended Posts

This is driving me nuts...

 

So I have a database which contains some double barreled names and I've been trying to optimize the search. To make it worse, some of them have hyphens and some just a space. Some have both. And, of course, there are also entries that are not double barrelled.

 

For example, I have an entry called Arias-Stella reaction. Ideally, I would like the user to arrive at it by typing any of the following inthe search:

Arias-Stella reaction

Arias Stella reaction

Arias-Stella

Arias Stella

Arias

Stella etc.

 

Here's what I've done so far (assuming each entry consists of no more than three words separated by hyphens ans/or spaces):

 

<?php

$q=mysqli_real_escape_string ($dbcnx, $_GET['q']);

$t = trim($q);

$t1 = explode("-", $t);

$t2 = explode(" ", $t1[0]);

$t3 = explode(" ", $t1[1]);

echo "$t2[0]<br>$t2[1]<br>$t3[0]<br>$t3[1]";

$result = mysqli_query ($dbcnx, "SELECT * FROM main WHERE name LIKE '%$t2[0]%' OR name LIKE '%$t2[1]' OR name LIKE '%$t3[0]' OR name LIKE '%$t3[1]' ORDER BY name");

?>

 

There's an obvious problem with this approach: unused variables are assigned NULL value. If even one of the four search parameters is unused, the search returns the entire content of the database which kinda defies the point.

 

Is there a simpler way around it? Am I sweating too much over it? If there's no simpler solution I'll just explode the hyphens and use the first array element as a search parameter.

 

Using AND instead of OR doesn't help either since the end number of array elements varies from search to search.

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.