我有一个查询,可以获取我需要在网页上显示的一些数据。这是我的疑问:
$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
参数。
答案 1 :(得分: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);
?>