2017-09-28 39 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

相关问题