change array values after explode in yii2 - php

I have a problem regarding insertion of data after explode. In this gridview column, I throw function in value like this:
[
'attribute' => 'CONNECTOR_ACTION',
'value' => function($model){
$apps = \app\models\APPLICATION::find()
->where(['ID' => $model->ID_APPLICATION])
->one();
$options = $apps['CONNECTOR_PARAM'];
$optionsArr = explode(', ', $options);
return Html::activeDropDownList($model, 'CONNECTOR_ACTION', $optionsArr, ['class'=>'form-control', 'disabled' => true]);
},
'format' => 'raw'
],
And in HTML view like this:
<td>
<select id="requestapplication-connector_action" class="form-control" name="REQUESTAPPLICATION[CONNECTOR_ACTION]" disabled>
<option value="0">create</option>
<option value="1">addrole</option>
<option value="2">defaultrole</option>
<option value="3">removerole</option>
<option value="4" selected>disable</option>
<option value="5">enable</option>
<option value="6">setpassword</option>
</select>
</td>
If I want to change dropdown like this:
<td>
<select id="requestapplication-connector_action" class="form-control" name="REQUESTAPPLICATION[CONNECTOR_ACTION]" disabled>
<option value="create">create</option>
<option value="addrole">addrole</option>
<option value="defaultrole">defaultrole</option>
<option value="removerole">removerole</option>
<option value="disable" selected>disable</option>
<option value="enable">enable</option>
<option value="6">setpassword</option>
</select>
</td>
How do I do that?

This is because the keys of $optionsArr are 0-6 instead of the values. To combine it set the keys the same as the values.
$combined = array_combine($optionsArr, $optionsArr);
And then use $combined in Html::activeDropdownList()

Related

Selected value get from DB into dropdown select box

I have an Employee Form Edit.
My problem is, my function for the edit is working fine but I can't get the value of my employee type.
I have 4 employee type:
Staff
Supervisor
Manager
Super User
Every time I go to edit form, the employee type on drop down is always on staff (I want to display, if the employee is "Manager" then the drop box show Manager).
This is the code for editroledetails.blade.php
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff">Staff</option>
<option value="Supervisor">Supervisor</option>
<option value="Manager">Manager</option>
<option value="Super User">Super User</option>
</select>
</div>
Controller function
public function editroledetails(Request $request)
{
$user = \Auth::user();
$userphone = 0;
$reportTo = DB::select(DB::raw("select username from customer_type where customer_type = 'Supervisor' or customer_type ='Manager' "));
$select = DB::select(DB::raw("select customer_type from customer_type "));
$data = [
'editUsername' => $request->editUsername,
'editNik' => $request->editNik,
'editEmail' => $request->editEmail,
'editRegIdentities' => $request->editRegIdentities,
'editReportTo' => $request->editReportTo,
'editID' => $request->editID
];
return view('editroledetails', compact('user', 'userphone', 'data', 'reportTo', 'select'));
}
You can do something like this:
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
#if($user->customer_type == 'Staff')
<option value="Staff">Staff</option>
#endif
#if($user->customer_type == 'Supervisor')
<option value="Supervisor">Supervisor</option>
#endif
#if($user->customer_type == 'Manager')
<option value="Manager">Manager</option>
#endif
#if($user->customer_type == 'Super User')
<option value="Super User">Super User</option>
#endif
</select>
</div>
Use the selected attribute on the select options
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
<option value="Staff" #if($user->customer_type == 'Staff')selected#endIf>Staff</option>
<option value="Supervisor" #if($user->customer_type == 'Supervisor')selected#endIf>Supervisor</option>
<option value="Manager" #if($user->customer_type == 'Manager')selected#endIf>Manager</option>
<option value="Super User" #if($user->customer_type == 'Super User')selected#endIf>Super User</option>
</select>
</div>
You can use this to show the selected item in select list
<?php
$customerTypes = [
'Staff' => 'Staff',
'Supervisor' => 'Supervisor',
'Manager' => 'Manager',
'Super User' => 'Super User',
];
?>
<div class="form-group">
<label for="editCustomerType">Customer Type</label><br/>
<select name="editCustomerType">
#foreach($customerTypes as $type)
<?php
$selected = "";
#if($user->customer_type == $type) {
$selected = "selected";
}
?>
<option value="{{ $type }}" {{ $selected }} >{{ $type }}</option>
#endforeach
</select>
</div>

