My db query is as follows:
$query = "SELECT lm_users.users_id, lm_cdata.cfield_id, lm_cdata.value FROM lm_users
INNER JOIN lm_cdata ON lm_users.users_id = lm_cdata.user_id WHERE lm_users.group_id =2
AND (
lm_cdata.cfield_id = '1'
OR lm_cdata.cfield_id = '2'
OR lm_cdata.cfield_id = '3'
OR lm_cdata.cfield_id = '11'
OR lm_cdata.cfield_id = '12'
OR lm_cdata.cfield_id = '9'
) order by lm_users.users_id";
//echo $query;
I need to have all participants organized by country, state, city without any duplicate organizations.
For example: (Need it to be alpha asc sorted)
State 1
City
Participant 1
Participant 2
City 2
Participant 3
Participant 4
'No City Specified'
Participant 5
Participant 6
State 2
City
Participant 1
Participant 2
City 2
Participant 3
Participant 4
'No City Specified'
Participant 5
Participant 6
Here is my code:
<?php
$stateArray = array("AL"=>"Alabama",
"AK"=>"Alaska",
"AZ"=>"Arizona",
"AR"=>"Arkansas",
"CA"=>"California",
"CO"=>"Colorado",
[..and so on...]
);
function displayEvents() {
echo "<a name='top'></a>";
global $stateArray;
$res = getData();
$are_there_any = mysql_num_rows($res);
$uidsHostingEvents = array();
$states = array();
$countries = array();
$event_descs = array();
$cities = array();
$orgs = array();
while($row = mysql_fetch_array($res)) {
if($row["cfield_id"] == 11 && $row["value"] == "yes") {
$uidHostingEvents[] = $row["users_id"];
}
//event description
if($row["cfield_id"] == 12) {
$event_descs[$row["users_i
d"]] = $row["value"];
} else if($row["cfield_id"] == 1) {
$states[$row["users_id"]] = $row["value"];
} else if($row["cfield_id"] == 2) {
$countries[$row["users_id"
]] = $row["value"];
} else if($row["cfield_id"] == 3) {
$cities[$row["users_id"]] = $row["value"];
} else if($row["cfield_id"] == 9) {
$orgs[$row["users_id"]] = $row["value"];
}
}
displaySearch($stateArray,
$countries);
$otherEvents[][] = array();
$usEvents[][] = array();
foreach($uidHostingEvents as $value) {
if($countries[$value] == "United States") {
$usEvents[$value][] = array($countries[$value], $states[$value], $cities[$value], $event_descs[$value], $orgs[$value]);
} else {
$otherEvents[$value][] = array($countries[$value], $states[$value], $cities[$value], $event_descs[$value], $orgs[$value]);
}
}
//actually only sorts by state
usort($usEvents, 'compare');
$str = "<a name='United States'></a><H3>UNITED STATES</H3>\n";
$startState = '';
$orgDisplayed = array();
foreach($usEvents as $id => $participant) {
foreach($participant as $field) {
//only handle participants who have a US as their country and have a state other than NA
if(isset($field[0]) && $field[0] != "NA" || isset($field[1]) && $field[1] != "NA") {
//if this organization has not already been displayed
if(!in_array($field[4], $orgDisplayed)) {
//is there even an organiation for this participant?
if($field[4] != "") {
$orgDisplayed[] = $field[4];
if($startState != $field[1]) {
if($startState != '') {
$str .='<div align=right><a href="#top">go to top</a></div>';
}
$startState = $field[1];
$str .= "</ul>\n<a name='" . $field[1] . "'></a><b>" . $stateArray[$field[1]] . "</b>\n<ul>";
}
//ok now we have to sort cities and put participants under common cities
if($field[2] != "") {
$str .= "\n<li>".ucfirst($field[2]
) . " ";
} else {
$str .= "\n<li>";
}
if($field[4] != "" && $field[2] != "") {
$str .= "<BR><b>" . $field[4] . "</b> ";
} else {
$str .= "<b>" . $field[4] . "</b> ";
}
if($field[3] != "" && ($field[0] != "NA" || $field[1] != "NA" || $field[2] != "")) {
$str .="<BR>" . $field[3] . "";
}
if($field[0] != "NA" || $field[1] != "NA" || $field[2] != "" && $field[3] != '') {
$str .= "<BR><BR></li>";
}
} //end if not organization, do not display
} else {
// else if organization has already been displayed
//display event description
}
} // end if has country US and a state
} //end foreach field
} //end participant
echo $str . "</ul>";
}
If you could offer advice. There has got to be a better way. Thank you!
Start Free Trial