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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

You still fail to let us know if this is a function defined in mysql or php?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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