/    Sign up×
Community /Pin to ProfileBookmark

Hi,

I have my site create but i am missing one thing ? a search bar!

Ok i have no idea on how to create it, i have been doin php for about a year now and my skills are adverage.

I have a database with about 7 tables in and it needs to look through all the tables in that database.

I dont need it to search files or directories just mysql databases. but there is about 7 of them and it needs to display the results in a list

Is there any tutorials or information i could use to help me

Thanks
Adam

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@madddidleyDec 20.2004 — I think you can set your query up like this or something.


$select="select * from table1, table2, table3 where fields='$searchbar'";
Copy linkTweet thisAlerts:
@k0r54authorDec 20.2004 — Ok but what about searchin on more than one field as well.

Also how does it search for txt in the db and not on an exact match?

Then how do i rank it in order of most likely???


Thanks

Adam
Copy linkTweet thisAlerts:
@k0r54authorDec 20.2004 — Put a msg here by mistake!

If any knows how to do what i need or knows of any tutorials please let me know
Copy linkTweet thisAlerts:
@k0r54authorDec 20.2004 — Hi, i have kinda come up with this so far

[code=php]
<?
if($_POST['good']=='yes')
{
echo("<font face="tahoma" size="3" color="green"><b>Results for $words</b></font><br><br>");
$location = ""; // database host (localhost)
$username = ""; // mysql username
$password = ""; //mysql password
$database = ""; //mysql database name
$db_table = ""; // mysql table name to search

$conn = mysql_connect("$location","$username","$password");
if (!$conn) die ("Could not connect MySQL");
mysql_select_db($database,$conn) or die ("Could not open database");

$query = "ALTER TABLE $db_table ORDER BY id DESC";
$result = mysql_query($query); // bad coding yes ;)

$query = "SELECT * FROM $db_table";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){

if(preg_match("/$words/i", $row[Title])) //the column to search
{

$rawid = ($row[Code]);
$rawmonth = ($row[Title]); //yes strange variable names. It works so it doesnt matter.
$rawdate = ($row[Cetegory]); //T just grab extra data to be added with the search tool
$newnews = preg_replace("/$words/i", "<b><font color="red">$words</font></b>", $row[Title]); //the column to search
$display = '<font face="tahoma" size="2"><b><a href="event_details.php#'.$rawid.'">' .$rawmonth . ' ' . $rawdate . ', ' . $rawyear . '</b><br>' . $newnews . '</a></font><br><br>';
echo($display);
$foundcount++;
}
$totalcount++;
}
if(is_null($foundcount)){$foundcount=0;}
echo("<font face="tahoma" size="3" color="green"><b>Found $foundcount entries out of $totalcount</b></font>");
}
?>
[/code]


The problem is that it can only currently search 1 column in 1 table, i need to to search all columns in 1 table?

Also i need a better method of displayin the data i need it to be in order of closes match.

This is rly sketchy codin but any advances will be a great help ?

Thanks

Adam
Copy linkTweet thisAlerts:
@JonaDec 20.2004 — [font=trebuchet ms]You might try using the LIKE clause for wildcard characters.[/font]

[code=php]
$str = isset($_POST["q"])?$_POST["q"]:"";

if($str != ""){
$str = str_replace("*", "%", $str); # allow * wildcard character
$query = mysql_query("SELECT * FROM table WHERE field LIKE '$str'");
if(mysql_num_rows($query) == 0){
echo "<p>Sorry, no results were found for the search term "
.str_replace("%","*",$str).".</p>n";
} else {
while($row = mysql_fetch_assoc($query)){
echo "<p>Found: ".$row['title']."</p>";
}
}
}
[/code]
Copy linkTweet thisAlerts:
@k0r54authorDec 20.2004 — Ok yes that would work better ?

But how would you get it to search more than one field
Copy linkTweet thisAlerts:
@JonaDec 20.2004 — [font=trebuchet ms]Use the [/font][font=courier new]OR[/font][font=trebuchet ms] or [/font][font=courier new]AND[/font][font=trebuchet ms] conditional operators.[/font]

<i>
</i>SELECT * FROM <span><code>table</code></span> WHERE <span><code>field</code></span> LIKE '$term' OR <span><code>field2</code></span> LIKE '$term' AND <span><code>field3</code></span> LIKE '$term'
Copy linkTweet thisAlerts:
@k0r54authorDec 20.2004 — Ok that is nicely tested now ?

How do i get it to order it in relevency?

Thanks

Adam
Copy linkTweet thisAlerts:
@NogDogDec 20.2004 — [i]Originally posted by Jona [/i]

[B][font=trebuchet ms]Use the [/font][font=courier new]OR[/font][font=trebuchet ms] or [/font][font=courier new]AND[/font][font=trebuchet ms] conditional operators.[/font]



<i>
</i>SELECT * FROM <span><code>table</code></span> WHERE <span><code>field</code></span> LIKE '$term' OR <span><code>field2</code></span> LIKE '$term' AND <span><code>field3</code></span> LIKE '$term'
[/B][/QUOTE]

If looking for the term within any part of the searched fields:
<i>
</i>SELECT * FROM <span><code>table</code></span> WHERE <span><code>field</code></span> LIKE '%$term%' OR <span><code>field2</code></span> LIKE '%$term%' OR <span><code>field3</code></span> LIKE '%$term%'
Copy linkTweet thisAlerts:
@JonaDec 20.2004 — [font=trebuchet ms]I should also note that you can use regular expression patterns (to an extent) to the LIKE statement.

To sort by relevancy, you'll need to do some PHP programming (MySQL doesn't have anything built-in for this). Try looking into [url=http://us2.php.net/manual/en/function.similar-text.php]similar_text()[/url].[/font]
Copy linkTweet thisAlerts:
@k0r54authorDec 20.2004 — mmm I dont quite see how this is done?

any1 have any idea on how to use similar text with my outcome?


Thanks

Adam
Copy linkTweet thisAlerts:
@JonaDec 21.2004 — [font=trebuchet ms]Is the documentation really that hard to understand? It should be a matter of simply trying something like:[/font]

[code=php]
$newRows = array();
$i = -1;

while($row = mysql_fetch_assoc($query)){
similar_text($row['field1'], $term, $pcnt);
$newRows[$i++] = array($row['field1'], $pcnt);
}

$newRows = natsort($newRows);
echo "<pre>n", print_r($newRows), "n</pre>";
[/code]
×

Success!

Help @k0r54 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.18,
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,
)...