grocery crud multiselect field - multi-select

No problem to use this function 'multiselect field' which show on this website:
http://www.grocerycrud.com/documentation/options_functions/field_type
$crud->field_type('fruits','multiselect',
array( "1" => "banana", "2" => "orange", "3" => "apple"));
Next step, i try to extract data from database to replace the 'array' in the formulae above but failed, pls advise.
$this->db->select('employeeNumber');
$a = $this->db->get('employees')->result();
$crud->field_type('firstName', 'multiselect', $a);
I'm getting result like
Array ( [0] => stdClass Object ( [employeeNumber] => 1002 )
[1] => stdClass Object ( [employeeNumber] => 1056 )
Hmm... how to make it into this format, any advise?:
array( "1" => "banana", "2" => "orange", "3" => "apple")

You need to actually do a foreach here. In our case you need to do something like this:
$this->db->select('employeeNumber');
$results = $this->db->get('employees')->result();
$employees_multiselect = array();
foreach ($results as $result) {
$employees_multiselect[$result->employeeNumber] = $result->employeeNumber;
}
$crud->field_type('firstName', 'multiselect', $employees_multiselect);
or it is even better if you have the name of the employer to do something like this:
$this->db->select('employeeNumber, employeeName');
$results = $this->db->get('employees')->result();
$employees_multiselect = array();
foreach ($results as $result) {
$employees_multiselect[$result->employeeNumber] = $result->employeeName;
}
$crud->field_type('firstName', 'multiselect', $employees_multiselect);

Related

Laravel - How to insert multi-dimensional Array in DB

I have the array as below;
I'd like to insert each name keys into tableName and get the inserted id.
For the steps, each of them will be inserted into another table tableSteps including the last inserted id of the name.
Like as below screenshot.
In my controller,
Here's what I've done so far.
$instructionsArrays = $request->instructions;
$max = count($instructionsArrays);
for ($x = 1; $x <= $max; $x++) {
foreach($instructionsArrays as $instructionsArray){
Instruction::updateOrCreate(
['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $x],
['name' => $instructionsArray['name']],
);
}
}
I was able to save sequence numbers but for names it saves only the last name key.
And... I'm really lost..
You can achieve what you want from 2 for loops
foreach($request->instructions as $key => $val){
$id = Instruction::insertGetId(
['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $key + 1],
['name' => $val['name']],
);
$data = []; //bulk insertion
$created_at = now();
foreach($val["steps"] as $step){
array_push($data, ["header_id" => $id, "name" => $step, "sequence" => $key+1, "created_at" => $created_at]); //why insert sequence when you can obtain it from the relationship?
}
Steps::insert($data);
}
With the help of the answer of #Kneegrows, I came up with the code below and it is now working. Thank you.
foreach ($request->instructions as $key => $val) {
$instruction = Instruction::updateOrCreate(
['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $key + 1],
['instructions_name' => $val['name']],
);
$id = $instruction->id;
$data = []; //bulk insertion
$i = 1;
foreach ($val["steps"] as $step) {
if(!is_null($step)){
array_push($data, ["instruction_id" => $id, "steps_name" => $step, "sequence" => $i]);
$i++;
}
}
Steps::insert($data);
}

How can I turn an object into an array (Symfony)?

This is how I create my array fields:
public function index($slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$fields = (array) $page;
return $this->render('mypage.html.twig', ['page' => $page, 'fields' => $fields]);
}
The output is:
array:3 [▼
"\x00App\Entity\Pages\x00id" => 3
"\x00App\Entity\Pages\x00name" => "cat"
"\x00App\Entity\Pages\x00color" => ""
]
But I actually need this result:
array:3 [▼
"id" => 3
"name" => "cat"
"color" => ""
]
According to the suggestions I made this change:
public function index($slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$page = $this->getDoctrine()->getManager()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$fields = get_object_vars($page);
return $this->render('mypage.html.twig', ['page' => $page, 'fields' => $fields]);
}
But this outputs me an empty array.
You have two options:
1. Use Query::HYDRATE_ARRAY instead of findOneBy()
$query = $this->getDoctrine()
->getRepository(Pages:class)
->createQueryBuilder('p')
->getQuery();
$result = $query->getResult(Query::HYDRATE_ARRAY);
(stolen from this answer)
2. Use a Serializer
Use the Serializer Component or JMSSerializerBundle to serialize your entity object.

I want to display the key value from a associative array? I have created an array using csv file?

[0] => Array
(
[0] => name
[1] => email
[2] => contact
[3] => address
)
[1] => Array
(
[0] => santosh
[1] => santoshm9916#gmail.com
[2] => 9035619733
[3] => hennur
)
i want to access the [1] index(Email ID) from this array. please help......
Thank YOU
If you are doing javascript it could be done like this if you know what key to call for your data.
var arr = ([
["name","e-mail","contact","address"],
["santosh","santoshm9916#gmail.com","9035619733","hennur"],
["santosh2","santoshm991622222#gmail.com","90356197332","hennur2"],
["santosh3","santoshm9916#gmail.com","90356197333","hennur3"]
]);
console.log(arr[1][1]);
Your data :
$data = [[
'name',
'email',
'contact',
'address'
],
[
'santosh',
'santoshm9916#gmail.com',
'9035619733',
'hennur'
]
];
PHP Code:
$title = $data[0];
unset($data[0]);
$table = '<table border="1">';
$table .='<tr>';
foreach($title as $k=>$val){
$table.='<td>'.$val.'</td>';
};
$table.='<tr/>';
foreach($data as $key=>$val){
$table.='<tr>';
foreach($title as $k=>$v){
$table.='<td>'.$val[$k].'</td>';
}
$table.='</tr>';
}
$table.='</table>';
echo $table;
Output :
name email contact address
santosh santoshm9916#gmail.com 9035619733 hennur

PHP Display Array with Array_Slice

$array = array(
'name' => 'john',
'age' => '25',
'birthday' => '02-03-1988',
'gender' => 'male',
'telephone' => '98676878',
'location' => 'Australia'
);
$array_slice = array_slice($array, 0, 3);
foreach($array_slice as $key => $val) {
if($key !== 'age') {
echo $key.' => '.$val.'<br>';
}
}
Output:
name => john
birthday => 02-03-1988
How to display 3 values of array?
So, I want output to be like this:
name => john
birthday => 02-03-1988
gender => male
I am very beginner in programmer, thanks for help.
Update: new solution after exchanging some comments below
You must filter the array and get the result in a new array. Then you display the content of the second array.
To filter your original array, use either array_intersect_key or array_filter functions.

Extracting values from arrays in custom fields

I'm trying to come up with a single array of all values in specific custom fields. The values themselves are also arrays. I've tried all sorts of array functions but haven't come across the right one or the right combination. Here is my code thus far:
$args = array(
'post_type' => 'match_report',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'report_home-scorers'
),
array(
'key' => 'report_away-scorers'
)
)
);
$reportscore = new WP_Query($args);
$scorersResults = array();
if ( $reportscore->have_posts() ) {
while ( $reportscore->have_posts() ) {
$reportscore->the_post();
$homescorers = get_post_meta($post->ID,'report_home-scorers',false);
$awayscorers = get_post_meta($post->ID,'report_away-scorers',false);
foreach ($homescorers as $homescorer){
array_push($scorersResults, $homescorer);
}
foreach ($awayscorers as $awayscorer){
array_push($scorersResults, $awayscorer);
}
?>
<?php } wp_reset_postdata(); //endif
}//endwhile
$scorerResults = remove_empty($scorersResults);
function remove_empty($array) {
return array_filter($array, '_remove_empty_internal');
}
function _remove_empty_internal($value) {
return !empty($value) || $value === 0;
}
Here what I get if I print_r($scorerResults); :
Array
(
[1] => Array
(
[0] => 1
[1] => 63
)
[2] => Array
(
[0] => 263
[1] => 195
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
)
I just want the values in the internal arrays in an array.
Assuming you want the $scoreResults array to end up as array(1,63,263,195) you could use the array_reduce function like this:
function gatherScores($lhs, $rhs) {
foreach ($rhs as $key => $value)
if ($value)
$lhs[] = $value;
return $lhs;
}
$scorerResults = array_reduce($scorerResults, "gatherScores", array());
I'm not sure what the blank values are in your third and fourth arrays and how they should be handled, so you may need to change the if ($value) condition to check for something different. As it stands it'll obviously also filter out zero scores.

Resources