douxie0824 2019-02-15 11:17
浏览 261
已采纳

根据查询结果做DO

I got a block of the code written in PHP it uses a DB connection to an Oracle DB, i need to run a query on a data and the result for the first query it should be an input of the next query and this should be inside a loop, if i don't got any more result from the next query i should exit the loop, at the moment i am using and integer to loop through max 5 times as i know this the maximum possible results received, but this is dynamic.

My actual code what is working is the following:

this is the form when i input the data

<form name="identificare_cusut" action="test_hlkz.php" method="post">
   Scanati piesa: <input type="text" name="piesa"><br>
   <input type="submit" value="Cautare">
</form>

php code where i verify data and use function to execute query on DB while i am running a loop.

    if (isset($_POST['piesa']) && strlen($_POST['piesa'])>0) {
    $barcode = $_POST['piesa'];
}
if (isset($barcode)) {
    echo "<tr>";
    echo "<th>".$barcode."</th>";
    echo "</tr>";
    array_push($trasabilitate_array, $barcode);
    $x=1;
    do {
        $arr=get_query_results_hlkz($barcode);
        if ($arr) {
            foreach ($arr as $row) {
                echo "<tr>";
                echo  "<th>" .$row[0]. "</th>";
                echo "</tr>";
                $barcode=$row[0];
                array_push($trasabilitate_array, $row[0]);
            }
        }
        $x++;
    } while ($x <=5);
}

i don't want to use a static solution like while ($x <=5)

Any idea how to fulfill this in a different way?

  • 写回答

1条回答 默认 最新

  • douxi0098 2019-02-15 12:09
    关注

    Should do the trick. So if the next $arr is empty it will exit the loop.

    <?php  
        if (isset($_POST['piesa']) && strlen($_POST['piesa'])>0) {
            $barcode = $_POST['piesa'];
        }
        if (isset($barcode)) {
    
            echo "<tr>";
                echo "<th>".$barcode."</th>";
            echo "</tr>";
    
            array_push($trasabilitate_array, $barcode);
            $x=1;
            do {
                $arr=get_query_results_hlkz($barcode);
                if ($arr != null) {
                    foreach ($arr as $row) {
                            if ($arr == null) {
                                $x = 6;
                            }
                            if($x < 6){
                                echo "<tr>";
                                    echo  "<th>" .$row[0]. "</th>";
                                echo "</tr>";
    
                                $barcode=$row[0];
    
                                array_push($trasabilitate_array, $row[0]);
                            }
                        }
                    $x++;
                }
            } while ($x <=5);
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型