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
php mysql
add a comment |
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
php mysql
add a comment |
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
php mysql
<?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
php mysql
edited Nov 8 at 11:44
asked Nov 8 at 11:14
Mahesh Ramineedi
32
32
add a comment |
add a comment |
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>';
}
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
add a comment |
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 |
+--------+------+----------+-------+
i know its easy with query...but my mentor want to use only loops
– Mahesh Ramineedi
Nov 8 at 12:15
add a comment |
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>';
}
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
add a comment |
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>';
}
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
add a comment |
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>';
}
<?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>';
}
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
add a comment |
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
add a comment |
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 |
+--------+------+----------+-------+
i know its easy with query...but my mentor want to use only loops
– Mahesh Ramineedi
Nov 8 at 12:15
add a comment |
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 |
+--------+------+----------+-------+
i know its easy with query...but my mentor want to use only loops
– Mahesh Ramineedi
Nov 8 at 12:15
add a comment |
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 |
+--------+------+----------+-------+
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 |
+--------+------+----------+-------+
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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