我一直在做一个Web应用程序,在该应用程序中检索所有我选择的乐透彩票组合及其对应的中奖组合。这是它的工作方式/我的分步工作:
if($Picked1 == $Win1 || $Picked1 == $Win2 || $Picked1 == $Win3 || $Picked1 == $Win4 ||$Picked1 == $Win5 || $Picked1 == $Win6){
echo "<tr><td>$Win1</td>
<td>$Win2</td>
<td>$Win3</td>
<td>$Win4</td>
<td>$Win5</td>
<td>$Win6</td>
<td bgcolor = '#008000'>$Picked1</td>
<td>$Picked2</td>
<td>$Picked3</td>
<td>$Picked4</td>
<td>$Picked5</td>
<td>$Picked6</td></tr>";
当前输出(有逻辑错误)
如果从抽奖的乐透中有不止一个组合与乐透中奖的组合相匹配,则将同一行重复到下一行。
例如:如果有4个与中奖组合匹配的乐透组合,那么它将重复4次(4个表行):
这是我当前输出的图像:
目标输出
我想要修复的是重复打印行并在所选择的乐透组合与中奖组合匹配的情况下标记表格单元格。如果将其选中的乐透组合与对应的中奖乐透组合相匹配,则打印一行,并使单元格的bgcolor变为绿色。
问题
如何解决我的算法?还是我的算法从一开始就是错误的?
这是我的完整代码:https://pastebin.com/4gN9zqZZ
答案 0 :(得分:1)
问题在于,在每个语句中,您反复回显 TD 。因此,它被复制了。相反,您应该按照以下if语句仅使用它一次:
echo "<tr><td>$Win1</td>
<td>$Win2</td>
<td>$Win3</td>
<td>$Win4</td>
<td>$Win5</td>
<td>$Win6</td>";
if($Picked1 == $Win1 || $Picked1 == $Win2 || $Picked1 == $Win3 || $Picked1 == $Win4 ||$Picked1 == $Win5 || $Picked1 == $Win6){
echo "<td bgcolor = '#008000'>$Picked1</td>";
else{
echo "<td>$Picked1</td>";
}
if($Picked2 == $Win1 || $Picked2 == $Win2 || $Picked2 == $Win3 || $Picked2 == $Win4 ||$Picked2 == $Win5 || $Picked2 == $Win6){
echo "<td bgcolor = '#008000'>$Picked2</td>";
else{
echo "<td>$Picked2</td>";
}
if($Picked3 == $Win1 || $Picked3 == $Win2 || $Picked3 == $Win3 || $Picked3 == $Win4 ||$Picked3 == $Win5 || $Picked3 == $Win6){
echo "<td bgcolor = '#008000'>$Picked3</td>";
else{
echo "<td>$Picked3</td>";
}
//And so on...
echo "</tr>";
答案 1 :(得分:1)
代码:
<?php
$winning_combinations = [$Win1,$Win2,$Win3,$Win4,$Win5,$Win6];
while ($Row2 = mysqli_fetch_array($Picked_Lotto_Query, MYSQLI_ASSOC)){
$picked_db_values = [];
$picked_db_values[] = $Row2['First_Combination'];
$picked_db_values[] = $Row2['Second_Combination'];
$picked_db_values[] = $Row2['Third_Combination'];
$picked_db_values[] = $Row2['Fourth_Combination'];
$picked_db_values[] = $Row2['Fifth_Combination'];
$picked_db_values[] = $Row2['Sixth_Combination'];
foreach($picked_db_values as $each_index => $each_combination){
echo "<tr>
<td>$Win1</td>
<td>$Win2</td>
<td>$Win3</td>
<td>$Win4</td>
<td>$Win5</td>
<td>$Win6</td>";
$combination_present = in_array($each_combination,$winning_combinations);//store it instead of checking in every iteration
foreach($picked_db_values as $comb_index => $print_each_comb){
if($combination_present && $each_index === $comb_index){
echo "<td class='correct_combination'>";
}else{
echo "<td>";
}
echo $print_each_comb;
echo "</td>";
}
}
echo "</tr>";
}
}
在您的 style.css 中,只需编写
.correct_combination{
background-color:#008000;
}