This question already has answers here:
How to use php serialize() and unserialize()
(10 answers)
Closed 3 years ago.
I'm tasked to change the framework from CakePHP to Codeigniter. In the database there is this JSON like text in one column in order to define the values of 4 select inputs. The text looks like -
a:4:{i:58;s:1:"1";i:59;s:1:"0";i:60;s:1:"0";i:61;s:1:"0";}
Basically it says a(array size which is 4), i(being the id) and in s(i dont know about the first integer but all of its values is just 1 the second integer with the "" means it is the 0=>'None', 1=>'Yes', 2=>'NaN').
This is the previous programmer's code in CakePHP
<?php
$options_services = array(0=>'None', 1=>'Yes', 2=>'NaN');
?>
<div class="box-body">
<div class="form-group ">
<div class="row">
<div class="col-md-6">
<label> Select Service Category</label>
<div>
<?php echo $this->Form->input('catservice_id', array('options'=>$catservices,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select','empty'=>'Please Select Category')); ?>
</div>
</div>
<div class="col-md-6">
<label> Title <span class="required">*</span></label>
<div>
<?php echo $this->Form->input('title', array('div' => false, 'label' => false, 'class' => 'form-control')); ?>
</div>
</div>
</div>
</div>
<div class="form-group ">
<div class="row">
<?php if(!empty($service_list)): ?>
<?php foreach($service_list as $key=>$values): ?>
<div class="col-md-3">
<label><?php echo $values;?> </label>
<div>
<?php echo $this->Form->input('cleaning.'.$key, array('options'=>$options_services,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select')); ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<?php
echo $this->Html->link('Back', array('controller' => 'services', 'action' => 'index'), array('escape' => false, 'class' => 'btn btn-info'));
?>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
i certainly means id
i want to use the JSON like text to be able to use this
I tried json_decode in Codeigniter but it doesnt work. You guys have any idea how I can decode this? Would help me a lot. Thank you!
I got it thanks to #GetSet help.
Using unserialize
See this question : How to use php serialize() and unserialize()
Related
I am looking to make a simple Pop-Up window using TbModal (as requested by my superiors). However, I have no clue how to do this. I have never written a modal before and I am not certain if I am doing it completely wrong.
All I need to do, is to create a Pop-up window once a button is clicked. In this window I need to simply add a name, a description and something that is called carousel. However, whenever I try to make it work I get the following error:
Alias "bootstrap.widgets.TbModal" is invalid. Make sure it points to an existing directory or file.
Below is what the button in my view looks like (using a TbButtonColumn):
'buttons'=>array(
'update'=>array(
),
'delete'=>array(
),
'clone'=>array(
'id'=>'clone-dashboard-button',
'label'=>'Clone',
'title'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'option'=>array(
'rel'=>'tooltip',
'data-toggle'=>'tooltip',
'title'=>'Clone'
),
),
),
As well as the modal:
<?php $this->beginWidget('bootstrap.widgets.TbModal', array(
'id'=>'clone-form',
// 'focus'=>'#Dashboard_clone_title',
'htmlOptions'=>array(
'tabindex'=>'-1',
))); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Test</h4>
</div>
<div class="model-body">
<div class="row">
<div class ="col-md-2">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textArea($model,'title', array('rows'=>1, 'cols'=>20,'class'=>'resize-non form-control')); // Title input?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description', array('rows'=>1, 'cols'=>30,'class'=>'resize-non form-control')); //Description ?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'carousel'); ?>
<?php echo $form->checkbox($model,'carousel'); ?>
</div>
</div>
</div>
<div class="modal-footer">
<!--Buttons-->
<div class="pull-right">
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'htmlOptions'=>array(
'onClick'=>"
$(this).attr(\"disabled\", \"disabled\");"
),
)); ?>
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'button',
'label'=>'Cancel',
'htmlOptions'=>array(
'data-dismiss'=>'modal',
),
)); ?>
</div>
</div>
<?php $this->endWidget(); ?>
I would like to this, but with yii framework:
<span class="btn default btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="..."></input>
</span>
trying to do this way but doesn't work;
echo Button::widget([
'label' => 'Select Image',
'options' => ['class' => 'btn default btn-file'],
'options' => ['class' => 'fileinput-new'],
]);
I'm very newbie in yii framework and I've been spending some time trying to do this but without success. Anyhelp would be very apreciated
The make this input, cou can use either an ActiveForm (if you have a model) like this:
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'attribute')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Submit') ?>
</div>
<?php ActiveForm::end() ?>
Or the Html helper (if you don't want a model):
<?php
use yii\helpers\Html;
?>
<?php $form = Html::beginForm(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= Html::fileInput('...') ?>
<div class="form-group">
<?= Html::submitButton('Submit') ?>
</div>
<?php Html::endForm() ?>
I believe you won't have problem adapting the style after reading the docs. Both have the label method that allows you to edit the label as you want. Let me know if i wasn't clear on something.
try this :
<?= Html::a('label', ['/controller/action'], ['class'=>'btn btn-primary']) ?>
hope it will works as you want
If you want to use file field in the yii2. Create a form and place your field in the form.
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($modelobj, 'fieldname')->fileInput() ?>
<?= Html::submitButton('Submit')?>
<?php ActiveForm::end() ?>
I have dropdown list where there are five or six items (here items are services). I used here ajax request which updates the id “package_id” from the URL 'url'=>$this->createUrl('servicePackage'), once I select any service item. Till now, I have done successfully. But I want that ajax should use different URL and update different id i.e. “registrationid” if I select 4th number item, otherwise it should update “package_id”.
Is it possible?
Is it possible?
The form is:
<div class="form-group">
<?php echo $form->labelEx($model,'service_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php echo $form->dropDownList($model,'service_id',CHtml::listData(Service::model()->findAll(),'id', 'name'),
array(
'class' => 'form-control',
'prompt'=>'Select a Service',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('servicePackage'),
'update'=>'#'.CHtml::activeId($model, 'package_id'), // ajax updates package_id, but I want ajax update registration_id if I select item no 4
'data'=>array('service_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'service_id'); ?>
</div>
<div class="form-group" id="packageid">
<?php echo $form->labelEx($model,'package_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
echo $form->dropDownList($model,'package_id',$model->getServicePackage($model->service_id),array(
'class' => 'form-control','prompt'=>'Select a Package',
'ajax' =>
array('type'=>'POST',
'url'=>$this->createUrl('packagePrice'), //url to call.
'update'=>'#price', //selector to update
'data'=>array('package_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'package_id'); ?>
</div>
<div class="form-group" id="registrationid">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$registrants = Registration::model()->findAll();
$data = CHtml::listData($registrants,'id', function($registrants) {
return CHtml::encode($registrants->name.' ('.$registrants->id.')');
});
echo $form->dropDownList($model,'registration_id',$data,
array(
'class' => 'required form-control chzn-select',
'prompt'=>'Select a Registrant',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('Order'), //url to call.
'update'=>'#'.CHtml::activeId($model, 'order_id'), //selector to update
'data'=>array('registration_id'=>'js:this.value'),
)
));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>
I have been developing an application in Yii framework. At this point, I fall in an issue that is, I have an Order form where I select a registrant (all registrants come from database) from a dropdown, an item (all product items come from database) from dropdown and a input textbox where I type the quantity. There is a "ajaxSubmitButton" button to send all these values to the controller "actionCart" using Ajax. After receiving all values in the controller, I want to put all values in session variable. When I add another new item, the session values are being replaced what I want to hold all newly added items into the session variable. In this circumstance, what should I do. Please help me. I am giving my code snippets below:
in form:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'order-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal' , 'enctype'=>'multipart/form-data', ),
)); ?>
<div class="alert alert-info" xmlns="http://www.w3.org/1999/html">
<p class="note">Fields with <strong><span class="required">*</span></strong> are required.</p>
</div>
<?php echo $form->errorSummary($model); ?>
<div class="form-group">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Registration::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'registration_id',$data,array('class' => 'form-control chzn-select','prompt'=>'Select a Registrant'));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'item', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Products::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'item',$data, array('class'=>'form-control chzn-select' , 'id'=>'item', 'prompt'=>'Select an Item')); ?>
<?php
?>
</div>
<?php echo $form->error($model,'item'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'quantity', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-2">
<?php
echo $form->textField($model,'quantity',array('class' => 'form-control','size'=>60,'maxlength'=>11));
?>
</div>
<?php echo $form->error($model,'quantity'); ?>
</div>
<div class="form-group">
<div class="col-lg-8 pull-right">
<?php
echo CHtml::ajaxSubmitButton('Add to Cart',Yii::app()->createUrl('admin/order/cart'),
array(
'type'=>'POST',
'update'=>'#cartResult',
),
array('class'=>'btn btn-primary btn-sm',));
?>
</div>
</div>
<div class="form-group">
<div id="cartResult" class="col-lg-12">
</div>
</div>
<?php $this->endWidget(); ?>
</div>
In controller:
public function actionCart()
{
if(isset($_POST["Order"])){
$item = $_POST["Order"];
$registration_id = $item["registration_id"];
$productId = $item["item"];
$quantity = $item["quantity"];
$quantity = $item["quantity"]=='' ? 1 : $item["quantity"];
$productInfo = Products::model()->findByPk(array('id'=>$productId));
$totalPrice = $productInfo->price * $quantity;
$session = Yii::app()->session;
$session['cart'] = array("product_id" => "$productId" , "product_name" => "$productInfo->name", "quantity" => "$quantity","price" => "$productInfo->price");
}
}
I'm trying to generate a layout with php and jQuery which lists certain profiles and organizes them in a certain way.
Specifically, I have an array of program titles and I'd like to list all of the program titles on the page as headers with two program titles per row. Then, I have an array of names which each contain an array of program names. I'd like to list all of the users that have a certain program in their array underneath the relevant program title. Additionally, the names start off hidden and each program title has a button underneath it which hides and shows tutors. I've created this layout using Bootstrap here:
<?php
include ('objects.php');
function tutorCreator($tutorName, $boxNum, $spanClass){
include ('objects.php');
echo "<div class='" . $spanClass . " tutorBox" . $boxNum . "'>";
}
?>
<div class="row-fluid">
<span class="programHeader span5">DreamWeaver</span>
<span class="programHeader offset1 span5">GIMP</span>
</div>
<div class="row-fluid">
<span class="span2 showTutors">Show tutors</span>
<span class="span2 offset4 showTutors">Show tutors</span>
</div>
<div class="row-fluid">
<?php
tutorCreator('Kevin Marks', '1', "span4");
tutorCreator('Kevin Marks','2', "span4 offset2");
?>
</div>
<div class="row-fluid">
<?php
tutorCreator('Helen Larson', "1", "span4");
tutorCreator('Helen Larson','2', "span4 offset2");
?>
</div>
<div class="row-fluid">
<span class="programHeader span5">Google Analytics</span>
<span class="programHeader offset1 span5">HootSuite</span>
</div>
<div class="row-fluid">
<span class="span2 showTutors">Show tutors</span>
<span class="span2 offset4 showTutors">Show tutors</span>
</div>
<div class="row-fluid">
<?php
tutorCreator('Ken Gato', '3', "span4");
tutorCreator('Julien Mans', '4', "span4 offset2");
?>
</div>
<div class="row-fluid">
<?php
tutorCreator('Ommed Kosh', '3', "span4");
?>
</div>
The classes added to the tutor names are for the purpose of being shown when the appropriate "show tutors" button is listed and to help with the layout.
The objects.php file contains the arrays with the list of tutors and the list of programs. This layout works fine. However, I'm trying to change the php code so that this layout is automatically updated each time I add a program or a tutor to the arrays in objects.php. I've been trying to figure it out for a few days and I just can't get an automatically updated layout which works well. I realize this is probably too broad a question but I've hit a road block so any help would really be appreciated. You can see a fuller version of the current layout here: http://www.tutorgrams.com/tutors. Thanks for any help.
Here is the objects.php file (with some tutors and programs removed for brevity):
<?php
$programs = array();
$programs['DreamWeaver'] = array(
'name' => 'DreamWeaver');
$programs['GIMP'] = array(
'name' => 'GIMP');
$programs['Google Analytics'] = array(
'name' => 'Google Analytics');
$programs['HootSuite'] = array(
'name' => 'HootSuite');
$tutors['Helen Larson'] = array(
'name' => 'Helen Larson',
'programs' => array('DreamWeaver', 'GIMP')
);
$tutors['Kevin Marks'] = array(
'name' => 'Kevin Marks',
'programs' => array('DreamWeaver', 'GIMP')
);
$tutors['Ommed Kosh'] = array(
'name' => 'Ommed Kosh',
'programs' => array('Google Analytics')
);
$tutors['Julien Mans'] = array(
'name' => 'Julien Mans',
'programs' => array('HootSuite')
);
$tutors['Ken Gato'] = array(
'name' => 'Ken Gato',
'programs' => array('Google Analytics')
);
?>
How does this look?
<?php
$numProgs = count($programs);
$progs = array_keys($programs);
$i = 0; ?>
<?php while($i < $numProgs): ?>
<div class="row-fluid">
<span class="programHeader span5"><?php echo $progs[$i]; ?></span>
<span class="programHeader offset1 span5"><?php echo $progs[$i + 1]; ?></span>
</div>
<div class="row-fluid">
<span class="span2 showTutors">Show tutors</span>
<span class="span2 offset4 showTutors">Show tutors</span>
</div>
<div class="row-fluid">
<?php foreach($tutors as $name => $data) {
$tutProgs = $data['programs'];
if(in_array($progs[$i], $tutProgs)) {
tutorCreator($name, $i, "span4");
}
} ?>
</div>
<div class="row-fluid">
<?php
foreach($tutors as $name => $data) {
$tutProgs = $data['programs'];
if(in_array($progs[$i + 1], $tutProgs)) {
tutorCreator($name, $i + 1, "span4 offset2");
}
}
?>
</div>
<?php $i += 2; ?>
<?php endwhile; ?>
It could probably be optimised a little but it seems to do the trick for me. The only thing is I don't have your themes so It might not look right yet.