-3

コードは次のとおりです。

<?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
----------------------------

2回繰り返しています。私のコードで何が間違っていますか? どうぞ、どんな助けでも大歓迎です。

どうもありがとう。

4

3 に答える 3

0

PHP でデータをフィルタリングするのではなく、データベースに個別のデータを要求してみてください。明確な言葉を強調します(それはあなたのグーグルへの合図です...)。

于 2012-09-14T03:00:58.927 に答える
0
  1. 必要な列のみを選択してください。
  2. 複数の全体を取得しないためには、ある種の追加の修飾子が必要です。最近の年を表示したいので、GROUP BY を使用して個別の場所を選択し、ORDER BY を使用して最新の年を表示することができます。
  3. コードは醜いです...変更された部分をjsutで置き換えることができるのに、同じSQLを繰り返し続けます。

すべてをまとめると

<?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; ?>
于 2012-09-14T22:22:59.453 に答える
0

mysql_fetch_assocではなく、使用してくださいmysql_fetch_array

于 2012-09-14T01:33:13.103 に答える