greg Posted April 13, 2011 Share Posted April 13, 2011 Hello everyone, Could be possible someone help me on this code please? I need to group and sort $search_query. They are the keywords extracted from $_SERVER[HTTP_REFERER] (referer in the query). Thank you in advance. $query = ("SELECT referer, browser_ip, date, customers_id, counter, browser_id, browser_language FROM visitors WHERE SUBSTRING(browser_language,1,1) != '[' ORDER by date DESC"); $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $referer = $row[0]; IF($referer) { PREG_MATCH("/[\&\?]q=([^&]*)/", $referer, $matches); $search_query = RAWURLDECODE($matches[1]); $search_query = STR_REPLACE("+", " ", $search_query); } ........................ Quote Link to comment https://forums.phpfreaks.com/topic/233646-i-need-help-grouping-and-sorting/ Share on other sites More sharing options...
btherl Posted April 13, 2011 Share Posted April 13, 2011 By "group and sort" do you mean count how many times each $search_query appears and put them in descending order? Quote Link to comment https://forums.phpfreaks.com/topic/233646-i-need-help-grouping-and-sorting/#findComment-1201310 Share on other sites More sharing options...
greg Posted April 13, 2011 Author Share Posted April 13, 2011 Yes, I need something like this: Apple store (5) Boy t-shirt (56) Girls stuff (45) Php code (5) Windows mobile (1) Where the words are the keywords and the numbers is the count. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/233646-i-need-help-grouping-and-sorting/#findComment-1201313 Share on other sites More sharing options...
btherl Posted April 13, 2011 Share Posted April 13, 2011 Ok, sorted by keyword. You could do this: $keywords = array(); while ($row = mysql_fetch_array($result)) { $referer = $row[0]; IF($referer) { PREG_MATCH("/[\&\?]q=([^&]*)/", $referer, $matches); $search_query = RAWURLDECODE($matches[1]); $search_query = STR_REPLACE("+", " ", $search_query); $keywords[$search_query]++; } } ksort($keywords); foreach ($keywords as $k => $c) { print "$k ($c)<br>"; } You might also want to use strtolower() on the keywords if they don't already have standard capitalization. Quote Link to comment https://forums.phpfreaks.com/topic/233646-i-need-help-grouping-and-sorting/#findComment-1201315 Share on other sites More sharing options...
greg Posted April 13, 2011 Author Share Posted April 13, 2011 This is exactly what I needed! - Thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/233646-i-need-help-grouping-and-sorting/#findComment-1201341 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.