douchuxun4162 2016-03-23 12:48
浏览 75
已采纳

PHP / MYSQL中的条件的LOOP错误

I am trying to get an output pertaining to some conditions with a query and IF condition. Just want to know, is this the correct way to achieve the result or is there a better way. The following script doesnt execute correctly at times. If you see my php script, you will be clear on what iam trying to do. Pls help me on the same.

$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';
    }
}

Im just checking in the rows if Slab Range or Part Number exists. If both exists it's a different echo, if both doesnt exists its a different echo or if any one exists or not its a different echo. As of now its just checking the first record. How will i add a for loop or a while loop here to check all the records and give a display as per that.

These are the expected results iam trying to achieve.

=========================================
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.
  • 写回答

1条回答 默认 最新

  • doudi1979 2016-03-24 05:10
    关注

    I think as @DerVO mentioned, just change the if to while but you need add a flag to indicate the results as you mentioned above.

    
        $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';
        }
    
    


    Hope this is what you want your results be.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面