newbie question : combining 2 fields

Codes here !

Moderators: macek, egami, gesf

alexvgtb
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Jan 09, 2003 2:41 am

newbie question : combining 2 fields

Postby alexvgtb » Thu Jan 09, 2003 2:51 am

Hello,

This is a very stupid problem but I'm new at this :-)


Let's say I have two different fields in the same table :

Author1 and Author2

I want a sorted list of those two fields combined.

but if a same term exists both in Author1 and Author2, it should appear only once in the list of course.

What would be the MYSQL query to get this?
Also, what would be the php code?

Thanks in advance!

Alex

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Thu Jan 09, 2003 5:39 am

Use a normal query and an if

Code: Select all

while ($row=Mysql_fetch_array($result)){
    if ($row["Author1"] != $row["Author2"]){
       echo $row["Author1"]."<br>";
       echo $row["Author2"]."<p>";
    }
}


Is this what you meant??

alexvgtb
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Jan 09, 2003 2:41 am

Postby alexvgtb » Thu Jan 09, 2003 5:51 am

Thanks Joan!

But will I get a sorted list with this?

For example :

Let's say I have 3 fields in my database :

Id - Author1 - Author2

and 5 rows :

1 John Tom
2 Marc John
3 Marc (blank)
4 Kelly Sandra
5 Kelly (blank)

I would like to get a sorted list like this :

John
Kelly
Marc
Sandra
Tom

See? A mix of Author1 and Author2 but no duplicate.

Thanks in advance.

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Thu Jan 09, 2003 10:37 am

ok, use this -->

Code: Select all

while ($row=Mysql_fetch_array($result)){
   if (!array_key_exists($row["Author1"], $names) or !array_key_exists($row["Author2"], $names)) {
      $names[] = $row["Author1"];
      $names[] = $row["Author2"];     
   }
}

sort ($names,SORT_STRING);
print_r ($names);


bye!

alexvgtb
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Jan 09, 2003 2:41 am

Postby alexvgtb » Thu Jan 09, 2003 10:50 am

Thank you I'll try it!


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests

cron