Skip to content
Advertisement

DO while based on query result

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?

Advertisement

Answer

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);
    }
?>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement