Yii2 display serialized data in gridview - php

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

Related

How to delete specific session record by clicking on href?

I unable to delete specific record from session array. I want to delete specific row in table when I click on delete link.
<?php
session_start();
if(isset($_GET["product"]) && isset($_GET["category"])){
$nomProduct = trim($_GET["product"]);
$category = trim($_GET["category"]);
$_SESSION['product'][] = array(
"nomProduct" => $nomProduct ,
"category" => $category
);
//session_destroy();
}
?>
html table
<table class="table">
<?php foreach($_SESSION["product"] as $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
`
$key=array_search($_GET['product'],$_SESSION['product']);
if($key!==false)
unset($_SESSION['product'][$key]);
$_SESSION["product"] = array_values($_SESSION["product"]);
`
Maybe this might help!
You need to find the key as this is an array.
EDIT:
Made an example for you, here when you click the link, it deletes the first name from the session array.
<?php
session_start();
$_SESSION["user"] = ["fname"=>"William","lname"=>"Henry" ];
if(isset($_GET["delete"]))
{
if($_GET["key"])
{
$key=$_GET["key"];
unset($_SESSION['user'][$key]);
}
}
?>
HTML on the same page
<h1>
<?php
if(isset($_SESSION["user"]["fname"]))echo $_SESSION["user"]["fname"]." ";
if(isset($_SESSION["user"]["lname"]))echo $_SESSION["user"]["lname"];
?>
</h1>
Delete First Name
If you want to delete the lastname (lname), change the key=lname in the href of the link, hope this example helps in your case
Modify your HTML
<table class="table">
<?php foreach($_SESSION["product"] as $key => $items) { ?>
<tr>
<th width="250px"><?php echo $items['nomProduct']; ?></th>
<td><?php echo $items['category']; ?></td>
<td style="text-align: right">Delete<td>
</tr>
<?php }?>
</table>
Catch the array key and remove it from session array.
$key = filter_input(INPUT_GET, 'key');
unset($_SESSION['product'][$key]);

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.

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.

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.

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