Yii: How to get list of checkboxes? - php

I have a table called Table, it has id and name as attributes.
For each entry in Table, I would like to generate a checkbox.
How can I do this?
I am using the Yii-Boostrap plugin, which I'm expecting I would need use something like this:
foreach(...)
echo $form->checkBoxRow($model, 'name');
Which I got from the Yii-Bootstrap Documentation.

Try this simple one
And in this for precheck to work just pass the array as second parameter
as shown below
<?$select=array('2','3');?>
<?php echo CHtml::checkBoxList(
'TableValues',
'$select',//you can pass the array here which you want to be pre checked
CHtml::listData(Table::model()->findAll(),'id','name'),
array('checkAll'=>'Select all tasks', 'checkAllLast'=>true)
); ?>
And you can get the selected checkbox values in the controller using
print_r($_POST['TableValues']);
UPDATED
For this the precheck to work u have to assign the array to the model attribute as shown below
<?php $model->modelAttributename=array('3','5')//respective checked values as of yours
<?php echo $form->checkBoxList(
$model,
'modelAttributename',
CHtml::listData(Table::model()->findAll(),'id','name'),
array('checkAll'=>'Select all tasks', 'checkAllLast'=>true)
); ?>

You should see your result array form sql query and see how to access any string you want from result array and then you create array of string that contain list of name.
e.g. your result query is $result["name"] = array("a","b","c");
<?php /** #var BootActiveForm $form */
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'horizontalForm',
'type'=>'horizontal',
));
?>
<fieldset>
<legend>Legend</legend>
<?php
$result["name"] = array("a","b","c");
echo $form->checkBoxListRow($model, 'checkboxes', $result["name"]);
?>
</fieldset>

Check this example:
Book Model:
'authors' => array(self::MANY_MANY, 'Author', 'authorbook(book_id,author_id)'),
Author Model:
'books' => array(self::MANY_MANY, 'Book', 'authorbook(author_id, book_id)'),
Checkbox List in form:
$books = CHtml::listData(Book::model()->findAll(), 'id', 'name');
$selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);

Related

Codeigniter - input data save in multiple arrays and db table

I've an existing form which is passing the input data to the model in an array format. $postdata has all the data from the view and sending to model.
Controller:
$inquiry_id = $this->input->post('inquiry_id');
$postdata = $this->input->post();
$this->load->model('Design_model');
$this->Design_model->insertdata($postdata,$inquiry_id);
Model:
function insertdata($data = array(), $inquiry_id){
$sql = $this->db->query("select * from design where inquiry_id='".$inquiry_id."'");
if($sql->num_rows() == 0){
$sql_query = $this->db->insert('design', $data);
}
else{
$this->db->where('inquiry_id', $inquiry_id);
$this->db->update('design', $data);
}
}
Above is working fine. Now, I'd like to add few fields in the view and save in a different database table. Need to exclude the new field values from $postdata array getting saved. Need to find the best approach to do this. I can start with some name for all the new fields, so that we can add any filter if available to exclude from the $postdata.
You can use elements() function from Array helper.
$array = array(
'id' => 101,
'title' => 'example',
'desc' => 'something',
'unwanted' => 'bla bla'
);
$filtered_array = elements(array('id','title','desc'),$array); //you can use this directly to the post data
$this->Design_model->insertdata($filtered_array,$inquiry_id);
You can use array_merge() or array_push() functions to add new fields to the array.
Let's say you have following data
$postdata = array("name"=>"xyz",
"email"=>"xyz#gmail.com",
"age"=>"40",
"gender"=>"Male",
"occupation"=>"Engineer"
);
Of which first 3 records are from old fields and last 2 are from new fields as you saying.
You need to find last index of first set i.e. '3' Now you can do this.
$firstDb = array_splice($postdata,0,3); //here 3 is index we are using to get first 3 records from $postdata
$secondDb = array_slice($postdata,0,3); //here 3 is index we are using to get records from position 3 from $postdata
Output:
$firstDb = array("name"=>"xyz","email"=>"xyz#gmail.com","age"=>"40");
$secondDb = array("gender"=>"Male","occupation"=>"Engineer");
Now you can insert you records as you wish to. Happy coding

How to use the key of an array to set initial value of dropDownList on Yii1.1

