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

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.

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);
}
],

Cakephp only showing one record out of four records in the database

I have looped through my record, when i use print_r(), i will see all my records, but if i try to display it in a table, it will only show one record.
Please how do i solve this?
<?php foreach ($clists as $clist) ;?>
<tr>
<td><?php echo $clist['Course']['id']; ?></td>
<td><?php echo $this->Html->link($clist['Course']['course_name'], array('controller' => 'this', 'action' => 'this')); ?></td>
<td><?php echo $clist['Course']['course_desc']; ?></td>
<td><?php echo $clist['Course']['closing_day']; ?></td>
</tr> <?php //endforeach; ?> <?php unset($clist); ?> –
}
How do you loop through your array?
Here is a great example on how to echo the results in an array:
<?php
//Example array
$colors = array(
"red",
"green",
"blue",
"yellow"
);
foreach ($colors as $value) {
echo "$value <br>";
}
?>
More info here and here
Also, for example;
$result = array(
[0] => 'DB result 1',
[1] => 'DB result 2',
[2] => 'DB result 3'
);
To show these results in a table for example in this case would be:
<table>
<?php foreach($result as $key => $value) { ?>
<tr>
<td><?= $value ?></td>
</tr>
<?php } // End foreach ?>
</table>
Good luck!

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

CakePHP only selecting last checkbox in a loop

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.

Update multiple users at once?

On my site, users can create teams (clans) and invite other users to join them.
I’m working on a page at the moment where team leaders can edit the “ranks” of each user in the team. These options are Founder, Captain, Member, Inactive and Kick (which will remove the user from the team altogether).
I’ve included a quick screenshot of how the page looks to help explain what I’m hoping to achieve.
http://cl.ly/E9Gb
So team leaders can go through and change the rank of each user and then hit update.
What would be the best way to go about this?
Here are some extracts from my code at present:
CONTROLLER:
if ($this->input->post('update_roster'))
{
$member_id = $this->input->post('user_id');
$member_rank = $this->input->post('member_rank');
$this->clan_model->update_roster($clan_id, $member_id, $member_rank);
}
MODEL:
public function update_roster($clan_id, $user_id, $rank)
{
$data = array(
'rank' => $rank
);
$this->db->where('clan_id', $clan_id);
$this->db->where('user_id', $user_id);
if ($this->db->update('clan_joined', $data))
{
return TRUE;
}
}
VIEW:
<h3>Roster</h3>
<?php
$member_rank = array(
'1' => 'Founder',
'2' => 'Captain',
'3' => 'Member',
'4' => 'Inactive',
'5' => 'Kick'
);
?>
<?php $attributes = array('id' => 'update_roster', 'class' => 'nice'); ?>
<?php echo form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<table width="100%">
<thead>
<tr>
<th class="text-center">#</th>
<th>Username</th>
<th>Rank</th>
</tr>
</thead>
<?php form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<?php foreach ($members AS $member) : ?>
<tr>
<td class="text-center"><?php echo avatar(32, $member->username, $member->avatar_uploaded); ?></td>
<td><?php echo $info->clan_tag; ?> / <?php echo $member->username; ?></td>
<td><?php echo form_dropdown('member_rank', $member_rank, $member->rank); ?></td>
</tr>
<?php echo form_hidden('user_id', $member->username); ?>
<?php endforeach; ?>
</table>
<?php echo form_submit('update_roster', 'Update'); ?>
<?php echo form_close(); ?>
I understand this code is wrong, as it's not working, but thought I would share what I have done so far.
On your HTML form change the dropdown and your hidden input to be arrays:
<td><?php echo form_dropdown('member_rank[]', $member_rank, $member->rank); ?></td>
and
<?php echo form_hidden('user_id[]', $member->username); ?>
Then $this->input->post('member_rank') and 'user_id' will each receive an array. You can loop through the arrays in PHP and call the UPDATE code for each iteration of the loop:
for ($i = 0; $i < count($member_id); $i++) {
$current_member_id = $member_id[$i];
$current_member_rank = $member_rank[$i];
// Call your update code.
}

Categories