How to get Value from Selected Option in Laravel from 2 different table in database

I want to get the Value of it to insert it into my Games table in database. But I have to show the name of genre from the Genres table in database.
View:
<select name="genreid">
#foreach($genres as $genre)
<option value="{{ $genre->genreid }}"> {{ $genre->genre }}</option>
#endforeach
</select>
Controller:
public function insertgame(Request $request){
$this -> validate($request, array(
'gamename' => 'required|min:3',
'price' => 'required|int|min:1',
'genre' => 'required',
'releaseddate' => 'required|date',
'picture' => 'required|mimes:jpeg,jpg,png,gif'
));
$gamename = $request->input('gamename');
$genreid = $request->input('genreid');
$price = $request->input('price');
$releaseddate = Carbon::parse($request->input('releaseddate'));
$picture = $request->file('picture')->getClientOriginalName();
$data=array('gamename' => $gamename, 'genreid'=>$genreid, 'price'=>$price,'releaseddate'=>$releaseddate,'picture'=>$picture );
DB::table('games')->insert($data);
return redirect('managegame');
}
You've said it works for you if you use this HTML:
<select name="genre">
<option selected disabled>Choose one</option>
<option value="FPS">FPS</option>
<option value="Action">Sports</option>
<option value="Action">Action</option>
<option value="Racing">Racing</option>
<option value="Simulation">Simulation</option>
</select>
In this case, to use proper data format, change this:
<option value="{{ $genre->genreid }}">{{ $genre->genre }}</option>
To:
<option value="{{ $genre->genre }}">{{ $genre->genre }}</option>
Do something like this
#foreach($game->genres as $genre)
{
<option value='{{$genre->id}}' {{$game->genre ==$genre->id ? 'selected' :' '> {{$genre->name}} </option>
}
I've found the answer!
I just changed
'genre' => 'required',
to
'genreid' => 'integer|required',
LOL Thanks all

creating dropdowns in cakephp using the data returned by using mysql query

app/Controller/Actionscontroller.php
<?php
App::uses('AppController', 'Controller');
class ActionsController extends AppController {
public Function index(){
App::import('Model', 'ConnectionManager');
$con = new ConnectionManager;
$cn = $con->getDataSource('default');
$tablequery="SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='cakephp_db' AND TABLE_NAME != 'report'";
$this->set('table',$cn->query($tablequery));
}
}
?>
app/View/Actions/index.ctp
<fieldset>
<table>
<tr><th><td>OR</td></th></tr>
<tr>
<th>CHOOSE EXISTING DATA</th>
<td>
<?php echo $this->Form->create('table',array('type' => 'get'));
echo $this->Form->select('table ', $table,['empty' => 'choose one']);?>
<?php echo $this->Form->end('submit'); ?>
</td>
</tr>
</table>
</fieldset>
app/model/action.php
<?php
App::uses('AppModel', 'Model');
class Action extends AppModel {
public $validate = array(
'table'=> array(
'required' => array(
'rule'=> 'notBlank',
'message' => 'atleast select one table'
)
)
);
}
?>
I need the result in this format. a sample output i have written
<select name="field">
<option value="">(choose one)</option>
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
But i am getting the output in this format;By running this code.Help me to solve the issue.
<select name="table " id="tableTable">
<option value="">choose one</option>
</optgroup>
<option value="TABLE_NAME">Employee</option>
<optgroup label="TABLES">
<optgroup label="1">
</optgroup>
<option value="TABLE_NAME">House_Price</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="2">
</optgroup>
<option value="TABLE_NAME">insurence</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="3">
</optgroup>
<option value="TABLE_NAME">posts</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="4">
</optgroup>
<option value="TABLE_NAME">topics</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="5">
</optgroup>
<option value="TABLE_NAME">users</option>
<optgroup label="TABLES">
</optgroup>
<optgroup label="6">
</optgroup>
<option value="TABLE_NAME">visualizations</option>
<optgroup label="TABLES">
</optgroup>
</select>
If you have gone througn the doc of cakephp
https://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
If you give structure like this
$options = array(
'Group 1' => array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
),
'Group 2' => array(
'Value 3' => 'Label 3'
)
);
echo $this->Form->select('field', $options);
Output will be:
<select name="data[User][field]" id="UserField">
<optgroup label="Group 1">
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
</optgroup>
<optgroup label="Group 2">
<option value="Value 3">Label 3</option>
</optgroup>
</select>
That's what in your case is happening .
You have to change the structure of the response you are getting like this
echo $this->Form->select('field', array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2',
'Value 3' => 'Label 3'
));
Output:
<select name="data[User][field]" id="UserField">
<option value=""></option>
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
<option value="Value 3">Label 3</option>
</select>
So you have to make changes to your code as bellow
$tablequery="SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='cakephp_db' AND
TABLE_NAME != 'report'";
$rows = $cn->query($tablequery)->fetchAll('assoc');
$dataToSend = [];
foreach ($rows as $row) {
$dataToSend[$row['TABLE_NAME']] = $row['TABLE_NAME'];
}
$this->set('table',$dataToSend);

