Forum Moderators: coopster
i have a phone table with phone_id, contact_id, phonenumber, phonetype
i have a email table with email_id, contact_id, emailaddress, emailtype
$data = mysql_query("SELECT `c`.`contact_id`, CONCAT(`c`.`icfname`,' ',`c`.`iclname`) AS flname, CONCAT(`p`.`phonenum`,' - ',`p`.`phonetype`) as `phones`, CONCAT(`m`.`emailaddress`,' - ',`m`.`emailtype`) as `emails`
FROM contact c
LEFT JOIN phone p
ON `c`.`contact_id` = `p`.`contact_id`
LEFT JOIN email m
ON `c`.`contact_id` = `m`.`contact_id`
ORDER BY `icfname`, `iclname` ")
or die(mysql_error());
my select works fine, its the output that is drivivng me crazy
HOW DO I USE AN IF STATEMENT or other method TO ONLY PRINT THE NAME ONCE AND THE THEN DETAILS THAT GO WITH THAT NAME, some peole have more than one phone number and email
my output looks like this
joe smith
phone work
email 1
joe smith
phone cell
email 1
joe smith
phone home
email 1
i want to just be able to print
joe smith
phone work
phone cell
phone home
email 1
email 2
any suggestions
thanks
jd
it sort of works, i got the name to stop printing for each address and email
but i keep getting duplicates of the other stuff
like
work phone
email 2
cell phone
email 2
work phone
email 1
cell phone
email 1
and thats just for person 1
it still prints the duplicate output for all even know the name isn't printed
can i do and if for phone and email like you did for $curname = '';
thanks
jd
Can you restructure the data, or do you have to deal the hand you've been dealt? If you can, I would suggest making say 6 telephone fields and 3 email fields; beyond that would have to be deemed unreasonable. If limitless tel fields & email fields is a must, I'd make a 3 column table for name, data, and type.
If you can't restructure, another approach would be to put the results into an associative array and build on it, with the emails being a subarray and the addresses being a subarray; then you can foreach them when the name changes:
array(
'name'=> 'thename',
'emails' => array(
'email','email','email',...
)
'addresses' => array(
'address','address','address',...
)
)
// As long as it's the same person, the name will be overwritten with
// the same name again, but the email and phone arrays will grow:
$data['name'] = $name;
$data['email'][] = $rec['emails'];
$data['phone'][] = $rec['phones'];
// not echoing other stuff here anymore;
} // EndWhile getting records
// Display the last person
echo $data['name'];
foreach($data['phone'] as $phone)
echo $phone;
foreach($data['email'] as $email)
echo $email;
$name = "<center><FONT color=blue SIZE=3>{$row1['flname']}</FONT></center><br>";
i follow your logic but i must be missing something code wise
and i think its the while ( )
$data = mysql_query("SELECT `c`.`contact_id`, CONCAT(`c`.`icfname`,' ',`c`.`iclname`) AS flname, CONCAT(`p`.`phonenum`,' - ',`p`.`phonetype`) as `phones`, CONCAT(`m`.`emailaddress`,' - ',`m`.`emailtype`) as `emails`
i get this error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
while(getting records)
should be whatever you're using to fetch the record. Something like
while($rec = mysql_fetch_array($data)){ // <-- your data ;)