virruss2 Posted August 6, 2008 Share Posted August 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
trq Posted August 6, 2008 Share Posted August 6, 2008 Can I put this function directly into the query? If its a mysql function yes. Quote Link to comment Share on other sites More sharing options...
papaface Posted August 6, 2008 Share Posted August 6, 2008 You can do it like this: <?php function qscore($str) { return "HELLO!"; } $select = "SELECT * FROM `webpages` ORDER BY ".qscore(`id`)." DESC"; echo $select; ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 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. >_> Quote Link to comment Share on other sites More sharing options...
papaface Posted August 6, 2008 Share Posted August 6, 2008 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.... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 No it's not...not using a PHP function at least (in the sense that the thread starter is talking about. Obviously other functions are usable, but not to separate MySQL results, just to modify the query....) Quote Link to comment Share on other sites More sharing options...
papaface Posted August 6, 2008 Share Posted August 6, 2008 We're talking about two separate things. You are talking about manipulating the mysql results. I am talking about constructing SQL syntax using a query that returns something to build the syntax. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Yeah, but that's not what the thread starter is trying to do. Quote Link to comment Share on other sites More sharing options...
virruss2 Posted August 6, 2008 Author Share Posted August 6, 2008 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.... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
trq Posted August 6, 2008 Share Posted August 6, 2008 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? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 I'd bet it was a PHP function otherwise it would have worked the way he had it. o-O Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.