2017-09-28 40 views
0

我有一個查詢獲取一些數據,我需要在我的網頁上顯示。這裏是我的查詢:在不使用循環的情況下顯示查詢中的PHP數據

$sql1 = "SELECT CAST([Series] AS INT) AS Series 
     ,[Master Supplier Title] 
     ,[Fund Name] 
     ,CAST([Agreement_ID] AS INT) AS Agreement_ID 
     ,CAST([Tier_ID] AS INT) AS Tier_ID 
     ,[Retro_to_1] 
     ,CAST([Payments per Year] AS INT) AS [Payments per Year] 
     ,[Condition Unit of Measure] 
     ,CAST([Condition Minimum] AS INT) AS [Condition Minimum] 
     ,CAST([Condition Maximum] AS INT) AS [Condition Maximum] 
     ,CAST([Incentive Multiplier] AS DEC(5,4)) AS [Incentive Multiplier] 
    FROM [Test].[dbo].[vExample] 
    WHERE [Master Supplier Title] = '$supp' AND [Series] = 1 AND [Fund Name] = '400P' AND [Agreement_ID] = 2 
    ORDER BY [Master Supplier Title]"; 

然後我使用這個代碼塊來顯示所需要的結果:

<?php foreach ($pdo->query($sql1) as $supp11) { ?> 
      <label>Agreement ID:</label><input value="<?php echo $supp11['Agreement_ID'];?>" readonly><br><br><br> 
      <table> 
       <tr> 
       <thead> 
        <th></th> 
        <th></th> 
        <th></th> 
        <th></th> 
        <th></th> 
        <th></th> 
        <th></th> 
       </thead> 
       </tr> 
       <?php foreach ($pdo->query($sql1) as $supp22) { ?> 
       <tr> 
       <tbody> 
        <td><?php echo $supp22['Tier_ID'];?></td> 
        <td><?php echo $supp22['Incentive Multiplier'];?></td> 
        <td><?php echo $supp22['Condition Minimum'];?></td> 
        <td><?php echo $supp22['Condition Maximum'];?></td> 
        <td><?php echo $supp22['Condition Unit of Measure'];?></td> 
        <td><?php echo $supp22['Retro_to_1'];?></td> 
        <td><?php echo $supp22['Payments per Year'];?></td> 
       </tbody> 
       </tr> 
       <?php } ?> 
      </table> 
      <?php } ?> 

您看到的變量$supp的是,我從下拉列表中選擇獲得值我有一些以前的代碼。

每當我做出下拉選擇時,它會顯示正確的數據。但是,我只需要它在一張表中顯示一次。例如,選擇可能在數據庫的多行中。因此,只要它在表格中顯示結果,它就會在表格中顯示數據庫中下拉選擇的次數。所以,如果我的$supp變量中的值在數據庫中看到了8次,那麼我只需要一次一個表中的該信息(8行)。不是像我目前正在獲取的八個不同時間。

我該如何解決這個問題?

+0

如果數值始終相同,只需將其顯示在循環外。 –

回答

1

我認爲問題是你有一個雙循環進行,導致重複。如果你想把所有的結果作爲一個集合來處理,你應該使用一個準備好的語句。

<?php 
$stmt = $pdo->prepare($sql1); 
$stmt->execute(); 
$results = $stmt->fetchAll(); 
?> 
<label>Agreement ID:</label><input value="<?php echo $results[0]['Agreement_ID'];?>" readonly><br><br><br> 
<table> 
<tr> 
<thead> 
    <th></th> 
    <th></th> 
    <th></th> 
    <th></th> 
    <th></th> 
    <th></th> 
    <th></th> 
</thead> 
</tr> 
<?php foreach ($results as $supp22) { ?> 
<tr> 
<tbody> 
    <td><?php echo $supp22['Tier_ID'];?></td> 
    <td><?php echo $supp22['Incentive Multiplier'];?></td> 
    <td><?php echo $supp22['Condition Minimum'];?></td> 
    <td><?php echo $supp22['Condition Maximum'];?></td> 
    <td><?php echo $supp22['Condition Unit of Measure'];?></td> 
    <td><?php echo $supp22['Retro_to_1'];?></td> 
    <td><?php echo $supp22['Payments per Year'];?></td> 
</tbody> 
</tr> 
<?php } ?> 
</table> 

fetchAll()的調用應返回可循環的結果數組。查看索引0處的第一條記錄應該讓您獲得Agreement_ID參數爲您的頂級標籤。

+0

@OP並沒有要求這樣做,但他們應該修正他們的HTML太... https://stackoverflow.com/questions/5395228/html-tables-thead-vs-th –

0

您可以在查詢中使用DISTINCT以確保您只獲取每行的一個副本,但這可能會太慢,具體取決於數據庫的大小和結構。我可能會主張在你的<tbody>循環中添加一條IF語句。例如:

<?php 
$shown = array(); // keeps track of which values were already displayed 
foreach ($pdo->query($sql1) as $supp22) { 
    // this just has to be a value (or combination of values) 
    // that you don't want repeated. 
    // Since I don't know the exact structure of your tables, I've just 
    // appended all the variables together into one long string. If 'Tier_ID' 
    // is a unique row identifier you'd want to exclude it from here. 
    $dont_repeat = $supp22['Tier_ID'].$supp22['Incentive Multiplier'].$supp22['Condition Minimum'].$supp22['Condition Maximum'].$supp22['Condition Unit of Measure'].$supp22['Retro_to_1'].$supp22['Payments per Year']; 
    // here we only display the row if it wasn't already shown 
    if (!in_array($dont_repeat, $shown)) { 
    // add it to our $shown array so we don't display the same data again 
    $shown[] = $dont_repeat; 
    ?> 
    <tr> 
    <tbody> 
    <td><?php echo $supp22['Tier_ID'];?></td> 
    <td><?php echo $supp22['Incentive Multiplier'];?></td> 
    <td><?php echo $supp22['Condition Minimum'];?></td> 
    <td><?php echo $supp22['Condition Maximum'];?></td> 
    <td><?php echo $supp22['Condition Unit of Measure'];?></td> 
    <td><?php echo $supp22['Retro_to_1'];?></td> 
    <td><?php echo $supp22['Payments per Year'];?></td> 
    </tbody> 
    </tr> 
    <?php 
    } 
    ?> 
    </table> 
    <?php 
    } 
/* be tidy and */ unset($shown, $dont_repeat); 
?> 
+0

是的,我不認爲'DISTINCT'會基於我所需要的工作。我用這段代碼代替了我的內容,現在它給了我一個內部服務器錯誤 – Rataiczak24

+0

某處可能有一個未關閉的支架。如果你想確定,請檢查你的php錯誤日誌 - 它會告訴你到底是什麼產生了這個錯誤。或者顯示內聯錯誤:[link](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Typel

相關問題