How to insert multiples arrays with multiples values in laravel by query builder - insert

lets suppose i want to insert an array which container following data;
here is an array,
Array
(
[0] => 1
[1] => ::1
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 0
[1] => 1
)
[4] => 2018-10-11
[5] => 2018-10-31
[6] => 0
[7] => 0
[8] => 2018-10-11
[9] => 10:38:36
)
but i am stuck how to insert this array with single foreach loop
and i am using this code but not working for multi arrays with different values
$count_row = 0;
foreach($products as $row){
$count_row ++;
//Set Field data according to table column
$data = array(
'user_id' => $user_id,
'ip_address' => $ip_address,
'product_id' => $row,
'page_id' => $row[$count_row],
'start_date' => $start_date,
'end_date' => $end_date,
'type' => $type,
'status' => $status,
'created_date' => $created_date,
'created_time' => $created_time,
);
//Query For Inserting Data
$query = DB::table('tbl_product_advertisements')
->insertGetId($data);
}

i found a solution, for inserting this type of array we have to use 2 loops,

Related

CakePHP 3 validation Range not working - i get no error message

I am trying to validate my Entity but it wont work. Only the isUnique works. But noEmpty, minLength and range are ignored. I would like to get these messages displayed too. What am I doing wrong? Or does the theruleschecker override the validation?
CitiesTable.php
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class CitiesTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('cities');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Fields', [
'foreignKey' => 'city_id'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('name', 'create')
->notEmpty('name')
->add('name', [
'length' => [
'rule' => ['minLength', 15],
'message' => 'Titles need to be at least 10 characters long',
]
])
->lengthBetween('username', [15, 20])
->minLength('name',15,'TestMessage');
$validator
->integer('fields_per_pieces_x')
->range('fields_per_pieces_x',[1,5],'TeestMessage')
->requirePresence('fields_per_pieces_x', 'create')
->notEmpty('fields_per_pieces_x');
$validator
->integer('fields_per_pieces_y')
->requirePresence('fields_per_pieces_y', 'create')
->notEmpty('fields_per_pieces_y');
$validator
->integer('field_pieces')
->requirePresence('field_pieces', 'create')
->notEmpty('field_pieces');
return $validator;
}
public function buildRules(RulesChecker $rules){
$rules->add($rules->isUnique(['name']));
return $rules;
}
}
and this:
$map['city'] = $this->generateCity('Kantosddd', -2, -2, -4);
pr($map);
die();
shows the following:
Array
(
[city] => App\Model\Entity\City Object
(
[name] => Kantosddd
[fields_per_pieces_x] => -2
[fields_per_pieces_y] => -2
[field_pieces] => -4
[[new]] => 1
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
[name] => 1
[fields_per_pieces_x] => 1
[fields_per_pieces_y] => 1
[field_pieces] => 1
)
[[original]] => Array
(
)
[[virtual]] => Array
(
[get] => Array
(
[0] => fields_per_pieces_all
[1] => fields_in_x
[2] => fields_in_y
[3] => count_of_all_fields
)
)
[[errors]] => Array
(
[name] => Array
(
[_isUnique] => This value is already in use
)
)
[[invalid]] => Array
(
[name] => Kantosddd
)
[[repository]] => Cities
)
)
generateCity Function
function generateCity(string $name, int $fieldsPerPiecesX, int $fieldsPerPiecesY, int $fieldPieces ): City
{
$citiesTable = TableRegistry::get('Cities');
$city = $citiesTable->newEntity();
$city->set('name', $name);
$city->set('fields_per_pieces_x', $fieldsPerPiecesX);
$city->set('fields_per_pieces_y', $fieldsPerPiecesY);
$city->set('field_pieces', $fieldPieces);
if(!$citiesTable->save($city)){
foreach($city->errors() as $fields => $field){
foreach($field as $rule => $message){
$this->Flash->error($fields.": ".$message);
}
}
}
return $city;
}
Thank you for the help :)
When you use the set function, validation is not done, only rules. Try this:
$city = $citiesTable->newEntity([
'name' => $name,
'fields_per_pieces_x' => $fieldsPerPiecesX,
'fields_per_pieces_y' => $fieldsPerPiecesY,
'field_pieces' => $fieldPieces
]);
Or, equivalently,
$city = $citiesTable->newEntity();
$city = $citiesTable->patchEntity($city, [
'name' => $name,
'fields_per_pieces_x' => $fieldsPerPiecesX,
'fields_per_pieces_y' => $fieldsPerPiecesY,
'field_pieces' => $fieldPieces
]);

