How do I make these fields autopopulate from the database? - php

I have an array, which holds values like this:
$items_pool = Array (
[0] => Array ( [id] => 1 [quantity] => 1 )
[1] => Array ( [id] => 2 [quantity] => 1 )
[2] => Array ( [id] => 72 [quantity] => 6 )
[3] => Array ( [id] => 4 [quantity] => 1 )
[4] => Array ( [id] => 5 [quantity] => 1 )
[5] => Array ( [id] => 7 [quantity] => 1 )
[6] => Array ( [id] => 8 [quantity] => 1 )
[7] => Array ( [id] => 9 [quantity] => 1 )
[8] => Array ( [id] => 19 [quantity] => 1 )
[9] => Array ( [id] => 20 [quantity] => 1 )
[10] => Array ( [id] => 22 [quantity] => 1 )
[11] => Array ( [id] => 29 [quantity] => 0 )
)
Next, I have a form that I am trying to populate. It loops through the item database, prints out all the possible items, and checks the ones that are already present in $items_pool.
<?php foreach ($items['items_poolpackage']->result() as $item): ?>
<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>">
<?php endforeach; ?>
I know what logically I'm trying to accomplish here, but I can't figure out the programming.
What I'm looking for, written loosely is something like this (not real code):
<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>" <?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>>
Specifically this conditional loop through the array, through all the key values (the ID) and if there's a match, the checkbox is selected.
<?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>
I understand from a loop structured like this that it may mean a lot of 'extra' processing.
TL;DR - I need to loop within the array, check for the key 'id', then print a string.

