Put values in a table from checkbox - php

I have a problem. I want to put dates into an array but when I make print_r() I get only the last value from checkbox.
My code is:
$id = Input::get('id');
$aObjects = Input::get('aObjects');
$iCount = count($aObjects);
for($i=0; $i < $iCount; $i++)
{
$test = array ($aGoupes = array(
'idGroupe' => $id,
'idObject' => $aObjects[$i]
));
}
echo '<pre>';
print_r($test);
echo '</pre>';
The output is:
Array
(
[0] => Array
(
[idGroupe] => 6
[idObject] => 8
)
)
So the problem is that only the last value checked from checkbox is put in this table. Please help me!! Thnx

Your problem is that you're resetting $test each time.
Try this:
$id = Input::get('id');
$aObjects = Input::get('aObjects');
$iCount = count($aObjects);
$test = array();
for ($i = 0; $i < $iCount; $i++) {
$test[] = array (
'idGroupe' => $id,
'idObject' => $aObjects[$i]
);
}
echo '<pre>';
print_r($test);
echo '</pre>';
I'm not too sure what your code is supposed to do, but the idGroupe will always be the same in each array, as you're setting it to $id which is never changed. That may well be correct, though.

Related

How can I delete data from my array with the key value?

I am trying to delete all the data from within a specific key in my array, but cant work out why my code isn't working. I have used print_r to check the code works and I can see that the correct value is being printed for each array when I click the 'remove' button, but it isn't removing the data from the key.
My current array looks like this:
Array
(
[0] => Array
(
[0] => S30813-Q100-X303
[1] => 5
[2] => Refurbished
)
[1] => Array
(
[0] => JX-1T1-LTU
[1] => 8
[2] => New
)
)
I am outputting the data to a table with:
for ($row = 0; $row < $totalcount; $row++) {
echo "<tr>";
for ($col = 0; $col < 3; $col++) {
echo "<td>".$contents[$row][$col]."</td>";
}
for ($col = 0; $col < 1; $col++) {
$del = $row;
echo "<td><form action='' method='post'><input type='text' name='del' value=".$row."></input><input type='submit' name='deletepart' value='Remove'></input></form></td>";
}
echo "</tr>";
}
Front-end:
My php to unset the array key (and where I am guessing the problem lies) is:
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
$value_to_delete = $key;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>
Any help on where I am going wrong here would be very much appreciated!
try this
write $key = $value_to_delete; instead of $value_to_delete = $key
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
//$value_to_delete = $key; //instead of this use
$key = $value_to_delete;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>

replace values of keys in json1 from Json2

I am very very new to php.. actually i am from java domain. But, i have to do some work in php for integration. My scenario is, i have one json array which will have 4 keys for ex:
one json --> {"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}.
I will be getting another JSON which ever edited from admin panel. for example if i updated any key, only that key will coming in the
second JSON --> for ex: {"blog_heading":"def"}
Now, i have to replace the value of second json to first json. example output for above scenario like I am very very new to php.. actually i am from java domain. But, i have to do some work in php for integration. My scenario is, i have one json array which will have 4 keys for ex:
output json --> {"id":7,"active":1,"blogId":"abc","blog_heading":"def"}.
So i am trying as below,
$id = json_decode($data_string);
$id2 = json_encode($post);
$id5 = json_decode($id2);
$id6 = array();
foreach ($id as $key => $value)
{
$log->debug($key . ': ' . $value);
if (array_key_exists($key, $id5->data)) {
$log->debug($key . 'element is in the array');
$log->debug($value . 'element is in the array');
//array_push($id5, "apple", "raspberry");
$id3 = array($key => $value);
$id3[$key] = $value;
$log->debug($id3);
}else{
$log->debug($key . 'element is not in the array');
}
}
$id7 = json_encode($id2);
$log->debug($id7);
id5 data is : $id5
DEBUG - 2017-06-05T02:26:20-04:00 - stdClass Object
(
[meta] => stdClass Object
(
[table] => story
[type] => item
)
[data] => stdClass Object
(
[id] => 7
[active] => 1
[blogId] => abc
[blog_heading] => xyz
)
)
==================
Log of $id :
stdClass Object
(
[active] => 1
[blog_heading] => def
[id] => 7
)
Please suggest me how can i achieve this... Anything i am doing wrong here
Please try that:
$j1 = '{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}';
$j2 = '{"blog_heading":"def"}';
$result = json_encode(
array_merge(
json_decode($j1, true),
json_decode($j2, true)
)
);
<?php
$json1='{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}';
$json2='{"blog_heading":"def"}';
$json1=json_decode($json1);
$json2=json_decode($json2);
foreach ($json1 as $key => $value) {
if($json2->$key){
$json1->$key=$json2->$key;
}
}
$json1=json_encode($json1);
$json2=json_encode($json2);
If you have only one element in array,Do like this
$a = json_decode('{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}',true);
$b = json_decode('{"blog_heading":"def"}',true);
$a['blog_heading'] = $b['blog_heading'];
print_r($a);
If you have multiple element like this :
$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"blog_heading":"def"},{"blog_heading":"hello"}]',true);
$return = array();
for ($i=0; $i < count($c); $i++) {
$c[$i]['blog_heading'] = $d[$i]['blog_heading'];
$return[] = $c[$i];
}
print_r($return);
If you want to replace value by specific id
$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"id":7,"blog_heading":"def"},{"id":9,"blog_heading":"hello"}]',true);
$return = array();
for ($i=0; $i < count($c); $i++) {
if($d[$i]['id'] == $c[$i]['id']) {
$c[$i]['blog_heading'] = $d[$i]['blog_heading'];
}
$return[] = $c[$i];
}
print_r($return);
Checking dynamic key value pair :
$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"id":6,"blog_heading":"def"},{"id":9,"blog_heading":"hello"}]',true);
$return = array();
for ($i=0; $i < count($c); $i++) {
$result = array_intersect_key($c[$i], $d[$i]);
foreach ($result as $key => $value) {
$c[$i][$key] = $d[$i][$key];
}
$return[] = $c[$i];
}
print_r($return);
Check demo here