how to access the array inside the array in codeigniter?

I am new to CI . I had a function to access a list of data as category. The function was like this:
public function get_category_tree()
{
$this->ci->db->where('is_display','1');
$query = $this->ci->db->get('product_categories');
if ($query->num_rows() > 0)
{
foreach($query->result() as $cat)
{
if($cat->parent_id=='0'){
//category
$categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => '');
}else{
//subcategory;
$categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
}
}
return $categories_arr;
}
return false;
}
this function was defined in general library and i accessed it from the controller like this:
$this->data['category_tree'] = $this->general->get_category_tree();
And this used to give me this result:
Array
(
[id] => 19
[parent_id] => 0
[name] => DVDs & Movies
[subcat] =>
)
Array
(
[id] => 32
[parent_id] => 0
[name] => Stamps
[subcat] => Array
(
[0] => Array
(
[id] => 78
[parent_id] => 32
[name] => Africa
)
[1] => Array
(
[id] => 79
[parent_id] => 32
[name] => Asia
)
[2] => Array
(
[id] => 80
[parent_id] => 32
[name] => Australia
)
[3] => Array
(
[id] => 81
[parent_id] => 32
[name] => Br Comm Other
)
[4] => Array
(
[id] => 82
[parent_id] => 32
[name] => Canada
)
[5] => Array
(
[id] => 83
[parent_id] => 32
[name] => Europe
)
[6] => Array
(
[id] => 84
[parent_id] => 32
[name] => Latin America
)
[7] => Array
(
[id] => 85
[parent_id] => 32
[name] => Middle East
)
[8] => Array
(
[id] => 86
[parent_id] => 32
[name] => Publications
)
[9] => Array
(
[id] => 87
[parent_id] => 32
[name] => Topical & Specialty
)
[10] => Array
(
[id] => 88
[parent_id] => 32
[name] => UK (Great Britain)
)
[11] => Array
(
[id] => 89
[parent_id] => 32
[name] => United States
)
[12] => Array
(
[id] => 90
[parent_id] => 32
[name] => Worldwide
)
)
)
Now i upgraded my php to 7+, now the line
$categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
did not work and gave php error and i simply removed the [] and it became
$categories_arr[$cat->parent_id]['subcat'] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
Now when i print_it it gives me this result:
Array
(
[id] => 19
[parent_id] => 0
[name] => DVDs & Movies
[subcat] =>
)
Array
(
[id] => 32
[parent_id] => 0
[name] => Stamps
[subcat] => Array
(
[id] => 90
[parent_id] => 32
[name] => Worldwide
)
)
Only the last array of the sub array. How can i solve it. I searched a lot about the upgrade and fix for it but couldn't garb anything useful. Can anyone please help. Thanks in advance
Because you define subcat as string by 'subcat' => '' in defining $categories_arr[$cat->id]. Use this:
public function get_category_tree()
{
$this->ci->db->where('is_display','1');
$query = $this->ci->db->get('product_categories');
if ($query->num_rows() > 0)
{
foreach($query->result() as $cat)
{
if($cat->parent_id=='0'){
//category
$categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => array());
}else{
//subcategory;
$categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
}
}
return $categories_arr;
}
return false;
}

How to use loops with Mustache.php?

