/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Search Function

I have a form with about 10 fields that a user would fill out to refine their search of a single table in the database. I think I understand how I’d find an exact match for what is input into the form, but is there a way to create a search which would give the most exact match followed by the 2nd most relevant match, etc…, etc…

So maybe the first result would be an exact match of all 10 fields while the 2nd result would be 8 fields with a match, the 3rd would maybe be 4 fields, etc…

?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@LaffinTooJan 06.2012 — What you are looking for is the relevancy of a search.

U can gain a lot of information from the mysql manual online: http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
Copy linkTweet thisAlerts:
@NtrimgsauthorJan 06.2012 — I took a look at that and it doesn't seem to take into account multiple field matches...maybe I'm wrong though. But what I envisioned was the following HTML form that the user would input their search into:

[code=html]
<form action="self.php" method="post">
<input type="text" name="hair_color" />
<input type="text" name="height" />
<input type="text" name="age" />
</form>
[/code]


And then it would search a table called "users" which would have the fields: hair_color, height, age, etc...

It would then search that table's individual fields with the form fields input and come up with the most relevant searches.
Copy linkTweet thisAlerts:
@LaffinTooJan 07.2012 — than your talking about building a new field with a calculated value
<i>
</i>SELECT id,(if(hair_color='black',1,0) + if(height='5.0',1,0) + if(age=30,1,0) as score FROM users ORDER BY score DESC

Should do the trick
Copy linkTweet thisAlerts:
@NtrimgsauthorJan 07.2012 — than your talking about building a new field with a calculated value
<i>
</i>SELECT id,(if(hair_color='black',1,0) + if(height='5.0',1,0) + if(age=30,1,0) as score FROM users ORDER BY score DESC

Should do the trick[/QUOTE]


Could you tell me what the 1,0 mean in the conditional statements? For instance (hair_color='black',1,0)
Copy linkTweet thisAlerts:
@LaffinTooJan 07.2012 — if(expression,true value,false value)
Copy linkTweet thisAlerts:
@NtrimgsauthorJan 07.2012 — if(expression,true value,false value)[/QUOTE]

Just so I understand this, if those true and false values weren't present then it would have no way to rank the results? Or are they there for a different reason?
×

Success!

Help @Ntrimgs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...