Get an array through if statement

I have a array that I want to get using an if statement. This is what I have so far:
if($expPositive[0] <= 5 ){
$data = array();
$data[$i] = $user_id;
$i++;
}
$data_arr = $data;
print_r($data_arr);
When I run print_r($data_arr), this is the result:
Array ( [1] => 105 )
Sometimes the index is 0; i.e.:
Array ( [0] => 96 )
I would like to get this value in an if statement.
Can anyone help me figure this thing out? Any help is greatly appreciated.
Try this:
if($expPositive[0] <= 5 ){
$data = array();
$data[$i] = $user_id;
$i++;
}
$data_arr = $data;
print_r($data_arr);
$temp_key=array_keys($data_arr);
//Now $temp_key will contain keys of the array data_arr
//if $data_arr=Array([1]=>105),$temp_key[0] will have the value **1**
//if $data_arr=Array([0]=>96),$temp_key[0] will have the value **0**
//Now you can use if condition like this:
if($data_arr[$temp_key[0]/*some condition*/)
{
///your code
}

Weird incremented query result in a while

I don't understand why the result of the '$taille' array begin to the key [1] instead of key [0]? So it s displaying 3 results instead of 4 (occulting the first result)...
<?php
$req = $bdd->prepare('SELECT size FROM tailles_produits WHERE id_produit = ?');
$req->execute(array($_GET['id']));
$donnees = $req->fetch();
$numb_taille = array();
$taille = array();
$i = 0;
while($donnees = $req->fetch())
{
$i++;
$taille[$i] = $donnees['size'];
$numb_taille['total'] = $i;
}
$total = $numb_taille['total'];
echo '<pre>';
print_r ($taille);
echo '</pre>';
$req->closeCursor();
?>
Which gives
ARRAY
(
[1] => S
[2] => M
[3] => L
)
Instead of
ARRAY
(
[1] => XS
[2] => S
[3] => M
[4] => L
)
Can anyone help me with this pleas?
PHP arrays start with 0, so all you need to do is move your i++ down until after you're done using the data from that index
<?php
$req = $bdd->prepare('SELECT size FROM tailles_produits WHERE id_produit = ?');
$req->execute(array($_GET['id']));
$donnees = $req->fetch();
$numb_taille = array();
$taille = array();
$i = 0;
while($donnees = $req->fetch())
{
$taille[$i] = $donnees['size'];
$numb_taille['total'] = $i;
$i++; //iterate after your calculations are done
}
$total = $numb_taille['total'];
echo '<pre>';
print_r ($taille);
echo '</pre>';
$req->closeCursor();
?>
The problem is that you have an extra
$donnees = $req->fetch();
statement before the loop that fills in $taille. So the data from the first row is being fetched but not stored in the array.
It is because you increment $i before using it as a key for the new array item.
Do the increment at the end of your while loop.

Trying to do multidimentional array in PHP but something going wrong

I have an array that I declare like this:
$health_array = array( );
For every iteration I try to put these 3 items in it like this:
$health_array["num_problems"] = $num_problems;
$health_array["category_id"] = $category_id;
$health_array["category_name"] = $category_name;
But when I loop through the array I get gibberish. Here is how I loop through it:
foreach ($health_array as $i => $row)
{
echo '<p>'.$row['category_name'].' '.$row['category_id'].' '.$row['num_problems'].'</p>';
}
Any idea what I am doing wrong?
Thanks!!
Your problem comes from the fact that you want to do a multi-dimensional array and you're creating a monodimensional one, by overwriting each time the same 3 elements.
You should do something like:
$health_array = array();
$tmp = array();
$tmp["num_problems"] = 5;
$tmp["category_id"] = 8;
$tmp["category_name"] = "something";
$health_array[] = $tmp;
$tmp["num_problems"] = 15;
$tmp["category_id"] = 22;
$tmp["category_name"] = "something else";
$health_array[] = $tmp;
foreach ($health_array as $h)
{
echo $h["num_problems"]." - ".$h["category_id"]." - ".$h["category_name"]."<br />";
}
For every iteration I try to put these 3 items in it like this:
$health_array["num_problems"] = $num_problems;
$health_array["category_id"] = $category_id;
$health_array["category_name"] = $category_name;
It looks like you meant to build your array like this instead:
$health_array[] = array(
"num_problems" => $num_problems,
"category_id" => $category_id,
"category_name" => $category_name
);
This makes an array of arrays, where previously you were overwriting the keys of the same array for each iteration.

Categories