CodeIgniter Forums
insert batch with group checkbox - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: insert batch with group checkbox (/showthread.php?tid=76910)



insert batch with group checkbox - eleumas - 07-02-2020

Hi. I have a group of checkbox like this:

Code:
<input type="checkbox" name="preview[]" value="1"/>
<input type="checkbox" name="preview[]" value="1"/>
<input type="checkbox" name="preview[]" value="1"/>

and the follow code in my controller:

PHP Code:
for($i 0$i $count$i++)
      {

        foreach($this->request->getPost('preview') as $preview)
        {
          $c = array('preview' => $preview);

          $data[$i] = array(
            'article_id' => $this->request->getPost('article_id'),
            'name'       => $names[$i],
            'text'       => $this->request->getPost('text')[$i],
            'title'      => $this->request->getPost('title')[$i],
            'alt'        => $this->request->getPost('alt')[$i],
            'preview'    => $c,
          );
        }
      }

      $imagesModel = new ImagesModel();
      $save $imagesModel->insertBatch($data); 

The value of checkbox is wrong. This is or always 1 or always 0. I have tried too the ternary operator, something like this:

PHP Code:
$c $this->request->getPost('preview') == 0

but the problem has not been resolved.

Someone can help me please? Thanks  Smile


RE: insert batch with group checkbox - marcogmonteiro - 07-02-2020

I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same.

Maybe if you provide the entire form HTML for more context and explain what you're really trying to do we could help you better.


RE: insert batch with group checkbox - eleumas - 07-02-2020

(07-02-2020, 06:41 AM)marcogmonteiro Wrote: I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same.

Maybe if you provide the entire form HTML for more context and explain what you're really trying to do we could help you better.

Hi Marco, thanks for help me. I would like use the checkbox for select the preview images. The preview images should have a value 1 the others 0.

[Image: support.png]


RE: insert batch with group checkbox - DarkKnight - 07-02-2020

(07-02-2020, 07:19 AM)eleumas Wrote:
(07-02-2020, 06:41 AM)marcogmonteiro Wrote: I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same.

Maybe if you provide the entire form HTML for more context and explain what you're really trying to do we could help you better.

Hi Marco, thanks for help me. I would like use the checkbox for select the preview images. The preview images should have a value 1 the others 0.

[Image: support.png]

Why don't you pass Product id to checkbox instead 1 for all


RE: insert batch with group checkbox - eleumas - 07-02-2020

(07-02-2020, 11:26 AM)DarkKnight Wrote:
(07-02-2020, 07:19 AM)eleumas Wrote:
(07-02-2020, 06:41 AM)marcogmonteiro Wrote: I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same.

Maybe if you provide the entire form HTML for more context and explain what you're really trying to do we could help you better.

Hi Marco, thanks for help me. I would like use the checkbox for select the preview images. The preview images should have a value 1 the others 0.

[Image: support.png]

Why don't you pass Product id to checkbox instead 1 for all

Ok i can pass the product id but the problem is the same. what change if i pass product id? please, can you explain better. Thanks.


RE: insert batch with group checkbox - ojmichael - 07-02-2020

Your code assumes a checkbox that is not selected will still submit a value.. that's never going to work. I'm also not sure that nested loops are necessary here?

Your field names should be something like:
Code:
Row 1: images[0][title], images[0][alt], images[0][text] and images[0][preview]
Row 2: images[1][title], images[1][alt], images[1][text] and images[1][preview]
... etc

Then you would do like this:

PHP Code:
$images = []];
foreach (
$this->request->getPost('images') AS $i => $image) {
    $image['article_id'] = $this->request->getPost('article_id');
    $image['name'] = $names[$i];
    $image['preview'] ??= 0;
    $images[] = $image;
}

$ImagesModel = new ImagesModel();
$ImagesModel->insertBatch($images); 



RE: insert batch with group checkbox - marcogmonteiro - 07-03-2020

If you have this:

PHP Code:
<input type="checkbox" name="preview[]" value="<?= $product->id ?>"/> 


then in your controller you would do


PHP Code:
foreach($this->request->getPost('preview) as $key => $product_id {
  // Your code for that product here, because your $_POST['
preview'] only has the selected products