Jump to content

Recommended Posts

Hi everyone,

 

Just wondering if someone would be so kind as to fill me in on if what I wold like to attempt is possible please?

 

I have 2 tables,

 

1 has (projects)

id

provider_id,

project_title,

date.

 

the 2nd is (providers)

id

name

 

Now the provider_id in the same as the id in the providers table so the name will match up. All I want is to output the providers name instead of the providers_id like I have at the moment. Some sort of linking is needed I guess?

 

$result = mysql_query("SELECT * from projects ORDER BY date");

echo "<table border='2' cellpadding='3' cellspacing='2'style='border-top: 1px solid #0092F2; border-right: 1px solid #0092F2; border-left: 1px solid #0092F2;'>
<tr class='grey_link'>
<th>PROVIDER</th>
<th>TITLE OF PROJECT</th>
<th>DATE</th>
</tr>";


while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td class='grey_link'>" . $row['provider_id'] . "</td>";
  echo "<td class='grey_link'>" . $row['project_title'] . "</td>";
  echo "<td class='grey_link'>" . $row['date'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

 

Thanks

 

Steve

Link to comment
https://forums.phpfreaks.com/topic/137170-solved-php-mysql-query/
Share on other sites

try changing your query to this;

 

$result = mysql_query("SELECT a.project_title, a.date, b.name 
FROM projects a, providers b 
WHERE a.provider_id = b.id
ORDER BY date");

 

Not tested but pretty straight forward :)

 

**EDIT**

 

don't forget to change the output to this;

 

  echo "<tr>";
  echo "<td class='grey_link'>" . $row['name'] . "</td>";
  echo "<td class='grey_link'>" . $row['project_title'] . "</td>";
  echo "<td class='grey_link'>" . $row['date'] . "</td>";
  echo "</tr>";

$result = mysql_query("SELECT t1.*, t2.* from projects AS t1, providers AS t2 WHERE projects.provider_id = provider.id ORDER BY date");

 

Then..

 

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td class='grey_link'>" . $row['t2.name'] . "</td>";
  echo "<td class='grey_link'>" . $row['t1.project_title'] . "</td>";
  echo "<td class='grey_link'>" . $row['t1.date'] . "</td>";
  echo "</tr>";
  }

 

This isn't checked...

 

EDIT: gevans beat me too it with a pretty similar solution..

 

A

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.