Welcome to WebmasterWorld Guest from 18.210.28.227

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Can anybody help me with a loop for a phone list?

please...

     
1:44 pm on Jun 28, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 17, 2003
posts:76
votes: 0


Could anybody help me with this problem please...

I have a database with various members, each belonging to a department. I need to sort the list by department, giving the title of the department in a line of the table, followed by the members of the department each as lines of the table. I'm using PHP and MySql.

When there are no more members in a department, I need to then move onto the next department, and list the staff there, and so on until all departments and staff have been listed.

Here's a piece of code that lists all of the staff in the directory:

while($myrow = mysql_fetch_array($result)){

$newID = $myrow['0'];
$newFirstName = $myrow['1'];
$newSurname = $myrow['2'];
$newState = $myrow['3'];
$newTelephone = $myrow['4'];
$newDirectLine = $myrow['5'];
$newMobile = $myrow['6'];
$newFax = $myrow['7'];
$newEmail = $myrow['8'];
$newDepartment = $myrow['9'];
$newJobTitle = $myrow['10'];
$newCarReg = $myrow['11'];
$newPicture = $myrow['12'];
$newLocation = $myrow['13'];
$newImportance = $myrow['14'];

$displayblock3 .= "
<tr><td valign='top'>
<p class='DarkFont'><a href='mailto:$newEmail' class='DarkFontSmall'>$newFirstName $newSurname</a></td>
<td valign='top'><p class='DarkFontSmall'>$newJobTitle</p></td>
<td valign='top'><p class='DarkFontSmall'>$newDirectLine</p></td>
<td valign='top'><p class='DarkFontSmall'>$newTelephone</p></td>
<td valign='top'><p class='DarkFontSmall'>$newFax</p></td>
<td valign='top'><p class='DarkFontSmall'>$newMobile</p></td>
</tr><tr><td colspan='6'><img src='images/spacer-black.gif' width='100%' height='1'></td></tr>
";

}

And here is a piece of code that lists all of the departments available:

while($myrow2 = mysql_fetch_array($result2)){
$newDept = $myrow2['0'];

print"<table border='0' width='650' align='center' cellpadding='2' cellspacing='0' bgcolor='#FFFFFF' class='border-black'>\n";
print"<tr bgcolor='#90A7D6'>\n";
print"<td colspan='6'><p class='whitefont'><b>$newDept</b></p></td>\n";
print"</tr>\n";

Any help would be appreciated!
Thanks

2:07 pm on June 28, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 31, 2003
posts:1316
votes: 0


I need to sort the list by department, giving the title of the department in a line of the table, followed by the members of the department each as lines of the table

Try something like this. In your SQL query, order by department, then name or whatever you wish. Then use a flag to indicate when you've reached the next department in the list. Example:

$lastDepartment = "";
while($myrow = mysql_fetch_array($result)) {
$newFirstName = $myrow['1'];
$newSurname = $myrow['2'];
$newDepartment = $myrow['9'];
if ($newDepartment!= $lastDepartment) {
print "Department: $newDepartment<br>";
}
print "Employee: $newFirstName $newSurname<br>"
$lastDepartment = $newDepartment;
}

2:14 pm on June 28, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 17, 2003
posts:76
votes: 0


Brilliant - thank you that works fine!