2012-03-08 75 views
2

我使用PHP從MySQL數據庫中提取表。在PHP中顯示來自MySQL的結果旁邊的圖像?

這大致是我使用的代碼,顯然我有更多的列,但這是代碼的演示版本。

如您所見,代碼按銷售名稱排序。現在我想要做的是,根據銷售計數在前十名賣家旁邊顯示一個小圖標。 因此,如果銷售計數是前十位數字之一,則必須在名稱旁顯示一個小圖片。

可以這樣做嗎?

<?php 
    // Get all the data from the "sales" table 
    $result = mysql_query("SELECT * FROM sales ORDER BY SalesName") or die(mysql_error()); 
    echo "<table border='1'>"; 
    echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>"; 
    while($row = mysql_fetch_array($result)) { 
    echo "<tr><td>"; 
    echo $row['SalesName']; 
    echo "</td><td>"; 
    echo $row['SalesCount']; 
    echo "</td></tr>"; 
    } 
    echo "</table>"; 
?> 

回答

0

是的,你可以做到這一點:

<?php 
     $item_no=1 // number of current items 
     // Get all the data from the "sales" table 
     $result = mysql_query("SELECT * FROM sales ORDER BY SalesCount desc SalesName ") or die(mysql_error()); 
     echo "<table border='1'>"; 
     echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>"; 

     while($row = mysql_fetch_array($result)) { 
     echo "<tr><td>"; 
     if($item_no <= 10){ 
      echo 'your image here'; 
      $item_no++; 
     } 

     echo $row['SalesName']; 
     echo "</td><td>"; 
     echo $row['SalesCount']; 
     echo "</td></tr>"; 
     } 
     echo "</table>"; 
    ?> 
0

嘗試是這樣的:

$result = mysql_query("SELECT * FROM sales ORDER BY SalesName") or die(mysql_error()); 
$result2 = mysql_query("SELECT id FROM sales ORDER BY SalesCount DESC limit 10") or die(mysql_error()); 
while($savedResult = mysql_fetch_array($result2)) { 
    $topten[] = $savedResult[0]; 
} 
echo "<table border='1'>"; 
echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>"; 
while ($row = mysql_fetch_array($result)) { 
    echo "<tr><td>"; 
    echo $row['SalesName']; 
    if (in_array($row['id'],$topten)) { 
     echo '<img src="star.gif"/>'; 
    } 
    echo "</td><td>"; 
    echo $row['SalesCount']; 
    echo "</td></tr>"; 
} 
echo "</table>"; 

這將其獲取id列前十名的訂單由銷售計數的第二個查詢。然後我將這些id s存儲在一個數組中($topten)。在顯示錶格的循環中,我會檢查正在處理的行是否位於前十個數組中 - 如果是這樣,請添加一個星號!

0

你可以做類似

<?php 
    // Get all the data from the "sales" table 
    $result = mysql_query("SELECT * FROM sales ORDER BY SalesName") or die(mysql_error()); 
    $topTen = mysql_query("SELECT SalesName FROM sales ORDER BY SalesCount LIMIT 10") or die(mysql_error()); 
    $topTenArray = array(); 
    while($row = mysql_fetch_array($result)) { 
     $topTenArray[] = $row['SalesName']; 
    } 
    echo "<table border='1'>"; 
    echo "<tr> <th>Sales Name</th> <th>Sales Count</th> </tr>"; 
    while($row = mysql_fetch_array($result)) { 
    echo "<tr><td>"; 
    echo $row['SalesName']; 
    echo "</td><td>"; 
    echo $row['SalesCount']; 
    if(in_array($row['SalesName'],$topTenArray){ 
     echo "<img src.... />"; 
    } 
    echo "</td></tr>"; 
    } 
    echo "</table>"; 
?>