CakePHP only selecting last checkbox in a loop - php

I found other people that had the same problem when trying to select multiple checkboxes, only the last one is selected. Here is one and here is another.
People have suggested using options like this
echo $this->Form->input('video',array('options'=> array('Value 1'=>'Label 1',
'Value 2'=>'Label 2',
'Value 3'=>'Label 3'
),
'multiple' => 'checkbox'
));
I don't think either of these answer my question. My checkboxes are generated in a loop, but everyone suggested using "options" to set the values. I have no idea how many checkboxes are going to be generated so I don't see how this would work.
<table>
<?
echo $this->Form->create(null,array(
'onsubmit'=>'return confirm("Are you sure you want to archive?'));
?>
<th>Order ID</th><th>Order Date</th><th>Order Total</th><th>Status</th><th>View</th><th>Select to Archive</th>
<?php foreach ($orders as $order):
?>
<tr>
<td><?php echo $order['Order']['orderid'];?> </td>
<td><?php echo $formatted_date." ".$time; ?></td>
<td><?php echo $order['Order']['total'];?> </td>
<td><?php echo $order['Order']['order_status'];?> </td>
<td><a href="/orders/details/<?php echo $order['Order']['orderid']; ?>"/>View Order</a> </td>
<td><? echo $this->Form->checkbox('archive_value', array('hiddenField' => false, 'value' => $order['Order']['orderid'])); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php //options for the archiving values
$archive_options = array(
'selected' => 'Archive Selected Orders',
'filled' => 'Archive All Filled Orders',
);
?>
<table class = "table_order_status">
<tr>
<td>
<?
echo $this->Form->input('archive_values', array('options' => $archive_options, 'value' => $select_value, 'name' => 'archive'));
?>
</td>
<td>
<?
echo $this->Form->end(__('Submit'));
?>
</td>
</tr>
</table>
<?

Try appending a period to the end of the input element's name, i.e. video. (derived from this forum post). This will apparently cause the elements to adopt the array name style notation, e.g. video[], though it might be Model.video[]. In any case, this should allow you to receive all of the checked values back in the form data.

Related

Yii2 display serialized data in gridview

I have some data stored in serialized format.
I can pull the data and show it in view like this for the logged in user:
$user_id = Yii::$app->user->identity->id;
$kids=unserialize($model->kids);
$children =\app\models\UserChildren::find()->where(['user_id' =>$user_id])->andwhere(['id'=>$kids])->all();
?>
<?php foreach ($children as $child): ?>
<table class="table table-bordered table-hover">
<tr>
<th><?php echo Yii::t('app', 'Kids Name')?> : </th>
<td><?php echo $child->child_name?> </td>
<th><?php echo Yii::t('app', 'Kids Birth Date')?> : </th>
<td><?php echo $child->child_birth_date?> </td>
<th><?php echo Yii::t('app', 'Kids Gender')?> : </th><td><?php echo $child->child_gender?> </td>
</tr>
</table>
<?php endforeach; ?>
The kids data is stored in order table like this, which is linked to the table userchildren.
s:0:"";
a:4:{i:0;s:2:"10";i:1;s:2:"11";i:2;s:2:"25";i:3;s:2:"26";}
a:2:{i:0;s:2:"10";i:1;s:2:"11";}
I have tried to display the same in gridview, but couldn't make out how I can achieve this:
like for example if I am using
[
'label'=> 'Child',
'value' => function($data){
return $data->order->kids;
}
],
I am getting the same data as I have shown.
I also tried foreach, but I am getting invalid argument foreach.
Not sure If I have done correctly, but it appears to have the correct data.
The code I am using is like below:
[
'label'=> 'Child',
'value' => function($data){
$kids=((unserialize($data->order->kids)));
$children1=ArrayHelper::map(\app\models\UserChildren::find()->where(['id'=>$kids])->all(),'id','child_name');
return implode(', ',$children1);
}
],

Looping through HTML table checked checkboxes using php

