在PHP中使用MySQL的结果旁边显示图像?

时间:2012-03-08 11:29:18

标签: 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>";
?>

3 个答案:

答案 0 :(得分: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存储在一个数组($topten)中。在显示表格的循环中,我检查正在处理的行是否在前十个数组中 - 如果是这样,请添加一个星号!

答案 1 :(得分: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>";
?>

答案 2 :(得分: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>";
    ?>