Jump to content

Outputting table with mysql join


ntroycondo

Recommended Posts

I'm outputting my DB to a table. I want to include a JOIN so the table shows name, email, and company (which is in a different table). Not sure where to put the join.

2 other things I'd like to do:

Not all users have email and I want any NULL to output as N/A instead of NULL.

Would like the rows to alternate background color between white and blue for each row.

Any hints appreciated.

 

 

$query= "select * from Managers";
$result=mysql_query($query);
echo mysql_error();

echo '<table align="center" cellspacing="1" cellpadding="2">
<tr>
<td align="left"><b>Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Company</b></td>
</tr>
';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['name'] . '</td>
	<td align="left">' . $row['email'] . '</td>
	<td align="left">' . $row['company.name'] . '</td> //Need JOIN here
</tr>
';
}

Link to comment
https://forums.phpfreaks.com/topic/212253-outputting-table-with-mysql-join/
Share on other sites

The join is I think:

 

JOIN Manager ON Manager.companyid = Company.companyid

 

But I'm not sure if I just create a variable for this in the PHP?

 

Table structure is:

Manager with 4 columns managerid, name, email, companyid

Company with 2 columns companyid, name

Your query will be like this

 

SELECT m.name as manager_name, 
       m.email as manager_email,
       c.name as company_name
FROM Managers m
LEFT JOIN Company c ON 
    c.companyid = m.companyid

 

So your complete code would be

$query= "SELECT m.name as manager_name, 
       m.email as manager_email,
       c.name as company_name
FROM Managers m
LEFT JOIN Company c ON 
    c.companyid = m.companyid";
    
$result = mysql_query($query);
echo mysql_error();

echo '<table align="center" cellspacing="1" cellpadding="2">
<tr>
<td align="left"><b>Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Company</b></td>
</tr>
';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['manager_name'] . '</td>
	<td align="left">' . $row['manager_email'] . '</td>
	<td align="left">' . $row['company_name'] . '</td>
</tr>
';
}

For the displaying N/A if email is NULL. Change this

		<td align="left">' . $row['manager_email'] . '</td>

To

		<td align="left">' . (!empty($row['manager_email']) ? $row['manager_email'] : 'N/A') . '</td>

 

For adding alternative row colours add

$i = 0;
$color1 = '#CCCCFF';
$color2 = '#CCFFFF';

NOTE: Set your two alternative colours above

 

Before

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

Now change

	echo '<tr bgcolor="' . $bg . '">

To

	$bg = ($i++ % 2) ? $color1 : $color2;
echo '<tr bgcolor="' . $bg . '">

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.