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); } ........................ 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? 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! 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. 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. 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
Archived
This topic is now archived and is closed to further replies.