If I understand correctly, you need something like this?
array_walk($items_pool, create_function('$array', 'global $item; if( in_array($item->id, $array) ) { echo "checked=\"checked\""; }'));
Conside this though, if you stored your products in a single dimensional array (presuming the ids will always be unique
$items_pool = array(id, quantity)
You will not end up with having duplicated entries of products and can incremement/decrement the quantity easier.
$items_pool[id]++; /* or */ $items_pool[id] = $items_pool[id] + 2;
Both techniques should work if the id exists in the array or not.
If you are hoping to store other attritubes in this array other than quantity, you can
$items_pool = array(id=>array(quantity=>3, colour=>"red"));
$items_pool[id][quantity]--;

The proper attribute is checked="checked"...
if (isset($item_pool[$item->id]) && $item_pool[$item->id] > 0) {
echo ' checked="checked"';
}

<?php echo ($items_pool[$item->id] ? ' checked="checked" ' : ''); ?>
Another option would be to use array_keys to grab the keys of the selected items and then do something like in_array($item->id, $item_keys). All depends on how you feel like doing the work. The tertiary operators are the easiest way to do what you want to inline. You can also use a simple if statement to echo the attribute if you want it.
if($items_pool[$item->id]) echo ' checked="checked"';

Related

How to remove an item from a nested array in PHP?

I have a complicated nested array which I want to remove all items and their children with specific store_id:
It's really difficult and I don't know how to figure it out.
Array
(
[0] => Array
(
[cart_id] => 89
[product_id] => 46
[store_id] => 2
[option] => Array
(
[0] => Array
(
[product_option_id] => 92
[value] => Aqua
)
[1] => Array
(
[product_option_id] => 91
[value] => 85C
)
)
)
[1] => Array
(
[cart_id] => 90
[product_id] => 46
[store_id] => 2
)
Many thanks for any kind help.
If you want to remove the entire array element if it has a specific store_id, you just need to loop over the array, check the store_id and remove the element if you don't want it anymore.
E.g.:
<?php
foreach($data as $key=>$row){
if($row['store_id'] == 2){
unset($data[$key]);
}
}
?>
You can change that '2' to be anything you want to specifically remove a store. Or you could change the if to match an array of ids if you want to match several stores.
Here is the example for unsetting the cart_id in multi dimensional array.
<?php
foreach($data as $key=>$row){
unset($data[$key]['cart_id']);
}
?>

PHP - Echo an array data to form_input

How to echo an array to a form?
My array
Array
(
[0] => Array
(
[id] => item1
[price] => 300
[quantity] => 1
)
[1] => Array
(
[id] => item2
[price] => 400
[quantity] => 2
)
)
I tried like (codeigniter):
echo form_textarea('detail_prod',print_r($detail));
And I got like
what happen? why the result is 1 ?
It's possible to echo to a form in array format?
Just pass the 2nd parameter true like below:
echo form_textarea('detail_prod',print_r($detail, true));
For your help:
http://php.net/manual/en/function.print-r.php
Hope this will useful.

How to classify array items?

I have a query which fetches all comments for question and answers in one page (each page is containing at least one post (question), and maybe plus one or more posts (answers)). Now I need to separate comments according to its own question or answers.
The result of my query (fetching all comments) is something like this:
while ($results = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<pre>';
print_r($results);
}
/* Output:
Array
(
[id] => 1
[post_id] => 1
[content] => Have you any tried so far?
)
Array
(
[id] => 2
[post_id] => 3
[content] => Great answer, upvote
)
Array
(
[id] => 3
[post_id] => 3
[content] => That semicolon is redundant in line 5. Pplease edit it
)
Array
(
[id] => 4
[post_id] => 2
[content] => Won't work ...!
)
Array
(
[id] => 5
[post_id] => 3
[content] => #alex You are right thanks, Edited.
)
So, as you see in the output, all comments are mixed.. Now I need to classify them. Something like this:
/* NewOutput:
[1] => Array
post_id ^ (
[0] => Array
(
[id] => 1
[content] => Have you any tried so far?
)
)
[2] => Array
post_id ^ (
[0] => Array
(
[id] => 4
[content] => Won't work ...!
)
)
[3] => Array
post_id ^ (
[0] => Array
(
[id] => 2
[content] => Great answer, upvote
)
[1] => Array
(
[id] => 3
[content] => That semicolon is redundant in line 5. Pplease edit it
)
[2] => Array
(
[id] => 5
[content] => #alex You are right thanks, Edited.
)
)
Well, Now I want to know, how can I create a nested array according to the value of specific key in the array? exactly like above.
Try this...
$output = array();
foreach($results as $r){
$key = "post_".$r["post_id"];
if(!array_key_exists($key, $output)){
$output[$key] = array();
}
array_push($output[$key], array("id"=>$k["id"], "content"=>$k["content"]));
}
print_r($output);

array_reduce to use dynamic variables pass in second function

I have below $test array
Array
(
[0] => Array
(
[quantity] => 3
[stock_id] => _PHONE
)
[1] => Array
(
[quantity] => 3
[stock_id] => 102
)
[2] => Array
(
[quantity] => 4
[stock_id] => _PHONE
)
[3] => Array
(
[quantity] => 3
[stock_id] => 102
)
[4] => Array
(
[quantity] => 4
[stock_id] => _PHONE
)
[5] => Array
(
[quantity] => 6
[stock_id] => _PHONE
)
[6] => Array
(
[quantity] => 2
[stock_id] => 102
)
)
and to sum same stock_id keys to one, i use below functions:
function sum($array, $key){
isset($array[$key['stock_id']]) ? $array[$key['stock_id']]['quantity'] += $key['quantity'] : $array[$key['stock_id']] = $key;
return $array;
};
//merge same stock_id and sum the quantity same stock id
$sum_same_stock_id = array_reduce($test, "sum");
and the result went well like below:
$sum_same_stock_id:
Array
(
[_PHONE] => Array
(
[quantity] => 17
[stock_id] => _PHONE
)
[102] => Array
(
[quantity] => 8
[stock_id] => 102
)
)
So the question here is, I wanted to pass an dynamic keys values not just fixed values stock_id & quantity in sum function above. Have tried variety ways but still can't figured out the way. And can we put those functions into class as well?
Any advise is appreciated!
Maybe you can use the "use" function for the callback?
See https://www.php.net/manual/en/functions.anonymous.php

Two Arrays, One Output (How to ForEach?)

Is there a way to foreach() through one array based on a matching value with a different key in another array? In this example, I have a category array ($cat_data) with cat_id as a key and an image array ($img_data) with category_id as a key.
Array (
[0] => Array (
[cat_id] => 1
[cat_name] => Category 1
)
[1] => Array (
[cat_id] => 2
[cat_name] => Category 2
)
)
Array (
[0] => Array (
[img_id] => 2
[img_name] => demo1.jpg
[img_label] => Demo 1
[category_id] => 2
[img_order] => 1
)
[1] => Array (
[img_id] => 3
[img_name] => demo2.jpg
[img_label] => Demo 2
[category_id] => 2
[img_order] => 2
)
[2] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
)
What I want is to output my display so it looks like the following:
Category 1
demo3.jpg
Category 2
demo1.jpg
demo2.jpg
Since I'm really not great at fully grasping arrays, I thought I'd try Stack, and I haven't been able to find an answer to my question, partially because I'm not sure what to ask for precisely. Any help??
The naïve way:
foreach ($cat_data as $cat) {
echo $cat['cat_name'];
foreach ($img_data as $img) {
if ($img['category_id'] != $cat['cat_id']) {
continue;
}
echo $img['img_name'];
}
}
This is rather inefficient, since it loops through the $imgs array several times, but easy and works.
More efficient:
$images = array();
foreach ($img_data as $img) {
$images[$img['category_id']][] = $img;
}
foreach ($cat_data as $cat) {
echo $cat['cat_name'];
if (isset($images[$cat['cat_id']])) {
foreach ($images[$cat['cat_id']] as $img) {
echo $img['img_name'];
}
}
}
This first groups all images by category into a new array, which you can then loop over directly once.
I would urge you to redesign your array when you fill them with data to instead look something like this.
Array (
[0] => Array (
[cat_id] => 1
[cat_name] => Category 1
[images] = Array(
[0] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
)
)
[1] => Array (
[cat_id] => 2
[cat_name] => Category 2
[images] = Array(
[0] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
[1] => Array (
[img_id] => 2
[img_name] => demo1.jpg
[img_label] => Demo 1
[category_id] => 2
[img_order] => 1
)
)
)
)
Then you would have all the relational data connected and would just have to loop through your array of categories and print the images associated with each one in turn. The numbered indexes could even be changed to associative names if the id of the catagory weren't important for example. Then the array could be indexed with the name of the category and just contain the images of that category.
If the images are to be used in other places where you initial layout of those fits better you could still use this layout for your main data graph. Just replace the actual data of the images in the images array under each category with a reference to the actual image object.

Categories