i want to change this code
$data = array(
'O' => 'Orange',
'Y' => 'Yellow',
'G' => 'Green',
'B' => 'Blue',
'I' => 'Indigo',
'V' => 'Violet',
);
with this code
$d = DB::table('sps')
->select(array('sps.namasp'))
->where('namasp','like',$term)
->get();
Here is my full code on route
Route::get('getdata', function()
{
$term = Input::get('term');
$data = array(
'SPION DEPAN' => 'Spion Depan',
'SPION TENGAH' => 'Spion Tengah',
'O' => 'Orange',
'Y' => 'Yellow',
'G' => 'Green',
'B' => 'Blue',
'I' => 'Indigo',
'V' => 'Violet',
);
$return_array = array();
foreach ($data as $k => $v) {
if (strpos($v, $term) !== FALSE) {
$return_array[] = array('value' => $v, 'id' =>$k);
}
}
return Response::json($return_array);
});
basicly i try to find code for autocomplete on my blade. and i stack here.
if you have any references for search autocomplete on laravel 5.1, give me the example or link. thanks before :)
You can get array using lists method
$d = DB::table('sps')
->select(array('sps.namasp'))
->where('namasp','like',$term)
->lists("<< value >>","<< key >>");
This query is return an array.
Note:-
<< value >> replace to your table field that make value of array.
<< key >> replace to your table filed that make key of array
Related
Im having a weird problem.
Im using laravel backpack for an admin panel. There i use select2_from_ajax to list a values according to another field in create operation. It is showing up correctly as expected & i can select one too.
But after selection when i click save & back it gives me an error
That means my column doesn't allow to update to null right.
So when i go back & check the column it has saved the correct value.
But when default value of my column was null this error will not showup & db value would be changed to null.
This is my select2_from_ajax part.
$this->crud->addField([ // Select
'label' => "Link Type",
'type' => 'select_from_array',
'name' => 'link_type', // the db column for the foreign key
'options' => [1 => 'Product',0 => 'Collection'],
'allows_null' => false,
]);
$this->crud->addField([ // Select
'label' => "Link To", // Table column heading
'type' => "select2_from_ajax",
'name' => "link_to",
'entity' => 'link',
'attribute' => "name",
'data_source' => url('admin/itemtype'),
'placeholder' => "Select a item",
'minimum_input_length' => 0,
'include_all_form_fields' => true,
'dependencies' => ['link_type'],
]);
So why is it trying to set null value after the correct value?
Any help would be appreciated. Thanks.
My admin/itemtype function:
$search_term = $request->input('q');
$form = collect($request->input('form'))->pluck('value', 'name');
if ($search_term) {
if ($form['link_type'] == 0) {
$items = Collection::where('name', 'LIKE', '%' . $search_term . '%')->paginate(10);
} else {
$items = Product::where('title', 'LIKE', '%' . $search_term . '%')->paginate(10);
}
} else {
if ($form['link_type'] == 0) {
$items = Collection::paginate(10);
} else {
$items = Product::paginate(10);
}
}
return $items;
Let's say you have following data:
$array = ['23' => 'item', '32' => 'another item', '1' => 'More items'];
Using
return response()->json($array);
Will decode data element following:
{
1 : 'More Items'
23 : 'Item'
32 : 'Another Item'
}
To prevent array resorting put it into object's variable as such:
$array = ['23' => 'item', '32' => 'another item', '1' => 'More items'];
$returnData = ['unsorted' => $array];
return response()->json($returnData);
This becomes a problem for orderBy() queries. You can alternatively, add orderBy()->values();
How can I add more than one element to my keyed array?
array_add($myArray, 'key', 'a');
array_add($myArray, 'key-2', 'b');
Is there a better way?
There is no need for any other custom function because in PHP there is a built-in function for this and it's array_merge and you can use it like this:
$myArray = array('one' => 'TheOne', 'two' => 'TheTwo');
$array = array_merge($myArray, array('three' => 'TheThree', 'four' => 'TheFour'));
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
You can also use this:
$myArray1 = array('one' => 'TheOne', 'two' => 'TheTwo');
$myArray2 = array('three' => 'TheThree', 'four' => 'TheFour');;
$array = $myArray1 + $myArray2;
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
I prefer:
$myArray['key'] = 'a';
$myArray['key-2'] = 'b';
But this is not really better, because you're not adding more than one in a single command.
And if you really need to add multiple, you can always create a helper:
function array_add_multiple($array, $items)
{
foreach($items as $key => $value)
{
$array = array_add($items, $key, $value);
}
return $array;
}
And use it as
$array = array_add_multiple($array, ['key' => 'a', 'key-2' => 'b']);
or, if you're not using PHP 5.4:
$array = array_add_multiple($array, array('key' => 'a', 'key-2' => 'b'));
My custom "on the fly" method:
function add_to_array($key_value)
{
$arr = [];
foreach ($key_value as $key => $value) {
$arr[] = [$key=>$value];
}
return $arr;
}
dd(add_to_array(["hello"=>"from this array","and"=>"one more time","what"=>"do you think?"]));
I wrote this two line in a phtml page:
$array = Mage::getStoreConfig('extcontacts/extendedcontactsGroup');
var_dump($array);
And this is the result:
array (size=8)
'excontactus_select' => string '1' (length=1)
'defaultrecipient_text' => string 'soufiane.marar1#gmail.com' (length=25)
'departements_textarea' => string 'Sales Department,sales#example.com
Support Department,support#example.com' (length=74)
'staticblock_select' => string '1' (length=1)
'contactfrm_select' => string '0' (length=1)
'emailsender_select' => string 'general' (length=7)
'sendcopytosender_select' => string '0' (length=1)
'emailtemplate_select' => string 'extcontacts_extendedcontacts_group_emailtemplate_select' (length=55)
For select it returns 1 or 0 and I don't want 1 or 0 to be returned I want the string.
staticblock_select items example = block1,block2,block3...
How can I do to make it return the selected value?
Well, you need to create your own, Store Config Dropdown class for that select element.
I recommend to read this, there is a detailed description how to do it:
Link to Custom Store Config settings
Especially check this part:
<?php
class JR_CustomConfigExample_Model_System_Config_Source_Dropdown_Values
{
public function toOptionArray()
{
return array(
array(
'value' => 'key1',
'label' => 'Value 1',
),
array(
'value' => 'key2',
'label' => 'Value 2',
),
);
}
}
Don't forget to change you system.xml also this part:
<source_model>adminhtml/system_config_source_yesno</source_model>
Without to seeing your full module files, I could help with this one. Hope it helped, to point to the right direction.
i found the solution and this is it :
public function toOptionArray()
{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT title FROM ' . $resource->getTableName('cms/block');
$results = $readConnection->fetchAll($query);
$monarray = array();
$compt = 1;
foreach ($results as $key => $value) {
foreach ($value as $key2 => $value2) {
the solution ==> $monarray[$compt-1] = array('value'=>$value2,
'label'=>Mage::helper('extendedcontacts')->__($value2));
$compt++;
}
}
return $monarray;
}
i changed this line :
$monarray[$compt-1] = array('value'=>$compt,'label'=>Mage::helper('extendedcontacts')->__($value2));
to this line :
$monarray[$compt-1] = array('value'=>$value2,'label'=>Mage::helper('extendedcontacts')->__($value2));
and thas the result it returns footer links as example :
array (size=8)
'excontactusSelect' => string '1' (length=1)
'staticblockSelect' => string 'Footer Links' (length=12)
'contactfrmSelect' => string '1' (length=1)
'defaultrecipientText' => string 'soufiane.marar1#gmail.com' (length=25)
'emailsenderSelect' => string 'general' (length=7)
'sendcopytosenderSelect' => string '0' (length=1)
'emailtemplateSelect' => string 'extcontacts_extendedcontactsGroup_emailtemplateSelect' (length=53)
'departementsTextarea' => string '' (length=0)
this is the module conf :
http://i.stack.imgur.com/tlBfa.png
I have been looking around but I have not found an answer yet. Kindly help if you know the answer.
How do you update multiple rows in CI?
In my MySQL:
I have column names:
ID, Settings Name, Settings Value ( Settings Name is Unique )
I have the ff Data:
ID = 1, Settings Name = "Hello" , Settings Value = "True"
ID = 2, Settings Name = "World", Settings Value = "Good"
and more ...
I also have a form that gets the Settings Value but I am not sure how to update it on the DB. How to update the True for the Hello being the Settings Name and update the Good for the World.
I heard about insert_batch() but is there an update_batch()?
Are you using Active record?
Yes there is an update batch: $this->db->update_batch();
$data = array(
array(
'ID' => 1 ,
'Settings Name' => 'Hello' ,
'Settings Value' => 'True'
),
array(
'ID' => '2' ,
'Settings Name' => 'World' ,
'Settings Value' => 'Good'
)
);
$this->db->update_batch('mytable', $data, 'where_key');
From the documentation:
The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.
There is indeed an update_batch() method available in CodeIgniter already.
You can use it your example like so:
$data = array(
array(
'ID' => 1,
'Settings Name' => 'Hello',
'Settings Value' => 'True'
),
array(
'ID' => 2,
'Settings Name' => 'World',
'Settings Value' => 'Good'
)
);
$this->db->update_batch('tableName', $data, 'id');
So what you have is an array of arrays, the children basically hold the data for each row in the database. The first parameter for update_batch() is the name of the database table, the second is the $data variable and the third is the column you want to use in the WHEN clause.
Here is a simple php code for perform this operations.
<?php
function basic_update($uwi='') {
$this->load->database();
$data = array(
'ID' => 1,
'Settings Name' => 'Hello',
'Settings Value' => 'True'
);
$this->db->where('ID', '1');
$this->db->update('<table name>', $data);
$data1 = array(
'ID' => 2,
'Settings Name' => 'World',
'Settings Value' => 'Good'
);
$this->db->where('ID', '2');
$this->db->update('<table name>', $data1);
}
In $this->db->where('ID', '1'); ID is your table field and 1 is the value.
In array first parameter will contain the table name, the second parameter is an associative array of values