Here is a way of doing it with arrays - I don't know enough advanced 
mySQL syntax to see how it could be done just in mySQL:

$resultsArray = array();

while ($results = mysql_fetch_array($article_query))
{
    $art_name = $results["article_name"];
    $art_text = $results["article_text"];
    $q_count = substr_count($art_text, "$q");
    $resultsArray[$q_count] = $art_text;
}

// $resultsArray is now a 2D array of results - order it

arsort($resultsArray);
reset($resultsArray);

// Now display the results

$list = '<ul>';
while (list($name, $score) = each($resultsArray))
{
    $list .= "<li><b>$name</b> - score $score</li>\n";
}
$list .= '</ul>';

echo $list;

Kristjan Kanarik wrote:

>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.
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to