I need to loop as foreach() with my array,
$input = array (
1 => array ( 'year' => '1534', 'name' => 'test1', ),
2 => array ( 'year' => '1644', 'day' => 'test2' )
3 => array ( 'year' => '2015', 'day' => 'test3', ),
// ...
);
$m->render( $template, $input );
but can't ref without a "rooot key"... This was the first problem... Then I sulve using $input = array('list'=>$input); and ok, now list key exist to
{#list} test {/list}
but it not loops (!), it shows "test" once...
I think the problem is inside the $input array. You shouldn't use the numeric keys. So try changing the array from
$input = array (
1 => array ( 'year' => '1534', 'name' => 'test1', ),
2 => array ( 'year' => '1644', 'day' => 'test2' )
3 => array ( 'year' => '2015', 'day' => 'test3', ),
);
to
$input = array (
array ( 'year' => '1534', 'name' => 'test1', ),
array ( 'year' => '1644', 'day' => 'test2' )
array ( 'year' => '2015', 'day' => 'test3', ),
);
This is a my example, a little bit different from your code:
Mustache_Autoloader::register();
$oMustache = new Mustache_Engine( array(
'loader' => new Mustache_Loader_FilesystemLoader( 'templates' ),
));
$aVariables = array(
'list' => array(
array( 'value' => 'one' ),
array( 'value' => 'two' ),
array( 'value' => 'three' ),
)
);
$template = $oMustache->loadTemplate( 'my_template_name' );
return $template->render( $aVariables );
And this is the mustache template:
{{#list}}
test {{value}} <br/>
{{/list}}

Magento, populating checkboxes fields on an admin edit form

I have a form page where I am using
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
to create a list of checkboxes.
The problem is I can't figure out how to get them to populate when editing. Can anyone tell me how to do this?
I am using the checkboxes type so they display as a list instead of in separate rows in the form. If there is a way to create them as separate fields but all in one row I would love to know how.
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));
Then checkboxes with values "1" and "5" will be checked. For more details you can check lib/Varien/Data/Form/Element/Checkboxes.php
I also attached code or you can follow below link for more help
http://pastebin.com/hKMmryE9
Magento, populating checkboxes fields on an admin edit form
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));
Little improved and verified:
$fieldset->addField('payment_methods', 'checkboxes', array('label' => 'Payment Methods', 'name' => 'payment_methods[]',
'values' => array(
array('value'=>'1', 'label'=>'Cash'),
array('value'=>'2', 'label'=>'Paypal'),
array('value'=>'3', 'label'=>'Authorize.Net'),
array('value'=>'4', 'label'=>'Square'),
),
'required' => true,
'checked' => array('1','4'),
'disabled' => array('1'), ////if you want
));
Create $array like below
Array
(
[0] => Array
(
[value] => 1
[label] => Value 1
)
[1] => Array
(
[value] => 2
[label] => Value 2
)
[2] => Array
(
[value] => 3
[label] => Value 3
)
[3] => Array
(
[value] => 4
[label] => Value 4
)
[4] => Array
(
[value] => 5
[label] => Value 5
)
)
$fieldset->addField('checkboxes', 'checkboxes', array(
'label' => 'Select Value',
'name' => 'checkboxes[]',
'values' => $array,
'onclick' => "",
'onchange' => "",
'disabled' => false,
'after_element_html' => '',
'tabindex' => 1
));

insert data and array multidimensional

I have an error when I am going to insert to database.
I have this array:
When I print_r($students) its structure is this:
Array ( [0] => stdClass Object ( [lastname] => en [firstname] => estudianten [code] => U0009876 [id_estud] => 5 ) [1] => stdClass Object ( [lastname] => Euno [firstname] => estudiante| [code] => U00020814 [id_estud] => 6 ) )
In my model I have this code:
function insert_register_students($students) {
foreach ($students as $student) {
foreach ($student['dates'] as $key => $value) {
$data = array(
'field1' =>$student['id'],
'field2' => $key,
'field3' => '',
);
$this->db->insert('mytable', $data);
}
}
}
In the model how can I do reference that $students is a stdClass Object? The last code in the model works well for me if $students is an array but now has stdClass Object.
What is my error?
Thanks for your help.
Actually you have an array of stdObject. On each iteration in the foreach you handle an object. So if you want to access its properties you have to do
'field1' => $student->property,
instead of
'field1' => $student['property']
You could do:
foreach ($students as $student) {
echo $student->lastname; //and so on
}
Hope it helps

Resources