/    Sign up×
Community /Pin to ProfileBookmark

I was wondering how i could search through a database to find things by keywords.

For example..
I have a database with a list of items.
Each item has a name and a description.

I want a user to be able to enter a keyword in a form and have the form use that keyword and search through the names and descriptions of the items in the database, then return the names and descriptions containing the keywords in a list on a new page.
And i would like each new item in the list to have a different background color. Like first item would have a light grey background, and the second item would have a dark grey background.

Can anyone help me with this?
Thank you.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMay 02.2004 — here is a simple search preformed on a mysql database:<?php
$key = explode(" ", $_GET['keywords']);
$search = "";

foreach($key as $k)
{
$search .= "(<span><code>name</code></span> LIKE '%$k%' OR <span><code>description</code></span> LIKE '%$k%')";

<i> </i>if(end($key) != $k)
<i> </i>{
<i> </i> $search .= " OR ";
<i> </i>}
}
$link_id = mysql_pconnect($hostname, $username, $password);
mysql_select_db($database, $link_id);
$result = mysql_query("SELECT * FROM <span><code>table</code></span> WHERE $search", $link_id);

while($res = mysql_fetch_array($result, MYSQL_NUM))
{
// process query results...
}
mysql_free_result($result);
?&gt;
Copy linkTweet thisAlerts:
@CSmith1128authorMay 02.2004 — Ummm... Im a little comfused by the last reply.

Could you explain what is going on in the code?

Thanks.
Copy linkTweet thisAlerts:
@ShrineDesignsMay 02.2004 — [b]the search[/b]: $_GET['keywords']

[b]format each search term (word) for sql statement[/b]: foreach($key as $k)

{

$search .= "(name LIKE '%$k%' OR description LIKE '%$k%')";

if(end($key) != $k)
{
$search .= " OR ";
}

}

[b]connect to mysql[/b]: $link_id = mysql_pconnect($hostname, $username, $password);

[b]select the database[/b]: mysql_select_db($database, $link_id);

[b]send query[/b]: $result = mysql_query("SELECT * FROM table WHERE $search", $link_id);

[b]get each row in the result and process[/b]: while($res = mysql_fetch_array($result, MYSQL_NUM))

{

// process query results...

}

[b]frees the sql results memory[/b]: mysql_free_result($result);
Copy linkTweet thisAlerts:
@CSmith1128authorMay 03.2004 — Thanks.. But i still dont quite understand the formatting of each term..

Like...

where is the $key and $k coming from?

Is the $key the keywords?

And what is the if(end($key) != $k) statement doing?

Sorry... Just a little confused.
Copy linkTweet thisAlerts:
@ShrineDesignsMay 03.2004 — normally, you have a form to send a search from, like:&lt;form action="" method="get"&gt;
&lt;input type="text" name="keywords" id="keywords" value=""&gt;
&lt;input type="submit" name="send" id="send" value="Search"&gt;
&lt;/form&gt;
once, the form is submitted you can access those form values via the $_GET, $_POST, or $_REQUEST, as in the form above, to get the value of the keywords value you would do this: <?php $_GET['keywords']; ?>
×

Success!

Help @CSmith1128 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...