How do I echo previously chosen dropdown value in codeigniter? - php

This code works well in terms of adding new entry with this dropdown category. My problem is, this sets Option1 as the default value to make sure the dropdown is not left unanswered. My question is, how do I echo the previously saved value for dropdown category? I should have it correctly displayed so I can do edit function correctly.
<tr>
<span style="font-size: 10pt" class="label label-info">Category</span><br/>
<? $options = array
(
'1' => 'Option1',
'2' => 'Option2',
'3' => 'Option3',
'4' => 'Option4',
); ?>
<?php echo form_dropdown('category_id', $options, 'Option1');?>
</tr>
I am looking forward to any help.
Have a nice day! :)

EDIT: this works (by the way there are good classes to handle forms with codeigniter out there, where you don't have to use all this code for each field and that manage validation rules etc. in a much more flexible way - keeping the view and controller very simple and moving most of it into models).
<?php $model = array(
array('value' => '1', 'display' => 'Option1'),
array('value' => '2', 'display' => 'Option2'),
array('value' => '3', 'display' => 'Option3'));
echo '<select name="category_id">';
foreach($model as $i) echo '<option '.($this->input->post('category_id') === $i['value'] ? 'selected="selected"' : '').' value="'.$i['value'].'">'.$i['display'].'</option>';
echo '</select>';
?>

in ref from your question : Edit form echoes previously saved data correctly but does not update the form fields
<?php
foreach($model as $row)
{
if($row="" )
{
$selected_category_id = '';
$options = array
(
'1' => 'Option1',
'2' => 'Option2',
'3' => 'Option3',
'4' => 'Option4',
); ?>
<?php echo form_dropdown('category_id', $options, $selected_category_id);?>
}
else
{
$selected_category_id = '$row->(database column name where vale is stored)';
$options = array
(
'1' => 'Option1',
'2' => 'Option2',
'3' => 'Option3',
'4' => 'Option4',
); ?>
<?php echo form_dropdown('category_id', $options, $selected_category_id);?>
}
}

Related

How to develop dependant drop down without database

I have following code in my controller:
public function getData($property)
{
$data = array(
'type'=>array(
'PC',
'Laptop'
),
'brand'=>array(
'1'=>array(
'ASUS PC',
'DELL PC',
),
'2'=>array(
'ASUS laptop',
'DELL laptop',
),
);
return $data[$property];
}
In my view:
<?= $form->dropDownList($model, 'type',$model->getData('type'), array('class' => 'form-control')) ?>
This code(in the view) returns types.(e.g shows pc and laptop options in the drop down ).
I need to develop dependant drop that will show 'ASUS PC'and 'DELL PC' in the drop down, if user chooses pc option from the the first drop down(type). How can I do it.
1) add two dropdown fields in view page.
<?php echo $form->dropDownList($model, 'type',array('PC'=>'PC','Laptop'=>'Laptops'),
array(
'empty'=>'--Select Product Type---' ,
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('yourcontroller/getData'), //url to call.
'data'=>array('type'=>'js:this.value'),// value to send
'update'=>'#dependent_value', //selector to update
))); ?>
//empty since it will be filled by the other dropdown
<div class="row">
<?php echo CHtml::dropDownList('dependent_value','', array()); ?>
</div>
Controller
public function actionGetData() {
$data = array('type' => array('PC', 'Laptop'), 'brand' => array('1' => array('ASUS PC', 'DELL PC'), '2' => array('ASUS laptop', 'DELL laptop')));
//echo "<pre>";print_r($_POST['type']);die;
if ($_POST['type'] == 'PC') {
$arr = $data['brand'][1];
} elseif ($_POST['type'] == 'Laptop') {
$arr = $data['brand'][2];
} else {
$arr = array();
}
echo CHtml::tag('option', array('value' => $value), CHtml::encode("Select Product Brand--"), true);
foreach ($arr as $value => $name) {
echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
}
}
Note : Make sure to set permission to access this action in accessRules

cakephp2 can't display data from contain