i have a similar thread using javascript but i found some problem and some bugs. so i changed to php
so far this is my code
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
//get the html data beside the checked checkboxes
}
}else{
echo '<script language="javascript">';
echo 'alert("Nothing checked")';
echo '</script>';
}
and my stripped html code
<tr>
<td><?php echo $r['Name'] ?></td>
<td><?php echo $r['Age'] ?></td>
<td><?php echo $r['Address'] ?></td>
<td><input name="check[]" type="checkbox" ></td>
</tr>
i wan't to get the data on the same row where a checkbox is checked. please help me. thanks
You have to loop through the POST data, and assign $r[NAME] = VALUE
For the PHP code:
array $r;
foreach($_POST as $post=>$value) {
//get the html data beside the checked checkboxes
$r[ $post] = $value;
}
}
}else{
echo '<script language="javascript">';
echo 'alert("Nothing checked")';
echo '</script>';
}
Assuming that the HTML part your referring to is the result page
For the HTML:
<tr>
<td><?php echo $r['Name'] ?></td>
<td><?php echo $r['Age'] ?></td>
<td><?php echo $r['Address'] ?></td>
</tr>
First, lets add some data:
<?php
$data = [
'111' => [
'Name' => 'John',
'Age' => '25',
'Address' => 'Baker street, 11'
],
'222' => [
'Name' => 'Mary',
'Age' => '19',
'Address' => 'Elm street, 14'
],
'333' => [
'Name' => 'Nick',
'Age' => '23',
'Address' => '64th stree, 2'
]
];
?>
Output all data items and set checks from previous posted data:
<form method="POST">
<table>
<?php foreach($data as $id => $item): ?>
<tr>
<td><?= $item['Name'] ?></td>
<td><?= $item['Age'] ?></td>
<td><?= $item['Address'] ?></td>
<td><input name="check[]" type="checkbox" value="<?= $id; ?>"
<?php if (isset($_POST['check']) && in_array($id, $_POST['check'])): ?>
checked="checked"
<?php endif; ?>/></td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" />
</form>
<?php if (empty($_POST['check'])): ?>
<script>alert("Nothing checked");</script>
<?php endif; ?>
You'll get reloaded page with properly marked checkboxes after the form submission.

Why in my post is only one item? CakePHP 2.x

