Click to See Complete Forum and Search --> : sorting problem


nebchill26
06-26-2008, 09:11 PM
i just made this code but im having a problem sorting it from highest to lowest...i tried using ORDER BY but i still can't get it,can anyone help me out

here's my code:


<?php
$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 = mysql_num_rows($Counts);
//////THIS IS THE RESULT I WANT TO SORT
echo $CountedRows;
echo "<br \>";
}
?>


here's is the sample data in the database:


tblDepartment
ID | Department
1 | CSD
2 | HRD
3 | DCD

view_name
ID | 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



now i get the result by using the code and the database is this:


2
4
3


how can i make it to become


4
3
2


sort it depending on the data in the database,if it cannot be done,is there a work around it?tnx

skywalker2208
06-26-2008, 09:35 PM
Why don't you use the id field of the department table in the view_name table instead of using the name of the department? If you do that you can do a join on the two tables. You could join on the department name, but it is better if you join on ID's. Kind of like what I have below.


tblDepartment
ID | Department
1 | CSD
2 | HRD
3 | DCD

view_name
ID | DATE | Department | PCName
1 | JUNE | 1 | csd-1
2 | JUNE | 1 | csd-2
3 | JUNE | 2 | hrd-1
4 | JUNE | 2 | hrd-2
5 | JUNE | 2 | hrd-3
6 | JUNE | 2 | hrd-4
7 | JUNE | 3 | dcd-1
8 | JUNE | 3 | dcd-2
9 | JUNE | 3 | dcd-3

nebchill26
06-26-2008, 09:52 PM
had to change something in the database sample:


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


can you please give me an example about that?im still not used to using JOINS

skywalker2208
06-26-2008, 10:10 PM
If you keep the table the way it is you would join on the department because that is what you are matching between the two tables.


SELECT * FROM tblDepartment t
JOIN view_name v ON v.Department = t.Department

nebchill26
06-26-2008, 10:30 PM
thanks for the help,ill try using it to solve my problem,tnx again