Simple Name Simple Name - 3 months ago 16
MySQL Question

PHP Table With Comma [delete this]

enter image description here

Here i get mos popular hashtags (mos used hashtags) but problem is with comma my query show this:

Popular hashtags:
#q7 (2)
#q9 (2)
#q7 , #q0 (1)
#q0 (1)


I want this:

Popular hashtags:
#q7 (3 times )
#q9 (2 times)
#q0 (2 times)


my code:

$query_result = mysqli_query($Connection, "select hashtag, count(hashtag) as cnt
from tricks
group by hashtag
order by cnt desc limit 5");

$results = array();
while($row = mysqli_fetch_assoc($query_result)) {
$results[] = "{$row['hashtag']} ({$row['cnt']})";
}
echo "Popular hashtags: </br>", implode("</br>", $results);

Answer

Php solution:

$query_result = mysqli_query($Connection, 
                  "SELECT hashtag FROM tricks");             

$popularHashtags = array();

while($row = mysqli_fetch_assoc($query_result)) {

  $hashtags = explode(',', $row['hashtag']);

  foreach($hashtags as $hashtag) {
      $hashtag = trim($hashtag);

      if (!empty($hashtag)) {

        if (!isset($popularHashtags[$hashtag])) {
          $popularHashtags[$hashtag] = 0;
        }

        $popularHashtags[$hashtag]++;
      } 
  }
}


arsort($popularHashtags);

// for showing popular hashtags

echo "Popular hashtags: <br />";

foreach($popularHashtags as $hashtag => $total) {
  echo $hashtag . " ( " . $total . " ) <br />";
}