数组中的重复数据

时间:2012-09-14 01:25:57

标签: php mysql html

代码是:

<?php

// my query

if (isset($_GET['ssiptype']))
{
    if (($_GET['ssiptype']) == 'gma'){
        $query = "SELECT location, year, serviceArea, noOfBenificiaries, stat, fundingSrc, projCost 
            FROM swipdd
            WHERE fundingSrc LIKE 'GMA-Rice%' AND ssiptype = 'SWIP'" ;
    }
    elseif (($_GET['ssiptype']) == 'am'){
        $query = "SELECT location, year, serviceArea, noOfBenificiaries, stat, fundingSrc, projCost 
            FROM swipdd
            WHERE fundingSrc LIKE 'AM-Rice%' AND ssiptype = 'SWIP'" ;
    }
    elseif (($_GET['ssiptype']) == 'hvcccred'){
        $query = "SELECT location, year, serviceArea, noOfBenificiaries, stat, fundingSrc, projCost 
            FROM swipdd
            WHERE fundingSrc LIKE 'HVCC RED%' AND ssiptype = 'SWIP'" ;
    }
    elseif (($_GET['ssiptype']) == 'hvcc'){
        $query = "SELECT location, year, serviceArea, noOfBenificiaries, stat, fundingSrc, projCost 
            FROM swipdd
            WHERE fundingSrc LIKE 'HVCC%' AND ssiptype = 'SWIP'" ;
    }
    else{}
}
// output

if (isset($query))
{
  $result = mysql_query($query) or die(mysql_error()); 

  echo "<table border='1' cellpadding='3'>";
  echo "<tr> 
            <td bgcolor=\"#DFE8EC\">LOCATION</td>
            <td bgcolor=\"#DFE8EC\">YEAR</td>
            <td bgcolor=\"#DFE8EC\">STATUS</td>
        </tr>";
  while($row = mysql_fetch_array( $result )) {

  $final_result = array_unique($row);

    echo "<tr>";
    echo '<td>' . $row['location'] . '</td>';
    echo '<td>' . $row['year'] . '</td>';
    echo '<td>' . $row['stat'] . '</td>';
    echo "</tr>"; 
  } 
  echo "</table>";
}
?>

我想要的输出如下:

//dummy data
LOCATION    YEAR   STATUS
---------------------------
park         1999   ok
----------------------------
sea          2000   fine
----------------------------

我收到了这个输出:

LOCATION    YEAR   STATUS
----------------------------
park         1999   ok
----------------------------
sea          2000   fine
----------------------------
park         1999   ok
----------------------------
sea          2000   fine
----------------------------
park         1999   ok
----------------------------
sea          2000   fine
----------------------------

重复两次。我的代码有什么问题?请,任何帮助将不胜感激。

  

非常感谢。

3 个答案:

答案 0 :(得分:0)

使用mysql_fetch_assoc,而不是mysql_fetch_array

答案 1 :(得分:0)

尝试向数据库询问不同的数据,而不是让PHP过滤掉它。强调单词 distinct (这是你对谷歌的暗示......)。

答案 2 :(得分:0)

  1. 仅选择您需要的列。
  2. 为了不获得多个内容,你需要一些额外的限定符。我将假设您希望显示最近一年,以便我们可以使用GROUP BY选择不同的位置,并使用ORDER BY来显示最近一年
  3. 代码很难看......当你可以替换更改的部分时,你会不断重复相同的SQL。
  4. 所以把它们放在一起

    <?php 
    
    $fundingSrcs = array(
      'gma' => 'GMA-Rice%',
      /* repeat for your other options */
    );
    $sql = "SELECT location, year, stat 
           FROM swipdd
           WHERE fundingSrc LIKE %s AND ssiptype = 'SWIP'
           ORDER BY year DESC
           GROUP BY location";
    
    $result = false; // initialize as a default of false
    
    if(isset($_GET['ssiptype']) {
       $ssitype = $_GET['ssiptype'];
       if(isset($fundingSrcs[$ssitype]) {
    
          // substitute the db quoted string for the %s
          $query = sprintf($sql, mysql_real_escape_string($fundingSrcs[$ssitype]));
          $result = mysql_query($query) or die(mysql_error());
       }
    }
    ?>
    
    <?php if($result && mysql_num_rows($result)): ?>
      <table border='1' cellpadding='3'>
        <thead>
          <tr>
            <th bgcolor="#DFE8EC">LOCATION</th>
            <th bgcolor="#DFE8EC">YEAR</th>
            <th bgcolor="#DFE8EC">STATUS</th>
          </tr>
        </thead>
        <tbody>
        <?php while ($row = mysql_fetch_array($result)): ?>
          <tr>
            <td><?php echo $row['location'] ?></td>
            <td><?php echo $row['year'] ?></td>
            <td><?php echo $row['stat'] ?></td>
          </tr>
        <?php endwhile; ?>
        </tbody>
      </table>
    <?php else if($err = mysql_error()): ?>
      <p>There was an error executing this request.</p>
    <?php else: ?>
      <p>No matching entries found</p>
    <?php endif; ?>