Jump to content

Can I use a function directly in the query?


virruss2

Recommended Posts

Hi,

 

I've created an algorithm to determine the quality of some webpages on my website based on the number of comments, traffic, number of words in that article, user ratings and others and now I need to sort the webpages by this quality score. But this algorithm uses other queries to search other tables to get the number of comments, visitors... etc.

 

Can I put this function directly into the query?

 

What I mean is something like this (where qscore() is the function):

 

$select = mysql_query("SELECT * 
FROM `webpages` 
ORDER BY qscore(`id`) DESC
");

 

If not, what should I do to get the same result?

 

Thank you.

Papaface......what?  That would do nothing at all....read the query and see if it makes sense.

 

Anyway, you could just execute the select and then use like usort() with a custom function that checks the qscore() and orders the result array. >_>

Papaface......what?  That would do nothing at all....read the query and see if it makes sense.

 

Anyway, you could just execute the select and then use like usort() with a custom function that checks the qscore() and orders the result array. >_>

My example wouldn't. But it was simply illustrating the theory! It IS possible. Technically if you had a field name called "HELLO!" it would work....

Oh, sorry guys for the problems created here...

 

I wasn't talking about creating a query using a function... I was talking about actually applying the function to the column data and to order by the returning values...

 

I guess I'm going to try usort()... Hope to figure it out...

 

Thanks.

 

Later edit: If it's not very complicated, can you help me with how my query should look like? I don't understand much from the documentation of usort() at php.net.... :(

Query should just be:

$select = mysql_query("SELECT *

FROM `webpages`

");

 

Then you can assemble everything into an array:

while ($row = mysql_fetch_assoc($select)) {

        $data[] = $row;

}

 

Then create a usort function:

function sort_qscore($data1, $data2) {

        if (qscore($data1['id']) > qscore($data2['id'])) {

              return 1;

        }

        if (qscore($data1['id']) == qscore($data2['id'])) {

              return 0;

        }

        if (qscore($data1['id']) < qscore($data2['id'])) {

              return -1;

        }

}

 

Then just use usort on $data.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.