I have table in my view. This table is in form. I need send all items from table to my controller. I create form but he is sending only one item, last item...
Here is my table in View:
<?php echo $this->Form->create('Goodsandoffer');?>
<table id="GoodSandOffersTable" class="display" cellspacing="0" width="100%">
<thead><tr>
<th><?php echo $this->Paginator->sort('id','ID'); ?></th>
<th><?php echo $this->Paginator->sort('nazwa','Nazwa'); ?></th>
<th><?php echo $this->Paginator->sort('kodKreskowy','Kod EAN'); ?></th>
<th><?php echo $this->Paginator->sort('cenaSprzedazyBrutto','Cena sprzedaży brutto'); ?></th>
<th>Cena Promocyjna</th>
<th>Akcja</th>
</tr>
</thead>
<tbody>
<?php $x =0;?>
<?php foreach ($goods as $good): ?>
<tr>
<td><?php echo h($goodID= $good['Good']['id']);?></td>
<td><?php echo h($good['Good']['nazwa']);?></td>
<td><?php echo h($good['Good']['kodKreskowy']);?></td>
<td><?php echo h($good['Good']['cenaSprzedazyBrutto']);?></td>
<?php echo $this->Form->input('promotionaloffer_id',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna', array('id' => 'cenaPromocyjna'.$x));?></td>
</tr>
<?php $x = $x + 1 ;?>
<?php endforeach; ?>
</tbody>
</table>
<?php echo $this->Form->end(__('Dodaj')); ?>
Here is controller:
if ($this->request->is('post')) {
debug($this->request->data);
}
And when Im debug I got this:
\app\Controller\GoodsandoffersController.php (line 110)
array(
'Goodsandoffer' => array(
'promotionaloffer_id' => '3',
'good_id' => '20',
'cenaPromocyjna' => '3'
)
)
In array I have only last item from table. I need all. What Im doing wrong?
I'm not familiar with CakePHP myself but it looks like all your fields in your table rows have the same name (promotionaloffer_id, good_id, cenaPromocyjna):
<?php echo $this->Form->input('promotionaloffer_id',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna', array('id' => 'cenaPromocyjna'.$x));?></td>
In order to receive all values, you need to indicate them with array keys like so:
<?php echo $this->Form->input('promotionaloffer_id['.$good['Good']['id'].']',array("default"=>$prom['Promotionaloffer']['id'],'type'=>'hidden'));?>
<?php echo $this->Form->input('good_id['.$good['Good']['id'].']',array("default"=>$good['Good']['id'],'type'=>'hidden'));?>
<td><?php echo $this->Form->input('cenaPromocyjna['.$good['Good']['id'].']', array('id' => 'cenaPromocyjna'.$x));?></td>
This would result in input names like
promotionaloffer_id[0], good_id[0], cenaPromocyjna[0]
promotionaloffer_id[1], good_id[1], cenaPromocyjna[1]
...
promotionaloffer_id[x], good_id[x], cenaPromocyjna[x]
Where x is the good-id.

codeigniter undefined offset error

I'm currently learning cart system with CI and got some problems
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 2
Filename: models/main_model.php
Line Number: 241
here's the code:
Controller:
function update_cart(){
$this->main_model->validate_update_cart();
redirect('cart');
}
Model:
function validate_update_cart(){
// Get the total number of items in cart
$total = $this->cart->total_items();
// Retrieve the posted information
$item = $this->input->post('rowid');
$qty = $this->input->post('qty');
// Cycle true all items and update them
for($i=0;$i < $total;$i++)
{
// Create an array with the products rowid's and quantities.
$data = array(
'rowid' => $item[$i], //this is line 241
'qty' => $qty[$i]
);
// Update the cart with the new information
$this->cart->update($data);
}
}
view:
<div id="main">
<?php if(!$this->cart->contents()):
echo 'You don\'t have any items yet.';
else:
?>
<?php echo form_open('cart/update_cart'); ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<thead>
<tr>
<td style="background-color:yellow;">Qty</td>
<td style="background-color:yellow;">Item No/td>
<td style="background-color:yellow;">Description</td>
<td style="background-color:yellow;">Color</td>
<td style="background-color:yellow;">Price</td>
<td style="background-color:yellow;">Sub-Total</td>
<td style="background-color:yellow;">Delete</td>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach($this->cart->contents() as $items): ?>
<?php echo form_hidden('rowid[]', $items['rowid']); ?>
<tr <?php if($i&1){ echo 'class="alt"'; }?>>
<td>
<?php echo form_input(array('name' => 'qty[]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>
</td>
<td><a style="font-size:11px;"><?php echo $items['id']?></a></td>
<td><a style="font-size:11px;"><?php echo $items['name']; ?></a></td>
<td><a style="font-size:11px;"><?php echo $items['warna']?></a></td>
<td><a style="font-size:11px;">Rp. <?php echo number_format($items['price'],0,",",".");?></a></td>
<td><a style="font-size:11px;">Rp. <?php echo number_format($items['subtotal'],0,",",".");?></a></td>
<td><img src="<?= base_url();?>assets/image/hapus.png"></img></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="5"><strong>Total</strong></td>
<td colspan="2"><a align="right"><?php echo $this->cart->format_number($this->cart->total()); ?></a></td>
</tr>
</tbody>
</table>
<p><?php echo "<input type='submit' class='Button' value='Update'>";?></p>
<?php
echo form_close();
endif;
?>
edited the view, complete code.
when i click the update button it returned error live above.
thanks.
It seems that $this->cart->total_items() returns something different from the total items in the $item and $qty arrays. Later on, you are using this result to iterate these arrays, and the loop variable ($i) exceeds the arrays' boundaries.
Change your loop to:
for($i=0;$i < count($item);$i++)
You can choose count($qty) if you prefer, provided that the two arrays contain the same number of elements (which has to be true anyway, in order for the whole algorithm to work).
when you receive the post you should use
rowid[] instead of rowid

Codeigniter can't pass value to controller

i have views here
<?php echo form_open('c_kategorimaterial/tambah'); ?>
<table>
<tr>
<td>Kode Kategori Material / Jasa</td> //primary key of table
<td><?php $inkode=array('name' => 'kkmj', 'disabled' => 'disabled', 'value' => $nextval, 'class' => 'GUI'); echo form_input($inkode) ?></td>
<td class="error"> <?php echo form_error('kkmj'); ?></td>
//i make the form_input disabled, because the value is
//auto generated from the last ID of table in database
</tr>
<tr>
<td>Nama Material / Jasa</td>
<td><?php $innama=array('name' => 'nmj', 'class' => 'GUI'); echo form_input($innama) ?></td>
<td class="error"> <?php echo form_error('nmj'); ?></td>
</tr>
<tr>
<td></td>
<td><?php $intambah=array('name' =>'login','class' =>'button','value' => 'Tambah'); echo form_submit($intambah); echo nbs(); $inreset=array('name' =>'reset','class' =>'button','value' => 'Hapus'); echo form_reset($inreset); ?></td>
</tr>
<?php echo form_close(); ?>
and it pass the value to the controller
function tambah()
{
$id = $this->input->post('kkmj');
$nama = $this->input->post('nmj');
$this->form_validation->set_rules('nmj','Nama','trim|required|min_length[2]|max_length[20]|xss_clean');
if($this->form_validation->run() == false)
{
$lastval = $this->m_admin->getlastval('KKMJ','ms_kategori_material','kode_kategori_material_jasa');
$data['nextval'] = $this->m_admin->gencode('KKMJ',3,$lastval);
$data['title'] = 'QB Tambah Kategori Material';
$this->load->view('head',$data);
$this->load->view('content/add_kategori_material',$data);
}
else
{
echo 'id adalah '.$id.' namanya adalah '.$nama; //$id is not printed
}
}
and then i fill the second field with 'water' and click the button. my page showing blank white screen with a text id adalah namanya adalah water
how come the ID is not printed ? how do i resolve this ?
Disabled elements do not get posted when you submit a form. If you need a field with unchangeable value, try to set it as read-only instead of disabled.
Because the field is disabled.
Use readonly attribute.

Categories