Forum Moderators: coopster

Message Too Old, No Replies

echo name once and all contact details

maybe use an if statement to avoid duplicate printing of name

         

nofia

9:15 pm on Jun 30, 2008 (gmt 0)

10+ Year Member



i have a contact table with contact_id, fname, lname

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

cameraman

11:05 pm on Jun 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Keep a running 'current' name and compare each record to it; when it changes, echo the name, otherwise don't:
$curname = '';
while(getting records) {
$name = $rec['icfname'] . ' ' . $rec['iclname'];
if($name != $curname) {
echo $name;
$curname = $name;
}
echo other stuff;
}

nofia

12:53 am on Jul 1, 2008 (gmt 0)

10+ Year Member



thanks cameraman

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

cameraman

2:09 am on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try using a GROUP BY clause; I didn't suggest that before because it always takes me a bit of twiddling to get what I want out of it.

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',...
)
)

nofia

2:25 am on Jul 1, 2008 (gmt 0)

10+ Year Member



thanks for the reply
i'm stuck with the data the way it is, and i tried the group thing but that got me nothing close to what i needed.

i'll work with your suggestions and see if i have any luck, if not i'll be back with more questions

cameraman

6:05 am on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you're looking at something along the lines of:
$curname = '';
$data = array();
while(getting records) {
if($rec['flname'] != $curname) {
if(!empty($data)) {// since it would be empty on first pass
echo $data['name'];
foreach($data['phone'] as $phone)
echo $phone;
foreach($data['email'] as $email)
echo $email;
} // EndIf have data
$curname = $name;
$data = array(); // clear this for the next person
} // EndIf name changed

// 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;

nofia

1:26 pm on Jul 1, 2008 (gmt 0)

10+ Year Member



thanks for the reply,
i typed in your code,
what do i put in (getting records)
where you have while(getting records)
is it something like this for, flname, phones, emails

$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 ( )

nofia

1:47 pm on Jul 1, 2008 (gmt 0)

10+ Year Member



every time i see $data, is it referring to $data below or the $data you called $data = array();
can $data id 2 things

$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

cameraman

5:49 pm on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RATS I'm sorry, change 'my' $data (every occurrence in the code I posted above) to anything else - I completely spaced the fact that you were already using that variable name.

while(getting records)

should be whatever you're using to fetch the record. Something like
while($rec = mysql_fetch_array($data)){ // <-- your data ;)

nofia

6:21 pm on Jul 1, 2008 (gmt 0)

10+ Year Member



thanks
i'll let you know if that works
going to work on it later tonight
jd

nofia

8:11 pm on Jul 2, 2008 (gmt 0)

10+ Year Member



thanks cameraman for your suggestions
i followed your logic and got it to work
i ended up using a few different querys and referring to what i needed in those to get what i wanted to print

jd