Page 1 of 1

help sorting data ... using PHP and/or MySQL

Posted: Wed Jul 25, 2007 3:57 pm
by shadow8807
This may sound absolutely impossible but I figured Id rather ask.

Basically I need to write a PHP function/algorithm that displays data from a table and order it very specifically. There are many fields and the table has about 200 total entries. Only certain pieces of it will be displayed at a time but there needs to be a specific order to how its displayed. If it was just alphabetically or asc/desc it doesn't work.

Is there any way to order it according to an array? I have no idea writing a complicated function I just am not sure where I would find the resources to figure it out.

Any ideas :?:

Posted: Wed Jul 25, 2007 4:08 pm
by superdezign
I just recently wrote a search engine with a relevance algorithm. It's really not that difficult (sorting isn't... coming up with the exact rules for relevance is).

First you get your results, then store each result into two arrays. I wrote a class to handle the entire search, so it differentiated between which fields counted as data and which fields counted towards the relevance.

Code: Select all

$sortMe = array();
$relev = array();
while($data = mysql_fetch_object($result))
{
    $index = sizeof($sortMe);
    foreach($data as $name => $value)
    {
        if($name is regular data)
            $sortMe[$index] = $value;
        if($name is data that has to do with relevance)
            $relev[$index] = $value;
    }
}
Then, you sort the relevance array according to relevance, and then save the unsorted data according to the sorted relevance array's index order.

Code: Select all

$finalData = array();
foreach($relev as $index => $value)
{
    $finalData[] = $sortMe[$index];
}