Viral Bhoot
Viral Bhoot

Reputation: 357

How can I use other variable value with for loop

Here, I want to use other variable value in for loop as I want to get that records. Here is my code,

$chk = $_SESSION['id'];
    $altxt= $_POST['text'];
    for($i=0; $i < sizeof($altxt); $i++)
    {
        if(!empty($altxt[$i]))
        {
            echo "<br>";
            echo "update order_details set process_order='1' tracking_number=$altxt[$i] where id IN(".implode(',',$chk).")";
        }
    }

I am getting like this,

update order_details set process_order='1' tracking_number=safdf where id IN(8,6)

and

I want,

update order_details set process_order='1' tracking_number=safdf where id IN(8)
update order_details set process_order='1' tracking_number=safdf where id IN(6)

Here, I am getting value of tracking number that is $altxt[$i] but I also want to get record of id that I coudn't get..

So, how it can be use?

The more code for this,

if ($order_list) {
                                    $i = $start +1;
                                    foreach ($order_list as $row) {
                                    ?>
                                        <tr class="border-bottom border-top">
                                            <td class="align-center">
                                                <input type="checkbox" name="id[]" id="id_<?php echo $row['id']; ?>" value="<?php echo $row['id'];?>" <?php if(in_array($row['id'], $_SESSION['id'])) echo checked; ?>>
                                            </td>
                                            <td class="align-center" id="id"><?php echo $i; ?></td>
                                            <td class="align-center"><?php echo $row['order_id']; ?></td>
                                            <td class="align-center"><?php echo $row['order_item_id']; ?></td>
                                            <td class="align-center"><?php echo $row['product_name']; ?></td>
                                            <td class="align-center">
                                                <input type="text" name="text[]" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php $row['text'] ?>">
                                            </td>
                                        </tr>
                                    <?php
                                    $i++;
                                }

Upvotes: 0

Views: 56

Answers (1)

swidmann
swidmann

Reputation: 2792

You can use 2 foreach loops instead, you should also use WHERE ID = $id instead of WHERE ID IN ( $id )

Due to your example:

$altxt = array( "", "test", "test1","", "", "", "", "", "", "");
$chk = array( "3", "4" );

you can try this code with array_shift():

for($i=0; $i < sizeof($altxt); $i++)
{
    if(!empty($altxt[$i]))
    {
        echo "<br>";
        echo "update order_details set process_order='1' tracking_number=$altxt[$i] where id=".  array_shift( $chk );
    }
}

will output this:

update order_details set process_order='1' tracking_number=test where id=3
update order_details set process_order='1' tracking_number=test1 where id=4

Upvotes: 1

Related Questions