Array or SQL?
| From: | Kristjan Kanarik | Date: | Thu, 28 Feb 2002 10:10:54 +0000 |
| Subject: | Array or SQL? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-86558@lists.php.net to get a copy of this message | ||
Hi,
I've never had reason to get deeply into array functions, but now it is
very likely needed. I'm working on a small search engine (to search
text from articles) and I'm not sure wheter I will be able to complete it
without any external help.
Here is a snip of code:
/* I'm using MySQL 3.23.37 together with 4.0.6 on a FreeBSD box */
$article_query = mysql_query("select article_name, article_text from
articles where article_name LIKE '%$q%'") or die (mysql_error());
/* $q is the search query with a space added in front */
while ($results = mysql_fetch_array($article_query))
{
$art_name = $results["article_name"];
$art_text = $results["article_text"];
$q_count = substr_count($art_text, "$q");
}
</end_of_code>
What I'd like to do is to get the results displayed and ORDERED by q_count
- this number shows how many times the search query accoures in the
article text. Now, this could probably be done in two ways:
1) redefining the SQL query to something like this:
article_query = mysql_query("select article_name, article_text from
articles where article_name LIKE '%$q%' ORDER BY $q ACCOURANCE IN
ARTICLE_TEXT DESC") or die (mysql_error());
What I am not sure of is how to write the 'ORDER BY $q ACCOURANCE IN
ARTICLE_TEXT DESC' part.
2) building a two-dimensional array (one dimension is the $art_name and
$art_text, another the $q_count), sort it by $q_count and then display the
members of this array.
I feel very uncomfortable both with array's and complicated SQL queries,
therefore I wonder if somebody could show me the way? The solution letting
MySQL to do the job is probably faster and therefore prefered... I guess.
Or not?
TIA,
Kristjan
P.S. Pls. CC me as well, I am only on the digest.