-2
<?
// 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 时显示另一个鸟图像的额外单元格?

例如,如果

  • blue_bird 值等于 1 在行中显示图像
  • red_bird 值等于 1 还显示行中的红色鸟图像
  • green_bird 值等于 1 显示绿鸟图像
  • gold_bird 值等于显示金鸟图像
  • black_bird 值等于显示黑鸟图像

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

4

1 回答 1

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

于 2013-01-22T14:37:17.620 回答