I am developing a web app using cake php 2.
I have an issue while displaying data from 2 tables...
My models :
<?php
class Discipline extends AppModel{
public $hasMany = "Student";
}
?>
<?php
class Student extends AppModel{
public $belongsTo = array(
"Discipline" => array(
"className" => "Discipline",
"foreignKey" => "discipline_id"
)
);
}
?>
Here is my studentscontroller :
<?php
class StudentsController extends AppController{
function admin_index(){
if($this->request->is('put') || $this->request->is('post')){
$student = $this->request->data['Student'];
if($this->Student->save($this->request->data)){
$this->Session->setFlash("L'eleve a bien été modifié","notif");
}
}
$d['student'] = $this->Student->find('all',array(
'contain' => "Discipline"
));
$this->set($d);
}
}
I am trying to display student's data using this view :
<?php
foreach($student as $k => $v){
$v = current($v);
echo "<td>Action</td>";
echo "<td>Label</td>";
echo "<td>".$v['nom']." ".$v['prenom']."</td>";
echo "<td>".$v['sexe']."</td>";
echo "<td>".$v['naissance']."</td>";
echo "<td>".$v['Discipline']['designation']."</td>";
echo "<td>".$v['comite']."</td>";
echo "<td>".$v['classe']."</td>";
echo "<td>".$v['elite']."</td>";
echo "<td>".$v['alerte']."</td>";
echo "<td>".$v['quota1']."</td>";
echo "<td>".$v['quota2']."</td>";
echo "<td>".$v['total']."</td>";
echo "<td>Pris</td>";
echo "<td>Restant</td>";
echo "<td>Supp</td>";
}
?>
But i have an issue on this line :
echo "<td>".$v['Discipline']['designation']."</td>";
It says this error :
notice (8): Undefined index: designation [APP\View\Students\admin_index.ctp, line 47]
I am used to develop on cakephp 3 and I am pretty embarassed with that error.. what to do to display data from Disciplines table from my student view ?
Any idea ? Thx
EDIT: I did a debug on my StudentsController, and I found the data I wanna display :
array(
'student' => array(
(int) 0 => array(
'Student' => array(
'id' => '2',
'prenom' => 'Jean',
'nom' => 'Michel',
'sexe' => '1',
'naissance' => '2015-08-02',
'age' => '12',
'classe' => '1',
'discipline_id' => '1',
'comite' => 'test',
'categorie' => 'test',
'elite' => true,
'alerte' => 'test',
'quota1' => '12',
'quota2' => '12',
'total' => '24',
'note' => 'tete'
),
'Discipline' => array(
**'id' => '1',
'designation' => 'Alpin'**
)
)
)
)
i think you should change the view
Change:
foreach($student as $k => $value){
$v = current($value);
to:
foreach($student as $k => $v){
$v = current($v);
and change:
echo "<td>".$v['Discipline']['designation']."</td>";
to
echo "<td>".$value['Discipline']['designation']."</td>";
You overwrite the variable $v with the first found array, so $v would be $v['Student']
I for myself won't use current to go inside a array for CakePHP, but use $v['Student'] everywhere

Merging two arrays into one array, when found duplicates, replace the old one with a new one

Hi I am new in PHP and I am trying to merge 2 arrays but I don't want to get duplicates.I have been stuck a week now.
I have first array:
$existed_product = array(array('name'=>"pano", 'code'=>"BR0001", 'qty'=>"2", 'price'=>"12"),
array('name'=>"ying", 'code'=>"AB001", 'qty'=>"5", 'price'=>"8"));
And I want to merge the second array:
$new_product= array('name'=>"pano", 'code'=>"BR0001", 'qty'=>"10", 'price'=>"12");
I want to merge them and when it found duplicate product, just replace it with a newer array(has qty=10). The result looks like this:
$final_array=array(array('name'=>"pano", 'code'=>"BR0001", 'qty'=>"10", 'price'=>"12"),
array('name'=>"ying", 'code'=>"AB001", 'qty'=>"5", 'price'=>"8"));
Please help me.Thank you
Assuming new product always is a single array, and code is the identifyer, something like this
$existed_product = array(
array(
'name' => 'pano',
'code' => 'BR0001',
'qty' => '2',
'price' => '12' ),
array(
'name' => 'ying',
'code' => 'AB001',
'qty' => '5',
'price' => '8'
)
);
echo '<pre>', print_r( $existed_product, true ), '</pre>';
$new_product = array(
'name' => 'pano',
'code' => 'BR0001',
'qty' => '10',
'price' => '12'
);
foreach ( $existed_product as $no => $product ) {
if ( $new_product['code'] == $product['code'] ) {
$existed_product[$no]['qty'] = $new_product['qty'];
$existed_product[$no]['price'] = $new_product['price'];
}
}
echo '<pre>', print_r( $existed_product, true ), '</pre>';

WordPress plugin activation insert multiple raws

I am trying to insert multiple raw into database on plugin activation, and I manage to insert only one raw.
I can figure out how to add multiple raw for example 10 raw.
Below is the working code that is inserting one raw only:
function my_func() {
global $wpdb;
$_IL_TABLE_SETTINGS = $wpdb->prefix . "table";
$wpdb->insert(
$_IL_TABLE_SETTINGS,
array( 'id' => '1', 'options' => 'something', 'values' => 'something' ));
}
I tried like this but is not working for the second raw:
function my_func() {
global $wpdb;
$_IL_TABLE_SETTINGS = $wpdb->prefix . "table";
$wpdb->insert(
$_IL_TABLE_SETTINGS,
array( 'id' => '1', 'options' => 'something', 'values' => 'something' ),
array( 'id' => '2', 'options' => 'something', 'values' => 'something' )
);
}
How i can add this values into your code:
$id = array('1', '2', '3');
$op = array('first', 'second', 'thaird');
$data = array('value1', 'value2', 'value3');
I will appreciate any help.
Here is the code which will work.
$id = array('1', '2', '3');
$op = array('first', 'second', 'thaird');
$data = array('value1', 'value2', 'value3');
for($i=0;$i<count($id);$i++)
{
$wpdb->insert($_IL_TABLE_SETTINGS,array("id"=>$id[$i],"option"=>$op[$i],"values"=>$data[$i]));
}
Please let me know if it didnt work for you.
for example if you want to add 10 records then you can try for loop:
for($i=0;$i<10;$i++)
{
$wpdb->insert(
$_IL_TABLE_SETTINGS,
array( 'id' => '$i', 'options' => 'something', 'values' => 'something' ),
}
of course you will need to add your dynamic data accordingly.
You can add a loop into it and put the $wpdb->insert inside that loop.
If value is stored in an array
$arr=array();//containing the values you want to insert
$arr[0]=array("id"=>idvalue,"key"=>"value");
for($i=0;$i<count($arr);$i++)
{
$wpdb->insert($_IL_TABLE_SETTINGS,$arr[$i]));
}
if you want to store imaginari values
for($i=0;$i<count($arr);$i++)
{
$wpdb->insert($_IL_TABLE_SETTINGS,array( 'id' => $i, 'options' => 'something', 'values' => 'something' ));
}
Hope you can relate your code with the above one, if no please let me know.

CakePHP disable some radio buttons?

I have a simple form with some radio buttons. I want to disable some of the radio buttons, is this possible?
$sixMonths = true;
$twelveMonths = true;
$twentyfourMonths = false;
echo $this->McForm->create('Wizard', array ('url'=>'/wizard/create'));
$options = array('24' => '24 months','12' => '12 months', '6' => '6 months');
$attributes = array('legend' =>false, 'default' => '6');
echo $this->McForm->radio('period', $options, $attributes);
echo $this->McForm->submit('Save');
echo $this->McForm->end();
So in this case I'd like to disable the first radio button and enable the other two.
I know I could probably do it with jQuery, but I'd prefer to do it without using it, is it possible?
Any ideas?
Thanks!
You can add a disabled array in $attributes that contains the values ​​of the radios:
$attributes = [
'legend' => false,
'default' => '6',
'disabled' => ['6', '24']
];
This isn't possible unless you call the radio function for each option separately and add 'disabled' => 'disabled' to the $attributes array for the ones you wish to disable. Here's a possible solution:
// Options
$options = array('24' => '24 months','12' => '12 months', '6' => '6 months');
// Disabled options
$disabled_options = array('12');
// Default attributes (these may need to be adjusted)
$attributes = array('legend' => false, 'default' => '6');
// Loop through all of the options
foreach ( $options as $key => $value )
{
// Output the radio button.
// The field name is now "period.n" which will result in "data[Model][period][n]" where "n" is the number of months.
// The options is an array contain only the current $key and $value.
// The 'disabled' => 'disabled' is added to the attributes if the key is found in the $disabled_options array.
echo $this->McForm->radio('period.' . $key, array($key => $value), ( in_array($key, $disabled_options) ? $attributes + array('disabled' => 'disabled') : $attributes ));
}

Categories