Jump to content

I need help grouping and sorting


greg

Recommended Posts

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

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.

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.