Jump to content

Replace user ID with Name

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
xsell

xsell

    Newbie

  • Members
  • Pip
  • 4 posts
Hello
this is my First post , SO Hola

I have Simple Script , Messages between Members .

i have two tabels on has users name and id , email.. another tabel for messages has message id , subject, from, to, text

Now the code Below Shows the messages between the users . the code Shows Message ID , From, To , Subject, Text .

But where "from" and "to" replaced with user ID , and i want it to be replaced with the User Name .. from the other Tabel ..

like if user id =100 get user name from table users where ID=100

$result = mysql_query("SELECT * FROM ".$DB->prefix("msgs")."  LIMIT 10"); 

$rows = mysql_num_rows($result); 

if($rows > 0){ 


            echo "<table width='700' border='1'> 

            <tr> 

           <td>"; 

            echo "<table border='1' width='100%'>"; 

            echo "<tr>"; 

            echo "<td width='15%'><b>ID</b></td>  "; 

            echo "  <td width='15%'><b>From</b></td><td width='15%'><b>To</b> "; 

            echo " <td width='15%'><b><p>Subject</p></b>"; 

            echo "</td><td width='45%'><b>Message</b></td>" ; 

            echo "</tr>"; 


while($row = mysql_fetch_array($result)){ 


echo "<tr><td width='15%'>".$row['id']."</td>"; 

echo "<td width='15%'>".$row['from']."</td> "; 

echo "<td width='15%'>".$row['to']."</td> "; 

echo "<td width='15%'>".$row['subject']."</td> "; 

echo "<td width='45%'>".$row['msg']."</td> "; 

} 

     echo "</table>"; 

     echo "</td> "; 

     echo "  </tr>   "; 

     echo " </table>"; 

} 

this the code i use

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
You don't replace them, you make a database query that joins the tabel together and access the info you need

SELECT f.name as fromname, t.name as toname, m.* FROM msgs m
JOIN users f ON m.from = f.id
JOIN users t ON m.to = t.id
this way, you get the extra two fields "fromname" and "toname" in your query result and can use them in your code.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
xsell

xsell

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you

Someone Said exactly that to me . but since i'm no coder i did not know wht to do with the ur code , The Code i posted i did not make it i'm just tryin to edit it .. So can you applay your Code to the One i provided..

i need to do it without mess with mysql tabels ... it tried to insert ur code and keep get error m fcolumn not exist

Update :

I done it .. but is there any other way rather than doing this way.

Edited by xsell, 14 October 2009 - 07:47 PM.


#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
well, this is the simpliest way of doing it. and it does not mess with the database at all, as it only reads from it.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall