How should I update this code to properly sort the combined results by Lastname? Currently, it prints all sorted employee profiles then all sorted dependent profiles.
// Perform queries to get profiles for resulting company
$query2 = "SELECT * FROM dependentProfile WHERE CompanyID=$CoID AND COBRA='Yes'
ORDER BY EmployeeID";
$result2 = mysql_query($query2) or die("Query 2 error : " . mysql_error() );
$query3 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID AND COBRA='Yes'
ORDER BY EmployeeID";
$result3 = mysql_query($query3) or die("Query 3 error : " . mysql_error() );
// Put Dependent results in table format
while($line = mysql_fetch_array($result2))
{
$ID = $line["DependentID"];
$type = $line["DependentType"];
$last = $line["DependentLast"];
$first = $line["DependentFirst"];
$tableinfo .= "<tr bgcolor=$color>
<td nowrap>$type: $ID</td>
<td nowrap>$last, $first</td>";
// if COBRA = YES, show empty cell
if ($COBRA=="Yes") {
$tableinfo .= "<td> </td>\n";
}
// if COBRA = No, show links to forms
if ($COBRA=="No") {
$tableinfo .= "<td nowrap>
<A HREF=\"f-A2.php?id=$ID\">Doc A</A> | <A HREF=\"f-H.php\">Doc H</A><br>
<A HREF=\"f-C.php?id=$ID\">Doc C</A> | <A HREF=\"f-C1.php?id=$ID\">Doc C1</A> ";
// If dependent is a minor include links to Doc D
if ( $type=="Minor")
{
$tableinfo .= "<BR>
<A HREF=\"f-D.php?id=$ID\">Doc D</A> | <A HREF=\"f-C1.php?id=$ID\">Doc D1</A>";
}
$tableinfo .= "</td>\n";
}
$tableinfo .= "</tr>\n";
}
// Put Employee results in table format
while($line = mysql_fetch_array($result3))
{
$type = "Employee"
$ID = $line["EmployeeID"];
$last = $line["EmployeeLast"];
$first = $line["EmployeeFirst"];
$taa = $line["TAA"];
$COBRA = $line["COBRA"];
$tableinfo .= "<tr>
<td nowrap>$type, $ID</td>
<td nowrap>$last, $first</td>";
if ($COBRA=="No") {
$tableinfo .= "<td nowrap class=\"small\" align=\"left\">
<A HREF=\"f-A.php?id=$ID\">Doc A</A> |
<A HREF=\"f-H.php\">Doc H</A><br>
<A HREF=\"f-B.php?id=$ID\">Doc B</A> | <A HREF=\"f-B1.php?id=$ID\">Doc B1</A>\n";
//if employee TAA = yes, show Doc J link
if ($taa=="Yes")
{
$tableinfo .= "| <A HREF=\"f-J.php\">Doc J</A>";
}
$tableinfo .= "</td>\n";
}
// if employee COBRA = YES, show empty cell
if ($COBRA=="Yes") {
$tableinfo .= "<td> </td>\n";
}
$tableinfo .= "</tr>\n";
}
// print results in table
print "<table cellpadding=1 cellspacing=1 width=100% border=\"0\" bordercolor=\"#cccccc\">
<tr>
<th align=\"left\" valign=\"bottom\">Type & ID </th>
<th align=\"left\" valign=\"bottom\">Name</th>
<th align=\"left\" valign=\"bottom\">Forms</th>
</tr>";
print "$tableinfo";
print "</table>";
print "<br>";
I was able to successfully sort both results in a SELECT box, but I'm confused how to work with multi-dimensional arrays to print results in a table; and in combination with the IF statements (to print certain info depending on $type). REF:
$query = "SELECT * FROM userProfiles,companyProfile WHERE userProfiles.CompanyID=companyProfile.CompanyID";
$result = mysql_query($query) or die("Query error: " . mysql_error() );
$row = mysql_fetch_array($result);//Get ID values
if($row) {
$CoID = $row["CompanyID"];
}
$query4 = "SELECT * FROM dependentProfile WHERE CompanyID=$CoID AND COBRA='Yes'
ORDER BY DependentLast,DependentFirst";
$result4 = mysql_query($query4) or die("Error with query 4 : " . mysql_error() );
$query8 = "SELECT * FROM employeeProfile WHERE CompanyID=$CoID AND COBRA='Yes'
ORDER BY EmployeeLast,EmployeeFirst";
$result8 = mysql_query($query8) or die("Error with query 8 : " . mysql_error() );
$name_8 = array();
while($row8 = mysql_fetch_array($result8))
{
$name_8[$row8["EmployeeID"]] = $row8["EmployeeLast"].", ".$row8["EmployeeFirst"];
}
$name_4 = array();
while($row4 = mysql_fetch_array($result4))
{
$name_4[$row4["DependentID"]] = $row4["DependentLast"].", ".$row4["DependentFirst"];
}
$combined = array_merge($name_8,$name_4);
asort($combined);
print "<select>";
while (list($key, $value) = each($combined)) {
echo "<OPTION VALUE=$key>$value</OPTION>\n";
}
print "</select>";
3 Database tables, each with unique IDs; CompanyID common to all three.