Array of multiple select inputs

I have this form :
<form>
<select name="data[][]" multiple="multiple">
<option>1</option>
<option selected="selected">2</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">3</option>
<option>4</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">5</option>
<option selected="selected">6</option>
</select>
</form>
I would like to get this :
$_POST['data'] = array(
0 => array(2),
1 => array(3),
2 => array(5,6)
);
I instead get this :
$_POST['data'] = array(
0 => array(2),
1 => array(3),
2 => array(5),
3 => array(6)
);
The solution would be to set the index : name="data[0][]" but i want it to be automatically done ..
Any ideas?
One way.Change to :
<select name="data[0][]" multiple="multiple">
...
<select name="data[1][]" multiple="multiple">
...
<select name="data[2][]" multiple="multiple">
Also see #CBroe comment.
You don't need to insert the keys manually.
I tested your code and it seems to be correct for what you intend to get or you expect.
I tested it like this;
<html>
<body>
<?php
if (!isset($_GET["submit"])) {
?>
<form method="get" action="yourfilename.php">
<select name="data[][]" multiple="multiple">
<option>1</option>
<option selected="selected">2</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">3</option>
<option>4</option>
</select>
<select name="data[][]" multiple="multiple">
<option selected="selected">5</option>
<option selected="selected">6</option>
</select>
<input type="submit" value="submit" name="submit"/>
</form>
<?php
}
else {
$data= $_GET["data"];
print_r($data);
echo $show;
}
\\ So to get the 5 or 6 value in the array with key "2";
\\ Output: 5
$show= $data[2][0];
\\ Or
\\ Output: 6
$show= $data[2][1];
?>
</body>
</html>

How to set IDs for select elements of Quickform_date?

Is there some way to add ID attribute to every select element generated by Quickform_date so it would look for example like this?
<select id="date-d" name="date[d]">
<option value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select><select id="date-M" name="date[M]">
<option value="1">Jan</option>
...
<option value="12" selected="selected">Dec</option>
</select><select id="date-Y" name="date[Y]">
<option value="2001">2001</option>
...
<option value="2011">2011</option>
</select>
I need those IDs for changing selected options by JavaScript. Any alternative ideas how to achieve that?
HTML_QuickForm_group::getElements() method turn out to be the key, in case anyone's interested :-)
$elements['date'] = $form->addElement('date', 'date','Datum:',
array('language' => 'cs',
'minYear' => 2005,
'maxYear' => date('Y') + 2));
foreach ($elements['date']->getElements() as $key => $element) {
$element->updateAttributes(array('id'=>'dateSelect'.$key));
}

Categories