• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
insert batch with group checkbox

#1
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
Reply

#2
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.
Reply

#3
(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]
Reply

#4
(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
Reply

#5
(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.
Reply

#6
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); 
Reply

#7
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

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.