Jump to content

Using MAX() in a Loop


iainlang

Recommended Posts

 

I've inherited the database of a charity whose volunteeers do Casework with the clients, and, as Caseworkers can occasionally be tackling more than one case at a time, I need to know the last Case number done by each Caseworker.  I've never worked with MySQL's MAX() before and I'm haviing notta lotta luck.  My (pretty naive, I guess) code follows.

 

The "personnel" fields include - firstname, surname, based, position, telephone, mobile, email

The "clients" fields include - caseworker_firstname, caseworker_surname, case_number

 

 

$count=1;

$TableName="personnel";

$Query="SELECT * FROM $TableName WHERE based=\"P\" AND position LIKE \"%Caseworker%\" ORDER BY surname desc, firstname desc";

$Result=mysql_db_query ($DBName, $Query, $Link);

while ($Row=mysql_fetch_array ($Result))

{

$firstname[]=$Row[firstname];

$surname[]=$Row[surname];

$telephone[]=$Row[telephone];

$mobile[]=$Row[mobile];

$email[]=$Row;

$count++;

}

 

$original_count=$count;

 

print("<table width=\"900\" cellspacing=\"0\" cellpadding=\"2\" border=\"1\">");

print("<colgroup span=\"6\"><col width=\"17%\"><col width=\"5%\"><col width=\"14%\"><col width=\"14%\"><col width=\"14%\"><col width=\"35%\"></colgroup>");

print("<tr bgcolor=\"#ffffee\" align=\"center\"><td>Caseworker</td><td>Total</td><td>Last Case done</td><td>Telephone</td><td>Mobile</td><td>e-mail</td></tr>");

 

$TableName="clients";

while ($count>0)

{

$Query="SELECT max(case_number) AS maximum FROM $TableName WHERE caseworker_surname='$surname[$count]' AND caseworker_firstname='$firstname[$count]' ORDER BY caseworker_surname desc, caseworker_firstname desc";

$Result=mysql_db_query ($DBName, $Query, $Link);

while ($Row=mysql_fetch_array ($Result))

{

$maximum[]=$Row[maximum];

}

$count--;

}

 

$count=$original_count;

while ($count > 0)

{

print("<tr bgcolor=\"#ffffee\"><td align=\"right\">$firstname[$count] $surname[$count] </td><td>$count</td><td><a href=\"cli_update_handle.php\">$last_case_number[$count]</a></td><td align=\"right\">$telephone[$count]</td><td align=\"right\">$mobile[$count]</td><td align=\"right\"><a href=\"mailto:$email[count]\">$email[$count]</a></td></tr>");

$count--;

}

print("</table>");

 

Any help would be appreciated.

 

Thanx,

 

Iain.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.