/    Sign up×
Community /Pin to ProfileBookmark

can somebody help me with this code on how to show other fields from the database..

here’s my code:

[CODE]
<table>
<tr>
<td>
<?php
$i=0;
$GetDepts = mysql_query(“SELECT * FROM tblDepartment”) or die (‘MySQL SELECT Error: ‘.mysql_error());

while($DeparmentArray=mysql_fetch_array($GetDepts))
{
$Depts = $DeparmentArray[‘Department’];
$Counts = mysql_query(“SELECT * FROM view_name where Department=’$Depts'”)
or die (‘MySQL SELECT Error: ‘.mysql_error());
$CountedRows[$i] = mysql_num_rows($Counts);
$i++;
}
?>
</td>
<td>
<?
rsort($CountedRows);
foreach ($CountedRows as $rows=> $val) {
echo $val . “<br >”;
}
?>
</td>
</table>
[/CODE]

and here’s the database sample:

[CODE]
tblDepartment
DeptId Department
1 CSD
2 HRD
3 DCD

view_name
ReqNum DATE Department PCName
1 JUNE CSD csd-1
2 JUNE CSD csd-2
3 JUNE HRD hrd-1
4 JUNE HRD hrd-2
5 JUNE HRD hrd-3
6 JUNE HRD hrd-4
7 JUNE DCD dcd-1
8 JUNE DCD dcd-2
9 JUNE DCD dcd-3
[/CODE]

now my problem is when i use the code i made,it shows the counted rows of each department which in this case the result will be like this

[CODE]
4 //this is the HRD which is not showed but i want to show this label
3 //this is the DCD which is not showed but i want to show this label
2 //this is the CSD which is not showed but i want to show this label
[/CODE]

what i want to do is also show the current department title…

something like this:
department | result
HRD | 4
DCD | 3
CSD | 2
hope someone can help me about this.tnx

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@jamesm6162Jun 27.2008 — Try the following query in your very first mysql_query():

[CODE]SELECT Department, count(*) as count FROM view_name
GROUP BY Department[/CODE]


This contains exactly what you want. Now just display the rows wherever you want.
Copy linkTweet thisAlerts:
@nebchill26authorJun 27.2008 — still did not work, what i want to do is sort it by highest to lowest based on the result,i already got it on how to sort it by the result but i can't show their corresponding fields..tnx for replying anyway
Copy linkTweet thisAlerts:
@jamesm6162Jun 27.2008 — Okay let me be clearer. I just tried it and it works.

[CODE]
$query = "SELECT Department, count(*) as count FROM view_name GROUP BY Department ORDER BY count(*) DESC";
$result = mysql_query($query);
echo "<table>";
echo "<tr><td>Department</td><td>Count</td></tr>";
while ($row = mysql_fetch_assoc($result))
{
echo "<tr><td>".$row['Department'] . "</td><td>" .$row['count']."</td></tr>";
}
echo "</table>";
[/CODE]
Copy linkTweet thisAlerts:
@nebchill26authorJun 27.2008 — wow thanks james!You saved me! it worked im still not pretty good at using joins but i will try to use it every time for me to be more skillful at this,thank you very much!
Copy linkTweet thisAlerts:
@jamesm6162Jun 27.2008 — You're welcome.

Its not a JOIN, though.

Its the combination of the count(*) and GROUP BY clause that does the trick.
Copy linkTweet thisAlerts:
@nebchill26authorJun 27.2008 — oh okay! my bad.. still thanks anyway
×

Success!

Help @nebchill26 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...