how to count students studying same branch,year,semester using loops in php











up vote
-3
down vote

favorite












    <?php
$link = mysqli_connect("localhost", "root", "", "college");

if (mysqli_connect_errno()) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}

$query = "SELECT branch,year,semester FROM students ORDER by branch,year,semester";
$result = mysqli_query($link,$query);
$rt= mysqli_fetch_all($result,MYSQLI_ASSOC);

$depCount = ;

foreach($rt as $s) {
if ($depCount["$s['branch']-$s['year']-$s['semester']-"])
{
$depCount["$s['branch']-$s['year']-$s['semester']-"]++;
} else {
$depCount["$s['branch']-$s['year']-$s['semester']-"] = 1;
}
}
var_dump($depCount);

?>


This is my ordered by 'branch year semester' table




name branch year semester
name1 cse 1 1
name9 cse 1 1
name13 cse 1 1
name5 cse 1 1
name26 cse 2 1
name6 cse 2 2
name10 cse 2 2
name4 cse 2 4
name2 cse 3 2
name8 cse 3 3
name11 cse 3 3
name3 cse 4 3
name8 cse 4 4
name12 cse 4 4
name20 ece 1 4
name25 ece 1 4
name14 ece 2 2
name21 ece 2 2
name17 ece 2 3
name18 ece 3 1
name22 ece 3 3
name15 ece 3 3
name19 ece 4 2
name24 ece 4 2
name23 ece 4 4
name16 ece 4 4
name42 eee 1 1
name34 eee 1 4
name29 eee 2 1
name43 eee 2 2


Now i want output like




cse 1 1 count 4
cse 2 1 count 1
cse 2 2 count 2
so on...


I want to display count of students studying same 'branch,year,semester'. usings loops in php.



database students table consists of n number of records










