PHP order by clicks in WHILE









up vote
0
down vote

favorite












I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.



My code :



$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch())
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '$row['country']' AND `link_id` = '$id'")->rowCount();



I want the foreach give me results from highest count of $clicks to the lowest.



Any ideas what can I do ?










share|improve this question





















  • What foreach? You mean the while loop?
    – Stephen Lake
    Nov 11 at 13:37










  • first of all, you must use $clicks instead of $click and then you will be able to use foreach
    – FatemehNB
    Nov 11 at 13:39














up vote
0
down vote

favorite












I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.



My code :



$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch())
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '$row['country']' AND `link_id` = '$id'")->rowCount();



I want the foreach give me results from highest count of $clicks to the lowest.



Any ideas what can I do ?










share|improve this question





















  • What foreach? You mean the while loop?
    – Stephen Lake
    Nov 11 at 13:37










  • first of all, you must use $clicks instead of $click and then you will be able to use foreach
    – FatemehNB
    Nov 11 at 13:39












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.



My code :



$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch())
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '$row['country']' AND `link_id` = '$id'")->rowCount();



I want the foreach give me results from highest count of $clicks to the lowest.



Any ideas what can I do ?










share|improve this question













I am trying to order in while from higger count of clicks to the lower and I am a bit of lost so I decided to ask the qustion here.



My code :



$stmt = $db->query("SELECT DISTINCT `country` FROM `entries` ORDER by `id` ASC");
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch())
$clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();
$conversations = $db->query("SELECT `id` FROM `conversations` WHERE `country` LIKE '$row['country']' AND `link_id` = '$id'")->rowCount();



I want the foreach give me results from highest count of $clicks to the lowest.



Any ideas what can I do ?







php pdo while-loop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 13:34









Prog AF

125




125











  • What foreach? You mean the while loop?
    – Stephen Lake
    Nov 11 at 13:37










  • first of all, you must use $clicks instead of $click and then you will be able to use foreach
    – FatemehNB
    Nov 11 at 13:39
















  • What foreach? You mean the while loop?
    – Stephen Lake
    Nov 11 at 13:37










  • first of all, you must use $clicks instead of $click and then you will be able to use foreach
    – FatemehNB
    Nov 11 at 13:39















What foreach? You mean the while loop?
– Stephen Lake
Nov 11 at 13:37




What foreach? You mean the while loop?
– Stephen Lake
Nov 11 at 13:37












first of all, you must use $clicks instead of $click and then you will be able to use foreach
– FatemehNB
Nov 11 at 13:39




first of all, you must use $clicks instead of $click and then you will be able to use foreach
– FatemehNB
Nov 11 at 13:39












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You can do this query entirely in SQL with something like this (depending on SQL dialect):



$countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
foreach ($countries as $country)
echo "$country['country'] has $country['clicks'] clicks. ";






share|improve this answer



























    up vote
    0
    down vote













    put number of clicks in an array as below :



     $clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();


    then you can simply sort the array by rsort php function as below :



    echo rsort($clicks);





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249269%2fphp-order-by-clicks-in-while%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      You can do this query entirely in SQL with something like this (depending on SQL dialect):



      $countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
      foreach ($countries as $country)
      echo "$country['country'] has $country['clicks'] clicks. ";






      share|improve this answer
























        up vote
        0
        down vote



        accepted










        You can do this query entirely in SQL with something like this (depending on SQL dialect):



        $countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
        foreach ($countries as $country)
        echo "$country['country'] has $country['clicks'] clicks. ";






        share|improve this answer






















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can do this query entirely in SQL with something like this (depending on SQL dialect):



          $countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
          foreach ($countries as $country)
          echo "$country['country'] has $country['clicks'] clicks. ";






          share|improve this answer












          You can do this query entirely in SQL with something like this (depending on SQL dialect):



          $countries = $db->query("SELECT `country`, count(1) AS clicks FROM `entries` GROUP BY `country` ORDER BY clicks DESC");
          foreach ($countries as $country)
          echo "$country['country'] has $country['clicks'] clicks. ";







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 13:44









          jonasdev

          664




          664






















              up vote
              0
              down vote













              put number of clicks in an array as below :



               $clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();


              then you can simply sort the array by rsort php function as below :



              echo rsort($clicks);





              share|improve this answer
























                up vote
                0
                down vote













                put number of clicks in an array as below :



                 $clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();


                then you can simply sort the array by rsort php function as below :



                echo rsort($clicks);





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  put number of clicks in an array as below :



                   $clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();


                  then you can simply sort the array by rsort php function as below :



                  echo rsort($clicks);





                  share|improve this answer












                  put number of clicks in an array as below :



                   $clicks = $db->query("SELECT `id` FROM `entries` WHERE `country` LIKE '$row['country']'")->rowCount();


                  then you can simply sort the array by rsort php function as below :



                  echo rsort($clicks);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 13:45









                  FatemehNB

                  25126




                  25126



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249269%2fphp-order-by-clicks-in-while%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      Evgeni Malkin