对于每个结果= 1将单元格添加到表格行?

时间:2013-01-22 14:33:25

标签: php mysql html-table

<?
// Set the MySQL Configuration
$db_host       = "test";
$db_user       = "test";
$db_password   = "test";
$db_name       = "test";
$db_table      = "test";

// Start Connection
$db_connect        =  mysql_connect ($db_host, $db_user, $db_password);

// Select Database
$db_select        =  mysql_select_db ($db_name, $db_connect);


$query = "SELECT bird_owner, place_name, funds, place_description FROM bird_locations";

$query2 = "SELECT blue_bird, red_bird, green_bird, gold_bird, black_bird FROM bird_locations"; 

// this query ($query2) is to select just the birds so can check if any bird = 1 then add a new image cell in table

// Execution MySQL Query
$result = mysql_query($query);


if($result)
{

// table headers

echo '<table width="90%" border="0" align="center" cellspacing="2" cellpadding="10">


<p><p><p>
<tr>
<th align="center"><b>Owner</b></th>
<th align="center"><b>PlaceName</b></th>
<th align="center"><b>Birds</b></th>
<th align="center"><b>Funds</b></th>
<th align="center"><b>Description</b></th>
</tr>
';

//now i add a new row for each result which works.

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
 {
     echo '<tr>
    <td align="center"><b>' . $row['bird_owner'].'</b></td>
    <td align="center"><b>' . $row['place_name'].'</b></td>

    <td align="center"><b><img src="bird_blue_50px.png"/></b></td> 

    <td align="center"><b>' . $row['funds'].'L$</b></td>
    <td align="center"><b>' . $row['place_description'].'</b></div></td>

    </tr>';

 }

echo '</table>';

}


//Close MySQL connection
mysql_close($db_connect);
?>

当生成行时,行的第3个单元格是鸟的图像,但是如何使其成为如果数据库中的鸟的值为1,则表示另一个鸟图像的额外单元格?

e.g。如果

  • blue_bird值等于1显示行中的图像
  • red_bird值等于1还显示行中的红色鸟图像
  • green_bird值等于1显示绿鸟图像
  • gold_bird值等于show gold bird image
  • black_bird值等于显示黑鸟图像

如果值等于0,则不要在表中显示该图像

1 个答案:

答案 0 :(得分:1)

拆分回声:

echo '<tr>
<td align="center"><b>' . $row['bird_owner'].'</b></td>
<td align="center"><b>' . $row['place_name'].'</b></td>';
if ($row['blue_bird']==1){
   echo '<td>Etc...';
}

echo '<td align="center"><b><img src="bird_blue_50px.png"/></b></td> 

<td align="center"><b>' . $row['funds'].'L$</b></td>
<td align="center"><b>' . $row['place_description'].'</b></div></td>

</tr>';

此外,我们将不再支持mysql_query:http://php.net/manual/en/function.mysql-query.php