HyperT
HyperT

Reputation: 23

Looping through two arrays -> mark checkbox check

Iam stuck when comparing two arrays.

In first array, I have IDs of rows, where I need to mark checkbox ckecked. In second array, there is a full dataset of items from table.

I had no luck to make it work properly yet - here is my last attempt which obviously don't work properly as it does everything 6 times (as first array contains 6 elements)

First array: array(6) { [0]=> string(2) "24" [1]=> string(2) "25" [2]=> string(2) "28" [3]=> string(1) "8" [4]=> string(1) "9" [5]=> string(2) "10" }

$products - array of 6 elements

$data - array of dataset pulled from mysql

foreach($datas as $data) { 
if(!empty($products)) {
    foreach($products as $product) { 
        if($product == $data->id) { ?>
            <tr>
            <td><input type="checkbox" name="selectedProduct[]" value="<?=$data->id;?>" class="big-checkbox" checked="checked">
            <td><?=$data->evidenceId;?></td>
            <td><?=$data->modelName;?></td>
            <td><?=$data->size;?></td>
            <td><?=$data->price;?></td>
            <td><?=$data->brand;?></td>
            </tr>
        <?php } 
        if($product !== $data->id) { ?>
            <tr>
            <td><input type="checkbox" name="selectedProduct[]" value="<?=$data->id;?>" class="big-checkbox">
            <td><?=$data->evidenceId;?></td>
            <td><?=$data->modelName;?></td>
            <td><?=$data->size;?></td>
            <td><?=$data->price;?></td>
            <td><?=$data->brand;?></td>
            </tr>
        <?php }
    }
} 

What I need is to render each row from $data and mark box checked where $data->id == $product

Any advices? Thank you! Martin

EDIT:

Also tried approach like this, but it only screwed the html table:

foreach($datas as $data) { 
                                    foreach($selectedProducts as $product) {
                                        if($product == $data->id) { ?>
                                            <tr>
                                            <td><input type="checkbox" name="selectedProduct[]" value="<?=$data->id;?>" class="big-checkbox" checked="checked"> 
                                        <?php }
                                        if($product !== $data->id) { ?>
                                            <tr>
                                            <td><input type="checkbox" name="selectedProduct[]" value="<?=$data->id;?>" class="big-checkbox">
                                        <?php }
                                    }
                                ?>

                                        <td><input type="checkbox" name="selectedProduct[]" value="<?=$data->id;?>" class="big-checkbox">
                                        <td><?=$data->evidenceId;?></td>
                                        <td><?=$data->modelName;?></td>
                                        <td><?=$data->size;?></td>
                                        <td><?=$data->price;?></td>
                                        <td><?=$data->brand;?></td>
                                    </tr>
                                <?php } ?>

Upvotes: 1

Views: 122

Answers (2)

alamnaryab
alamnaryab

Reputation: 1484

try this code
assuming your products array contains (model name, size...)
and data array contain product ids of selected products

<?php
//collect seelcted IDs
$selectedIds = array();
foreach($datas as $data){
    $selectedIds[]=$data->id;
}
//now loop through products, and check if current product_id exists (in_array()) in selectedIds echo checked else nothing
foreach($products as $product) { 
?>
    <tr>
        <td><input type="checkbox" name="product_n" value="<?=$product->id;?>" class="big-checkbox" <?php echo (in_array($product->id,$selectedIds?"checked='checked'":"")) ?> >
        <td><?=$product->evidenceId;?></td>
        <td><?=$product->modelName;?></td>
        <td><?=$product->size;?></td>
        <td><?=$product->price;?></td>
        <td><?=$product->brand;?></td>
    </tr>
<?php
    }
?>

Upvotes: 0

some-non-descript-user
some-non-descript-user

Reputation: 616

If $products contains the product ids you can check if a products has to be selected with in_array(). No need for a second loop.

if(!empty($products)) {
    foreach($datas as $data) { 
    ?>
        <tr>
    <?php 
        if(in_array($data->id, $products) { 
    ?>
            <td><input type="checkbox" name="selectedProduct[]" value="<?=$data->id;?>" class="big-checkbox" checked="checked"></td>
    <?php
        }else{
    ?>
            <td><input type="checkbox" name="selectedProduct[]" value="<?=$data->id;?>" class="big-checkbox"></td>
    <?php
        }
    ?>
       <td><?=$data->evidenceId;?></td>
       <td><?=$data->modelName;?></td>
       <td><?=$data->size;?></td>
       <td><?=$data->price;?></td>
       <td><?=$data->brand;?></td>
       </tr>

    }
} 

...and your code lacks a closing td tag.

Upvotes: 1

Related Questions