2012-03-08 81 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>"; 
?>