Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

newbie question : combining 2 fields

Codes here !

Moderators: macek, egami, gesf

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
alexvgtb
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Jan 09, 2003 2:41 am

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??
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

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.
alexvgtb
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Jan 09, 2003 2:41 am

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!
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

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

Thank you I'll try it!
alexvgtb
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Jan 09, 2003 2:41 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.