That won't work will it? Here is the table structure that I am dealing with:
cdata_id
users_id
field_id
value
Yikes, please help some more :)
Main Topics
Browse All TopicsMy 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
} else if($row["cfield_id"] == 1) {
$states[$row["users_id"]] = $row["value"];
} else if($row["cfield_id"] == 2) {
$countries[$row["users_id"
} 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,
$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!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Country 1 (cfield_id= 2) [Start with United States, then followed alpha by other countries]
State 1 (cfield_id= 1)
City (cfield_id= 3)
Participant 1 (cfield_id= 9)
event description (cfield_id= 12)
Participant 2 (cfield_id= 9)
event description (cfield_id= 12)
City 2 (cfield_id= 3)
Participant 3 (cfield_id= 9)
event description (cfield_id=12)
Participant 4 (cfield_id= 9)
State 2 (cfield_id= 1)
example in http://www.f1astronomie.eu
Whether it's better than yr solution, I don't know, but I think it's clearer and more easily maintained?
NOTE: I CHANGED cdata (no particular reason, just lazyness)
CREATE TABLE `web22-testgaller`.`cdata`
`cdata_id` int(10) unsigned NOT NULL auto_increment,
`users_id` int(11) default NULL,
`field_id` int(11) default NULL,
`value` varchar(30) default NULL,
PRIMARY KEY (`cdata_id`)
) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
Rows in cdata:
1, 1, 2, '1'
2, 1, 1, '5'
3, 1, 3, '1'
4, 1, 9, '1'
5, 1, 12, '1'
6, 2, 2, '1'
7, 2, 1, '2'
8, 2, 3, '11'
9, 2, 9, '1'
10, 2, 12, '2'
11, 3, 2, '4'
12, 3, 1, '4'
13, 3, 3, '2'
14, 3, 9, '3'
15, 3, 12, '3'
16, 4, 2, '4'
17, 4, 1, '3'
18, 4, 3, '3'
19, 4, 9, '2'
20, 4, 12, '5'
CREATE TABLE `web22-testgaller`.`lm_use
`users_id` int(11) default NULL,
`group_id` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
rows in lm_users:
1, 2
2, 2
3, 2
4, 2
5, 2
NEW TABLE (could be temp)
DROP TABLE IF EXISTS `web22-testgaller`.`tmp_ta
CREATE TABLE `web22-testgaller`.`tmp_ta
`user_id` int(11) default NULL,
`country_id` int(11) default NULL,
`state_id` int(11) default NULL,
`city_id` int(11) default NULL,
`partic` int(11) default NULL,
`event` varchar(30) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
NOTE: I didn't know the nature of partic and event.
Also I don't know what is field 12 .
Here is the code
<?php
define("DB_SERVER", $_SERVER['SERVER_NAME']);
define("DB_USER", "user");
define("DB_PASS", "pass");
define("DB_NAME", "db");
$con= mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
$db=mysql_select_db(DB_NAM
//first remove everything from tmp_table
// this wouldn't work with multiple user calling the page
// but you could add a session_id column on tmp_table to get around that
$Qry ="DELETE FROM tmp_table";
$res=mysql_query($Qry,$con
function mk_sorted($field,$key,$r){
global $con;
$Qry = " SELECT lm_users.users_id user, cdata.value ".$field." FROM lm_users
,cdata WHERE lm_users.group_id =2
AND lm_users.users_id =cdata.users_id
AND cdata.field_id =".$key ;
// you might use getData($Qry);
// and do some error checking :-)
$res=mysql_query($Qry,$con
while ($row=mysql_fetch_assoc($r
$r[$row[user]][$field]=$ro
}
}
function in_tmp($values, $keys){
global $con;
$Qry = "INSERT INTO tmp_table VALUES(".$keys.",".$values
$res=mysql_query($Qry,$con
}
//define the names corresponding to cfield_id
$sort=array("2"=>"country"
//select these cfield_id one by one and assemble in an array
array_walk($sort,"mk_sorte
// now we have an array $result of the shape
//$result{
// [user_id] => {
// ['country']=>country_id
// ['state']=>stateid
// ['city']=>cityid
// ['partic']=>particid
// [event']=>eventid
// }
//}
//Insert the useful values of $result in tmp_table
array_walk($result,"in_tmp
//Now we can get the data sorted as we want
$Qry = "SELECT country_id, state_id,city_id,partic,ev
//...Your code for display here................etc.
//this for the demo:
$res=mysql_query($Qry,$con
$endResult=array();
while ($row=mysql_fetch_assoc($r
array_push($endResult,$row
}
print_r($endResult);
?>
My solution http://www.f1astronomie.eu
Business Accounts
Answer for Membership
by: dereshPosted on 2007-01-26 at 00:44:40ID: 18402752
you should do it in your database:)
expand your order by statment to sort on fields that you want, for example:
select distinct participants.id, pacticipants.field from entry where .... ORDER BY country, state, city
that should sort stuff the way you want.