share|improve this question




























    up vote
    -3
    down vote

    favorite












        <?php
    $link = mysqli_connect("localhost", "root", "", "college");

    if (mysqli_connect_errno()) {
    printf("Connect failed: %sn", mysqli_connect_error());
    exit();
    }

    $query = "SELECT branch,year,semester FROM students ORDER by branch,year,semester";
    $result = mysqli_query($link,$query);
    $rt= mysqli_fetch_all($result,MYSQLI_ASSOC);

    $depCount = ;

    foreach($rt as $s) {
    if ($depCount["$s['branch']-$s['year']-$s['semester']-"])
    {
    $depCount["$s['branch']-$s['year']-$s['semester']-"]++;
    } else {
    $depCount["$s['branch']-$s['year']-$s['semester']-"] = 1;
    }
    }
    var_dump($depCount);

    ?>


    This is my ordered by 'branch year semester' table




    name branch year semester
    name1 cse 1 1
    name9 cse 1 1
    name13 cse 1 1
    name5 cse 1 1
    name26 cse 2 1
    name6 cse 2 2
    name10 cse 2 2
    name4 cse 2 4
    name2 cse 3 2
    name8 cse 3 3
    name11 cse 3 3
    name3 cse 4 3
    name8 cse 4 4
    name12 cse 4 4
    name20 ece 1 4
    name25 ece 1 4
    name14 ece 2 2
    name21 ece 2 2
    name17 ece 2 3
    name18 ece 3 1
    name22 ece 3 3
    name15 ece 3 3
    name19 ece 4 2
    name24 ece 4 2
    name23 ece 4 4
    name16 ece 4 4
    name42 eee 1 1
    name34 eee 1 4
    name29 eee 2 1
    name43 eee 2 2


    Now i want output like




    cse 1 1 count 4
    cse 2 1 count 1
    cse 2 2 count 2
    so on...


    I want to display count of students studying same 'branch,year,semester'. usings loops in php.



    database students table consists of n number of records










    share|improve this question


























      up vote
      -3
      down vote

      favorite









      up vote
      -3
      down vote

      favorite











          <?php
      $link = mysqli_connect("localhost", "root", "", "college");

      if (mysqli_connect_errno()) {
      printf("Connect failed: %sn", mysqli_connect_error());
      exit();
      }

      $query = "SELECT branch,year,semester FROM students ORDER by branch,year,semester";
      $result = mysqli_query($link,$query);
      $rt= mysqli_fetch_all($result,MYSQLI_ASSOC);

      $depCount = ;

      foreach($rt as $s) {
      if ($depCount["$s['branch']-$s['year']-$s['semester']-"])
      {
      $depCount["$s['branch']-$s['year']-$s['semester']-"]++;
      } else {
      $depCount["$s['branch']-$s['year']-$s['semester']-"] = 1;
      }
      }
      var_dump($depCount);

      ?>


      This is my ordered by 'branch year semester' table




      name branch year semester
      name1 cse 1 1
      name9 cse 1 1
      name13 cse 1 1
      name5 cse 1 1
      name26 cse 2 1
      name6 cse 2 2
      name10 cse 2 2
      name4 cse 2 4
      name2 cse 3 2
      name8 cse 3 3
      name11 cse 3 3
      name3 cse 4 3
      name8 cse 4 4
      name12 cse 4 4
      name20 ece 1 4
      name25 ece 1 4
      name14 ece 2 2
      name21 ece 2 2
      name17 ece 2 3
      name18 ece 3 1
      name22 ece 3 3
      name15 ece 3 3
      name19 ece 4 2
      name24 ece 4 2
      name23 ece 4 4
      name16 ece 4 4
      name42 eee 1 1
      name34 eee 1 4
      name29 eee 2 1
      name43 eee 2 2


      Now i want output like




      cse 1 1 count 4
      cse 2 1 count 1
      cse 2 2 count 2
      so on...


      I want to display count of students studying same 'branch,year,semester'. usings loops in php.



      database students table consists of n number of records










      share|improve this question















          <?php
      $link = mysqli_connect("localhost", "root", "", "college");

      if (mysqli_connect_errno()) {
      printf("Connect failed: %sn", mysqli_connect_error());
      exit();
      }

      $query = "SELECT branch,year,semester FROM students ORDER by branch,year,semester";
      $result = mysqli_query($link,$query);
      $rt= mysqli_fetch_all($result,MYSQLI_ASSOC);

      $depCount = ;

      foreach($rt as $s) {
      if ($depCount["$s['branch']-$s['year']-$s['semester']-"])
      {
      $depCount["$s['branch']-$s['year']-$s['semester']-"]++;
      } else {
      $depCount["$s['branch']-$s['year']-$s['semester']-"] = 1;
      }
      }
      var_dump($depCount);

      ?>


      This is my ordered by 'branch year semester' table




      name branch year semester
      name1 cse 1 1
      name9 cse 1 1
      name13 cse 1 1
      name5 cse 1 1
      name26 cse 2 1
      name6 cse 2 2
      name10 cse 2 2
      name4 cse 2 4
      name2 cse 3 2
      name8 cse 3 3
      name11 cse 3 3
      name3 cse 4 3
      name8 cse 4 4
      name12 cse 4 4
      name20 ece 1 4
      name25 ece 1 4
      name14 ece 2 2
      name21 ece 2 2
      name17 ece 2 3
      name18 ece 3 1
      name22 ece 3 3
      name15 ece 3 3
      name19 ece 4 2
      name24 ece 4 2
      name23 ece 4 4
      name16 ece 4 4
      name42 eee 1 1
      name34 eee 1 4
      name29 eee 2 1
      name43 eee 2 2


      Now i want output like




      cse 1 1 count 4
      cse 2 1 count 1
      cse 2 2 count 2
      so on...


      I want to display count of students studying same 'branch,year,semester'. usings loops in php.



      database students table consists of n number of records







      php mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 11:44

























      asked Nov 8 at 11:14









      Mahesh Ramineedi

      32




      32
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          <?php

          $students[0] = array("name" => 'a', "branch" => 'x', "semester" => 1, "year" => 1);
          $students[1] = array("name" => 'b', "branch" => 'y', "semester" => 2, "year" => 1);
          $students[2] = array("name" => 'c', "branch" => 'y', "semester" => 2, "year" => 1);

          $depCount = ;

          foreach ($students as $s) {
          $key = $s["branch"] . '-' . $s["year"] . '-' . $s["semester"];
          if (isset($depCount[$key])) {
          $depCount[$key]++;
          } else {
          $depCount[$key] = 1;
          }
          }

          foreach ($depCount as $k => $d) {
          $cat = explode('-', $k);
          echo $cat[0] . ' ' . $cat[1] . ' ' . $cat[2] . ' count ' . $d . '<br>';
          }





          share|improve this answer























          • not working bro...check my updated code
            – Mahesh Ramineedi
            Nov 8 at 11:43












          • try updated code
            – Sa Si Kumar
            Nov 8 at 12:04










          • thanks a ton bro...its working...
            – Mahesh Ramineedi
            Nov 8 at 12:25


















          up vote
          1
          down vote













          Is there a specific reason why you don't want to use a query?



          SELECT branch, year,semester, COUNT(*) total FROM students GROUP BY branch,year,semester;
          +--------+------+----------+-------+
          | branch | year | semester | total |
          +--------+------+----------+-------+
          | cse | 1 | 1 | 4 |
          | cse | 2 | 1 | 1 |
          | cse | 2 | 2 | 2 |
          | cse | 2 | 4 | 1 |
          | cse | 3 | 2 | 1 |
          | cse | 3 | 3 | 2 |
          | cse | 4 | 3 | 1 |
          | cse | 4 | 4 | 2 |
          | ece | 1 | 4 | 2 |
          | ece | 2 | 2 | 2 |
          | ece | 2 | 3 | 1 |
          | ece | 3 | 1 | 1 |
          | ece | 3 | 3 | 2 |
          | ece | 4 | 2 | 2 |
          | ece | 4 | 4 | 2 |
          | eee | 1 | 1 | 1 |
          | eee | 1 | 4 | 1 |
          | eee | 2 | 1 | 1 |
          | eee | 2 | 2 | 1 |
          +--------+------+----------+-------+





          share|improve this answer





















          • i know its easy with query...but my mentor want to use only loops
            – Mahesh Ramineedi
            Nov 8 at 12:15











          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%2f53206616%2fhow-to-count-students-studying-same-branch-year-semester-using-loops-in-php%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










          <?php

          $students[0] = array("name" => 'a', "branch" => 'x', "semester" => 1, "year" => 1);
          $students[1] = array("name" => 'b', "branch" => 'y', "semester" => 2, "year" => 1);
          $students[2] = array("name" => 'c', "branch" => 'y', "semester" => 2, "year" => 1);

          $depCount = ;

          foreach ($students as $s) {
          $key = $s["branch"] . '-' . $s["year"] . '-' . $s["semester"];
          if (isset($depCount[$key])) {
          $depCount[$key]++;
          } else {
          $depCount[$key] = 1;
          }
          }

          foreach ($depCount as $k => $d) {
          $cat = explode('-', $k);
          echo $cat[0] . ' ' . $cat[1] . ' ' . $cat[2] . ' count ' . $d . '<br>';
          }





          share|improve this answer























          • not working bro...check my updated code
            – Mahesh Ramineedi
            Nov 8 at 11:43












          • try updated code
            – Sa Si Kumar
            Nov 8 at 12:04










          • thanks a ton bro...its working...
            – Mahesh Ramineedi
            Nov 8 at 12:25















          up vote
          0
          down vote



          accepted










          <?php

          $students[0] = array("name" => 'a', "branch" => 'x', "semester" => 1, "year" => 1);
          $students[1] = array("name" => 'b', "branch" => 'y', "semester" => 2, "year" => 1);
          $students[2] = array("name" => 'c', "branch" => 'y', "semester" => 2, "year" => 1);

          $depCount = ;

          foreach ($students as $s) {
          $key = $s["branch"] . '-' . $s["year"] . '-' . $s["semester"];
          if (isset($depCount[$key])) {
          $depCount[$key]++;
          } else {
          $depCount[$key] = 1;
          }
          }

          foreach ($depCount as $k => $d) {
          $cat = explode('-', $k);
          echo $cat[0] . ' ' . $cat[1] . ' ' . $cat[2] . ' count ' . $d . '<br>';
          }





          share|improve this answer























          • not working bro...check my updated code
            – Mahesh Ramineedi
            Nov 8 at 11:43












          • try updated code
            – Sa Si Kumar
            Nov 8 at 12:04










          • thanks a ton bro...its working...
            – Mahesh Ramineedi
            Nov 8 at 12:25













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          <?php

          $students[0] = array("name" => 'a', "branch" => 'x', "semester" => 1, "year" => 1);
          $students[1] = array("name" => 'b', "branch" => 'y', "semester" => 2, "year" => 1);
          $students[2] = array("name" => 'c', "branch" => 'y', "semester" => 2, "year" => 1);

          $depCount = ;

          foreach ($students as $s) {
          $key = $s["branch"] . '-' . $s["year"] . '-' . $s["semester"];
          if (isset($depCount[$key])) {
          $depCount[$key]++;
          } else {
          $depCount[$key] = 1;
          }
          }

          foreach ($depCount as $k => $d) {
          $cat = explode('-', $k);
          echo $cat[0] . ' ' . $cat[1] . ' ' . $cat[2] . ' count ' . $d . '<br>';
          }





          share|improve this answer














          <?php

          $students[0] = array("name" => 'a', "branch" => 'x', "semester" => 1, "year" => 1);
          $students[1] = array("name" => 'b', "branch" => 'y', "semester" => 2, "year" => 1);
          $students[2] = array("name" => 'c', "branch" => 'y', "semester" => 2, "year" => 1);

          $depCount = ;

          foreach ($students as $s) {
          $key = $s["branch"] . '-' . $s["year"] . '-' . $s["semester"];
          if (isset($depCount[$key])) {
          $depCount[$key]++;
          } else {
          $depCount[$key] = 1;
          }
          }

          foreach ($depCount as $k => $d) {
          $cat = explode('-', $k);
          echo $cat[0] . ' ' . $cat[1] . ' ' . $cat[2] . ' count ' . $d . '<br>';
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 12:03

























          answered Nov 8 at 11:27









          Sa Si Kumar

          518412




          518412












          • not working bro...check my updated code
            – Mahesh Ramineedi
            Nov 8 at 11:43












          • try updated code
            – Sa Si Kumar
            Nov 8 at 12:04










          • thanks a ton bro...its working...
            – Mahesh Ramineedi
            Nov 8 at 12:25


















          • not working bro...check my updated code
            – Mahesh Ramineedi
            Nov 8 at 11:43












          • try updated code
            – Sa Si Kumar
            Nov 8 at 12:04










          • thanks a ton bro...its working...
            – Mahesh Ramineedi
            Nov 8 at 12:25
















          not working bro...check my updated code
          – Mahesh Ramineedi
          Nov 8 at 11:43






          not working bro...check my updated code
          – Mahesh Ramineedi
          Nov 8 at 11:43














          try updated code
          – Sa Si Kumar
          Nov 8 at 12:04




          try updated code
          – Sa Si Kumar
          Nov 8 at 12:04












          thanks a ton bro...its working...
          – Mahesh Ramineedi
          Nov 8 at 12:25




          thanks a ton bro...its working...
          – Mahesh Ramineedi
          Nov 8 at 12:25












          up vote
          1
          down vote













          Is there a specific reason why you don't want to use a query?



          SELECT branch, year,semester, COUNT(*) total FROM students GROUP BY branch,year,semester;
          +--------+------+----------+-------+
          | branch | year | semester | total |
          +--------+------+----------+-------+
          | cse | 1 | 1 | 4 |
          | cse | 2 | 1 | 1 |
          | cse | 2 | 2 | 2 |
          | cse | 2 | 4 | 1 |
          | cse | 3 | 2 | 1 |
          | cse | 3 | 3 | 2 |
          | cse | 4 | 3 | 1 |
          | cse | 4 | 4 | 2 |
          | ece | 1 | 4 | 2 |
          | ece | 2 | 2 | 2 |
          | ece | 2 | 3 | 1 |
          | ece | 3 | 1 | 1 |
          | ece | 3 | 3 | 2 |
          | ece | 4 | 2 | 2 |
          | ece | 4 | 4 | 2 |
          | eee | 1 | 1 | 1 |
          | eee | 1 | 4 | 1 |
          | eee | 2 | 1 | 1 |
          | eee | 2 | 2 | 1 |
          +--------+------+----------+-------+





          share|improve this answer





















          • i know its easy with query...but my mentor want to use only loops
            – Mahesh Ramineedi
            Nov 8 at 12:15















          up vote
          1
          down vote













          Is there a specific reason why you don't want to use a query?



          SELECT branch, year,semester, COUNT(*) total FROM students GROUP BY branch,year,semester;
          +--------+------+----------+-------+
          | branch | year | semester | total |
          +--------+------+----------+-------+
          | cse | 1 | 1 | 4 |
          | cse | 2 | 1 | 1 |
          | cse | 2 | 2 | 2 |
          | cse | 2 | 4 | 1 |
          | cse | 3 | 2 | 1 |
          | cse | 3 | 3 | 2 |
          | cse | 4 | 3 | 1 |
          | cse | 4 | 4 | 2 |
          | ece | 1 | 4 | 2 |
          | ece | 2 | 2 | 2 |
          | ece | 2 | 3 | 1 |
          | ece | 3 | 1 | 1 |
          | ece | 3 | 3 | 2 |
          | ece | 4 | 2 | 2 |
          | ece | 4 | 4 | 2 |
          | eee | 1 | 1 | 1 |
          | eee | 1 | 4 | 1 |
          | eee | 2 | 1 | 1 |
          | eee | 2 | 2 | 1 |
          +--------+------+----------+-------+





          share|improve this answer





















          • i know its easy with query...but my mentor want to use only loops
            – Mahesh Ramineedi
            Nov 8 at 12:15













          up vote
          1
          down vote










          up vote
          1
          down vote









          Is there a specific reason why you don't want to use a query?



          SELECT branch, year,semester, COUNT(*) total FROM students GROUP BY branch,year,semester;
          +--------+------+----------+-------+
          | branch | year | semester | total |
          +--------+------+----------+-------+
          | cse | 1 | 1 | 4 |
          | cse | 2 | 1 | 1 |
          | cse | 2 | 2 | 2 |
          | cse | 2 | 4 | 1 |
          | cse | 3 | 2 | 1 |
          | cse | 3 | 3 | 2 |
          | cse | 4 | 3 | 1 |
          | cse | 4 | 4 | 2 |
          | ece | 1 | 4 | 2 |
          | ece | 2 | 2 | 2 |
          | ece | 2 | 3 | 1 |
          | ece | 3 | 1 | 1 |
          | ece | 3 | 3 | 2 |
          | ece | 4 | 2 | 2 |
          | ece | 4 | 4 | 2 |
          | eee | 1 | 1 | 1 |
          | eee | 1 | 4 | 1 |
          | eee | 2 | 1 | 1 |
          | eee | 2 | 2 | 1 |
          +--------+------+----------+-------+





          share|improve this answer












          Is there a specific reason why you don't want to use a query?



          SELECT branch, year,semester, COUNT(*) total FROM students GROUP BY branch,year,semester;
          +--------+------+----------+-------+
          | branch | year | semester | total |
          +--------+------+----------+-------+
          | cse | 1 | 1 | 4 |
          | cse | 2 | 1 | 1 |
          | cse | 2 | 2 | 2 |
          | cse | 2 | 4 | 1 |
          | cse | 3 | 2 | 1 |
          | cse | 3 | 3 | 2 |
          | cse | 4 | 3 | 1 |
          | cse | 4 | 4 | 2 |
          | ece | 1 | 4 | 2 |
          | ece | 2 | 2 | 2 |
          | ece | 2 | 3 | 1 |
          | ece | 3 | 1 | 1 |
          | ece | 3 | 3 | 2 |
          | ece | 4 | 2 | 2 |
          | ece | 4 | 4 | 2 |
          | eee | 1 | 1 | 1 |
          | eee | 1 | 4 | 1 |
          | eee | 2 | 1 | 1 |
          | eee | 2 | 2 | 1 |
          +--------+------+----------+-------+






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 11:53









          Strawberry

          25.6k83149




          25.6k83149












          • i know its easy with query...but my mentor want to use only loops
            – Mahesh Ramineedi
            Nov 8 at 12:15


















          • i know its easy with query...but my mentor want to use only loops
            – Mahesh Ramineedi
            Nov 8 at 12:15
















          i know its easy with query...but my mentor want to use only loops
          – Mahesh Ramineedi
          Nov 8 at 12:15




          i know its easy with query...but my mentor want to use only loops
          – Mahesh Ramineedi
          Nov 8 at 12:15


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206616%2fhow-to-count-students-studying-same-branch-year-semester-using-loops-in-php%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

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff