【问题标题】:fetching array from sql and checking with conditions从 sql 获取数组并检查条件
【发布时间】:2016-08-04 00:08:12
【问题描述】:

我需要帮助...我正在尝试从 sql 表中检索数据并将其与某些 ID 的 if 语句进行比较,并相应地更新变量。但似乎该变量由于某种原因没有更新。下面是我的代码..

$query2 = "SELECT prcID, tProDone 
          FROM vw_fdwTracker
          WHERE AgrNo = '$agreement'";  

$result2= sqlsrv_query($conn, $query2);
    if ($result2==false){
        die( "<pre>".print_r(sqlsrv_errors(), true));
    }   

$current = 0;       
while($id= sqlsrv_fetch_array($result2, SQLSRV_FETCH_ASSOC)){       

    //echo $id['prcID']." ". $id['tProDone'].'<br>';

    if(($id['prcID']===3) && ($id['tProDone']===TRUE)){
        $current=12.5;
    }elseif(($id['prcID']===4) && ($id['tProDone']===TRUE)){
        $current=25;
    }elseif(($id['prcID']===5) && ($id['tProDone']===TRUE)){
        $current=37.5;
    }elseif(($id['prcID']===9) && ($id['tProDone']===TRUE)){
        $current=50;
    }elseif(($id['prcID']===10) && ($id['tProDone']===TRUE)){
        $current=62.5;
    }elseif(($id['prcID']===14) && ($id['tProDone']===TRUE)){
        $current=75;
    }elseif(($id['prcID']===12) && ($id['tProDone']===TRUE)){
        $current=87.5;
    }elseif(($id['prcID']===17) && ($id['tProDone']===TRUE)){
        $current=100;
    }else{
        $current=0;
    }
}

【问题讨论】:

  • 你的意思是$current 没有更新吗?如果你的意思是$current,那么如果你有多行,它会在每个循环中覆盖自己。
  • 另外,如果他们都要求$id['tProDone'] 为真,为什么不为if($id['tProDone'] === true) { } 做一个包装,然后在里面做一个开关?它可能会更干净一些。这样你就不需要所有的&amp;&amp; ($id['tProDone']===TRUE
  • 是的 $current 没有更新..我有多个具有不同 ID 的行,我想检查某些 ID 并相应地更新 $current...所以即使它自己覆盖它也应该有一些价值最后,但在这里我没有得到任何输出......最后 $current 仍然是 0....
  • 您确定需要=== 而不仅仅是==
  • === 和 == 都没有给我任何结果...虽然我不需要 === 我只是把它放在那里检查它是否提供任何输出..

标签: php sql arrays while-loop


【解决方案1】:

您不需要在代码开始时定义 $current 变量值,只需像下面这样执行您的代码,它也将在循环中工作

$id['prcID']=3;
$id['tProDone']=false;   

if(($id['prcID']==3) && ($id['tProDone']==true)){
   $current=12.5;
}else{
   $current=0;
}
echo $current;

【讨论】:

  • 感谢 ram singh 的建议。我知道现在需要它,但提前声明变量总是更好......它现在仍在工作的任何方式......
【解决方案2】:

尝试保存到数组中,以便知道它是否有效:

function getCurrentArr($agreement,$conn)
    {
        $query = "SELECT prcID, tProDone FROM vw_fdwTracker WHERE AgrNo = '$agreement'";
        $result  =  sqlsrv_query($conn, $query);
        if(!$result){
            die( "<pre>".print_r(sqlsrv_errors(), true));
        }

        return $result;
    }

function getCurrVal($value)
    {
        $return[3]    =    12.5;
        $return[4]    =    25;
        $return[5]    =    37.5;
        $return[9]    =    50;
        $return[10]   =    62.5;
        $return[14]   =    75;
        $return[12]   =    87.5;
        $return[17]   =    100;
        return (isset($return[$value]))? $return[$value] : 0;
    }

$curr    =  array();
$result  =  getCurrentArr($agreement,$conn);
while($id= sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
    if(!$id['tProDone']) {
        $curr[]    =    0;
        continue;
    }

    $curr[]    =    getCurrVal($id['prcID']);
}
// See what this gets you for an array
// If what is in this array is what you expect, then
// make the $curr array the variable, but you will overwrite
// every time it loops, just keep that in mind
print_r($curr);

【讨论】:

  • 我尝试了这个解决方案,这是它的输出...Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4 ] => 12.5 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] = > 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0)
  • 这就是为什么,只有数组中第 4 个键的值具有有效条件(true 并匹配一个 id)
  • 另外,当在循环中设置一个变量时,最后一行值,在本例中为 0,将应用于变量。
  • 但是只有在条件匹配时才应将任何值应用于变量,但在这里它以某种方式将其更改为零而没有条件匹配......我有其他数据匹配 4 个条件......现在我的问题是我正在使用这个变量值通过 css 更新进度条......它是一个数组正在给出错误......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
相关资源
最近更新 更多