Firstly, I custom the array arr, then using the key apple to set the initial value, but it doesn't work.
<?php
$arr = array('apple'=>'red','banana'=>yellow,'watermelon'=>'green');
echo $form->dropDownList(
$model,
'color',
$arr,
array(
'options'=>array('apple'=>array('selected'=>true))
));
?>
Set attrubute before dropDownList
$model->color='apple';
Or set it in your form (extends CFormModel), e.g.
public $color = 'apple';

How to get the selected value of Yii dropDownList in the update function?

I have a problem in updating my model values, one of the model attributes is a number that is selected from a dropDownList, here is my code:
<?php $images = Homepage::model()->findAll();
if(!empty($images)){
$data = array();
$x = 1;
foreach ($images as $i){
array_push($data, $x);
$x++;
}
?>
<?php echo $form->labelEx($model,'order'); ?>
<?php echo $form->dropDownList($model, 'order', $data, array(
'empty'=>'Select image order',
'id'=>'order')); ?>
this dropDownList is containing the number of records of model (table) in the db, the problem is in the update function, for example: on the creation of (image 1) I selected 1 as the order value, then when I go to the update, I got the pre-selected option is 2 (which is the last value of the listData) instead of 1, so what is the error here ?
Im not a Yii expert but i think you are doing it wrong, Im using this code below and it works fine im getting the id from the database and showing it in the dropDown list, try it maybe it helps or check http://www.yiiframework.com/wiki/48/by-example-chtml/#hh5 ,
<?php echo $form->labelEx($model,'order_id'); ?>
<?php echo $form->dropDownList($model,'order_id',
CHtml::listData(Homepage::model()->findAll(),'order_id','order_id'),
array('empty'=>'--Select order --') ); ?>
You need to define the selected in the options
'options'=>array($model->id => array('selected'=>true))
Roughly your code will look like this now
echo $form->dropDownList($model, 'order', $data, array(
'empty'=>'Select image order',
'id'=>'order',
'options'=>array($model->id =>array('selected'=>true))
));
Give it a try this will help.
I think it might be with the $data variable.
In my experience with Yii, i always use associative array ($key=>$value) for dropdown
so maybe something like
$data = array('1'=>'Test', '2'=>'Test 2');
In this way, Test and Test 2 will be the values displayed on the dropdown itself and '1' and '2' will be the values stored in the $model and the database.

I want to display the last 4 pages in Yii?

Studying Programming Yii, I want to display the last 4 pages:
SiteController.php
public function actionStart()
{
$featured = Page::model()->findAllByAttributes(
array(),
$condition = 'featured = :featureId',
$params = array(
':featureId' => 1,
)
);
$this->render('/layouts/start/start', array('featured'=>$featured));
}
/layouts/start/start.php
<?php print_r($this->featured); ?>
The latter file does not display anything, and should be an array with the data, how do I get it?
$this->render('/layouts/start/start', array('featured'=>$featured));
Here, you are sending array(association array) of values to the View. You can access these values by calling the array Key.
So, your code should be
<?php echo print_r($featured); ?>
Another example.
$this->render('myView', array('myName'=>'Hearaman','myAge'=>25));
I'm sending my name and age to View. To show my name and age, i should call the keys
echo $myName;
echo $myAge;
Eliminate the $this for featured.
<?php echo print_r($featured, true); ?>

How to access array fields in codeigniter?

I'm trying to pass two arrays ($a_1 and $a_2) from my controller to my view like so:
$this->load->view('confirm_data_v', $a_1, $a_2);
In my view I want to print the value of one of them doing this:
<p><?php echo $name ?></p>
<p><?php echo $mail ?></p>
when I print each array I get this:
Array
(
[name] => jon
)
Array
(
[mail] => blabla#server.com
)
$name is a field inside $a_1 and $mail is a field inside $a_2, but it seems like the view doesn't know where these fields are, I mean, it doesn't know in wich array is $name and $mail, wether $a_1 or $a_2. How do I do that?.
the codeigniter wiki sais this
$data = array(
'name' => $a_1['name'],
'mail' => $a_2['mail'],
);
$this->load->view('confirm_data_v', $data);
https://www.codeigniter.com/user_guide/general/views.html
You're passing the arrays in an incorrect way. You can only pass one data array as a second parameter while loading the view.
You could instead put each array in the data array in your controller:
$data['a_1'] = $a_1;
$data['a_2'] = $a_2;
$this->load->view('confirm_data_v', $data);
Then in your view you can access $a_1 and $a_2 as you like
Name: <?php echo $a_1['name']; ?>
Email: <?php echo $a_2['mail']; ?>

Categories