gpt4 book ai didi

php - PHP/MYSQL 中的条件循环出错

转载 作者:行者123 更新时间:2023-11-29 21:13:19 25 4
gpt4 key购买 nike

我试图通过查询和 IF 条件获取与某些条件相关的输出。只是想知道,这是实现结果的正确方法还是有更好的方法。以下脚本有时无法正确执行。如果您看到我的 php 脚本,您就会清楚我要做什么。请帮助我。

$query_slab1 = mysql_query("SELECT slab_range,sup_itempartno FROM quotation_items where tender_id='$tender_id' ");

if ($result_slab1 = mysql_fetch_array($query_slab1)){

if (($result_slab1['slab_range'] == "") && ($result_slab1['sup_itempartno'] == "")){

echo 'NO SLAB RANGE or NO PART NUMBER';

}
else if (($result_slab1['slab_range'] !== "") && ($result_slab1['sup_itempartno'] !== "")){

echo 'SLAB RANGE and PART NUMBER EXISTS';

}
else if (($result_slab1['slab_range'] == "") || ($result_slab1['sup_itempartno'] !== "")){

echo 'SLAB RANGE is NOT THERE and PART NUMBER EXISTS';

}
else if (($result_slab1['slab_range'] !== "") && ($result_slab1['sup_itempartno'] == "")){

echo 'SLAB RANGE EXISTS and PART NUMBER IS NOT THERE';

}
else {
echo 'NO ITEMS FOUND IN THE DB';
}
}

我只是检查行中是否存在板范围或零件号。如果两者都存在,则为不同的 echo ,如果两者都不存在,则为不同的 echo ,或者如果其中任何一个存在或不存在,则为不同的 echo 。到目前为止,它只是检查第一条记录。我将如何在此处添加 for 循环或 while 循环来检查所有记录并据此进行显示。

这些是我试图实现的预期结果。

=========================================
tender_id | slab_range | sub_itempartno
=========================================

15001 ABCDE
15001 1-2 AMKOI
15001 5-6 OUIPR

15004
15004 CVIOU

15005
15005
15005

15009
15009 8-9
15009 6-9


Result for 15001 is 'SLAB RANGE AND PART NUMBER EXISTS' since when i iterete through the rows i can find both slab_range and sub_itempartno ENTERED.

Result for 15004 is 'SLAB RANGE DOESNT EXIST AND PART NUMBER EXISTS' since when i iterete ethrough the rows i can find both slab_range empty or null and sub_itempartno ENTERED.

Result for 15005 is 'SLAB RANGE AND PART NUMBER DOES NOT EXIST' since when i iterete through the rows i can find both slab_range and sub_itempartno NOT ENTERED.

Result for 15009 is 'SLAB RANGE EXISTS AND PART NUMBER DOES NOT EXIST' since when i iterete through the rows i can find slab_range ENTERED and sub_itempartno NOT ENTERED.

最佳答案

我认为正如@DerVO提到的,只需将if更改为while,但您需要添加一个标志来指示上面提到的结果。

    $query_slab1 = mysql_query("SELECT slab_range,sup_itempartno FROM quotation_items where tender_id='$tender_id' ");    $flagSlab=false;    $flagPart=false;    if(mysql_num_rows($query_slab1)>0){        while($result_slab1 = mysql_fetch_array($query_slab1)){            if (($result_slab1['slab_range'] !== "") && ($result_slab1['sup_itempartno'] !== "")){             //'SLAB RANGE and PART NUMBER EXISTS';             $flagSlab=true;             $flagPart=true;        }        else if (($result_slab1['slab_range'] == "") || ($result_slab1['sup_itempartno'] !== "")){            //'SLAB RANGE is NOT THERE and PART NUMBER EXISTS';            $flagPart=true;        }        else if (($result_slab1['slab_range'] !== "") && ($result_slab1['sup_itempartno'] == "")){            //'SLAB RANGE EXISTS and PART NUMBER IS NOT THERE';            $flagSlab=true;        }        if($flagSlab==true && $flagPart==true){             break;         }       }         if($flagSlab==true && $flagPart==true){             echo 'SLAB RANGE and PART NUMBER EXISTS';         }         else if($flagSlab==false && $flagPart==true){             echo 'SLAB RANGE is NOT THERE and PART NUMBER EXISTS';         }         else if($flagSlab==true && $flagPart==false ){             echo 'SLAB RANGE EXISTS and PART NUMBER IS NOT THERE';          }         else{             echo 'NO SLAB RANGE or NO PART NUMBER';          }    }else{         echo 'NO ITEMS FOUND IN THE DB';    }


希望这就是您想要的结果。

关于php - PHP/MYSQL 中的条件循环出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36178670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com