0

I have a table where each row tracks details about when a link was clicked. This query shows the top 5 pages that the user was on when they clicked the link:

$pop_pages = ("SELECT url FROM clicks WHERE link = '$link_id' GROUP BY url ORDER BY url ASC LIMIT 5");

foreach($pop_pages as $page) {

$page_string = $page_string."<li>".$page->url."</li>";

}

This will make a list of the top 5 most frequently clicked URL's. How do I add a count for how many times it was clicked in this query?

Something like:

$page_string = $page_string."<li>".$page->url." (X clicks)</li>";
1
  • please improve your question it is really difficult to understand.. What's the difference between url and link in your query??
    – nischayn22
    Jun 3, 2012 at 19:00

2 Answers 2

0

Try this:

$pop_pages = ("SELECT url, count(url) as c FROM clicks WHERE link = '$link_id' GROUP BY url ORDER BY url ASC LIMIT 5");

foreach($pop_pages as $page) {

$page_string = $page_string."<li>".$page->url." (".$page->c." clicks)</li>";

}
0
0

You could do something like this:

$pop_pages = ("SELECT url, COUNT(*) as count FROM clicks WHERE link = '$link_id' GROUP BY url ORDER BY url ASC LIMIT 5");

foreach($pop_pages as $page) {

  $page_string = $page_string."<li>".$page->url." (".$page->count." clicks)</li>";

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.