Foreach giving erroneous $value - php

This has me stumped. print_r displays the correct array indices and values, but the foreach construct retrieves erroneous values and even changes the value for the last index even though I'm not retrieving the values by reference (not using the ampersand).
<?php
require './includes/dbal.php';
require './includes/user.php';
require './includes/book.php';
session_start();
$title='My Shopping Cart';
include './template/header.php';
if(!isset($_SESSION['user']))
{
die('You are not logged in.');
}
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if(isset($_POST['submit']) && strcmp($_GET['mode'], 'add') == 0)
{
if(filter_var($_POST['qty'], FILTER_VALIDATE_INT) == FALSE)
{
echo '<div style="color: red;">Invalid quantity specified. Please go back and use a valid quantity.</div>';
}
else
{
$_SESSION['cart'][$_POST['book_id']] = $_POST['qty'];
}
}
else if(isset($_POST['update']) && strcmp($_GET['mode'], 'update') == 0)
{
foreach($_SESSION['cart'] as $key => &$value)
{
if((int) $_POST["qty_$key"] === 0)
{
unset($_SESSION['cart']["$key"]);
}
else
{
$value = $_POST["qty_$key"];
}
}
}
echo '<h3>Your shopping cart</h3>';
$db = new DBal();
$total=0;
echo '<div id="cart-items"><ul><form action="./cart.php?mode=update" method="post">';
// echo 'Original array: '; print_r($_SESSION['cart']);
foreach($_SESSION['cart'] as $key => $value)
{
// echo '<br />$key => $value for this iteration: ' . "$key => $value<br />";
// print_r($_SESSION['cart']);
$b = new Book($key, $db);
$book = $b->get_book_details();
$total += $value * $book['book_nprice']
?>
<li>
<div><img src="./images/books/thumbs/book-<?php echo $book['book_id']; ?>.jpg" title="<?php echo $book['book_name']; ?>" /></div>
<span class="cart-price">Amount: Rs. <?php echo $value * $book['book_nprice']; ?></span>
<h3><?php echo $book['book_name']; ?> by <?php echo $book['book_author']; ?></h3>
Price: Rs. <?php echo $book['book_nprice']; ?><br /><br />
Qty: <input type="number" name="qty_<?php echo $book['book_id']; ?>" maxlength="3" size="6" min="1" max="100" value="<?php echo $value; ?>" /><br />
</li>
<?php } echo "<span class=\"cart-price\">Total amount: $total</span>" ?>
<br />
<input type="submit" name="update" value="Update Cart" />
</form></ul></div>
<?php include './template/footer.html'; ?>
Sample output after pressing the update button is like this :
Original array:
Array (
[9] => 6
[8] => 7
[3] => 8
)
$key => $value for this iteration: 9 => 6
Array (
[9] => 6
[8] => 7
[3] => 6
)
$key => $value for this iteration: 8 => 7
Array (
[9] => 6
[8] => 7
[3] => 7
)
$key => $value for this iteration: 3 => 7
Array (
[9] => 6
[8] => 7
[3] => 7
)
The value for the last index gets changes to the value of the current index in every iteration. This results in the last value output having the same value as the second-to-last index.
Help?

You were using &$value as reference before:
foreach($_SESSION['cart'] as $key => &$value)
The variable continues to exist as reference beyond the loop, using it again in a loop has expected but non-obvious side effects. This is even mentioned in a big red box in the manual. unset($value) after the first loop to avoid that.

You are using references here:
foreach($_SESSION['cart'] as $key => &$value)
Either don't use reference here or unset $value immediately after the loop.

Related

get the next key in array in php foreach loop

I have a select box and wanting to get the key of the next value in array to go with the option here is my code
<select>
<?php foreach ($make as $key => $make):?>
<option value="<?php echo next($key);//not correct ?> - <?php echo $key; ?>"> <?php echo $make; ?></option>
<?php endforeach;
Here is the array
Array
(
[0] => Brand
[1] => Alfa Romeo
[123] => Alpina
[142] => Aston Martin
[152] => Audi
[619] => Bentley
[640] => BMW
[1122] => Buick
)
This will work with an associative array as well as numerically indexed:
<?php foreach ($make as $key => $value):?>
<?php next($make); ?>
<option value="<?php echo key($make); ?> - <?php echo $key; ?>"> <?php echo $value; ?></option>
<?php endforeach; ?>
Note, it's confusing and probably a bad idea to use the same variable name for iterating as the array. so instead of foreach ($make as $key => $make) I did foreach ($make as $key => $value) here.
The code above simply advances the pointer on the array and then gets the key for your option value using key(). Since there is no next key on the final array element, the value will be empty.
You can resolve your issue with a lot of solution, but if you have an assoiatve array or none order array or not numirc, you can use this:
<?php
$array = ["a" => 1, "b" => 2, "c" => 3, 1 => "aa"];
next($array);
$key = key($array);
echo $key;
prev($array);
$key = key($array);
echo $key;
Else, if array is ordered and numeric you can use $key + 1;
Simple way to get key of next element is to use array_search of next element of array e.g.
<?php foreach ($makers as $key => $make):?>
<option value="<?php echo array_search( next($makers), $makers );"> <?php echo $make; ?></option>
<?php endforeach;
Hope this helps.
try this function. it finds current key of given value in the array, and from that found key's position, you can get next/previous key with $increment
Ex: when $increment=1, it finds next key
when $increment=2, it finds 2 next key
when $increment=-1, it finds 1 previous key, and so on.
function sov_find_key($findvalue, $array, $increment) {
reset($array);
$key = array_search($findvalue, $array);
if ($key === false || $key === 0){
return false;
}
if ($increment === 0){
return $key;
}
$isNegative = $increment < 0 ? true:false;
$increment = abs($increment);
while(key($array) !== $key) {
next($array);
}
$x=0;
while($x < $increment) {
if( $isNegative ){
prev($array);
} else {
next($array);
}
$x++;
}
return key($array);
}
DEMO: https://3v4l.org/CSSmR
<select>
<?php
foreach ($make as $key => $make):
?>
<option value="<?php echo $key + 1; ?> - <?php echo $key; ?>">
<?php echo $make;
?>
</option>
<?php
endforeach;
Just increment the $key by 1, and you will have the next array element if the keys are in order and are numeric.

how to put array form data in update query and remove array to string conversion from update query

when using foreach loop its inner foreach loop two time more runs examples:-assumes
first foreach loop runs two time and second inner foreach loop runs one time now happened here is that first foreach loop runs two time and inner foreach also runs two times , due to this resign i am unable to get id and edit data in the database.
input array in this form :-
Array ( [reps_value] => Array ( [0] => q1 [1] => q2 ) [reps_percent] => Array ( [0] => e1 [1] => e2 ) [id] => Array ( [0] => 1 [1] => 2 ) )
i am getting null value in my query
UPDATE dev_prescription2 SET `reps_percent`= '',`reps_value`= '' WHERE id= ''
In update query value not showing and array to string conversion showing some times
public function prescriptionUpdateData($data) {
//print_r("Print");
print_r($data);
foreach( $data as $data1){
print_r( $reps_percent = $data1['reps_percent']);
print_r($id = $data1['id']);
print_r($reps_value = $data1['reps_value']);
$result = $this->db->query("UPDATE dev_prescription2 SET `reps_percent`= '".$reps_percent."',`reps_value`= '".$reps_value."'
WHERE
id= '".$id."'");
}
$insertid = $this->db->insert_id();
if ($result > 0) {
return TRUE;
} else {
return $this->db->_error_message();
}
}
my html format is :-
<?php foreach ($data['prescription_week1'] as $key=>$value){
$location[] = ($value['reps_value']);
$location[] = ($value['reps_percent']);
$location[] = ($value['id']);
}
// print_r($location);
?>
i am using array in html format
<td align="center" valign="middle" bgcolor="#FFFFFF"><font face="Agency-Roman" color="#000000"><br></font></td>
<td align="center" valign="middle" bgcolor="#F2F2F2" sdval="20" sdnum="1033;"><font face="Agency-Roman" color="#000000"><input style="width:90%" type="text" name="reps_value[]" id="reps_value" value="<?php echo $location[0] ?>" class="languages_list" /></font></td>
<td align="center" valign="middle" bgcolor="#F2F2F2" sdnum="1033;0;0.0%"><font face="Agency-Roman" size="1" color="#FF0000"><input style="width:90%" type="text" name="reps_percent[]" id="reps_value" value="<?php echo $location[1] ?>" /><input style="width:90%" type="hidden" name="id[]" id="id" value="<?php echo $location[2] ?>"/></font></td>
<td align="left" valign="bottom" bgcolor="#FFFFFF"><font color="#000000"><br></font></td>
<td align="center" valign="middle" bgcolor="#F2F2F2" sdval="20" sdnum="1033;"><font face="Agency-Roman" color="#000000"><input style="width:90%" type="text" name="reps_value[]" id="reps_value" value="<?php echo $location[3] ?>"/></font></td>
<td align="center" valign="middle" bgcolor="#F2F2F2" sdnum="1033;0;0.0%"><font face="Agency-Roman" size="1" color="#FF0000"><input style="width:90%" type="text" name="reps_percent[]" id="reps_value" value="<?php echo $location[4] ?>" /><input style="width:90%" type="hidden" name="id[]" id="id" value="<?php echo $location[5] ?>"/></font></td>
<td align="left" valign="bottom" bgcolor="#FFFFFF"><font color="#000000"><br></font></td>
my databse table is :-
enter image description here
this is my controller part:-
public function prescriptionUpdate() {
$response = $response = $this->Godspeed->prescriptionUpdateData($_POST);
}
Hey Developer here is your problem and solution:
Your array is this:
Array (
[reps_value] => Array (
[0] => q1
[1] => q2
)
[reps_percent] => Array (
[0] => e1
[1] => e2
)
[id] => Array (
[0] => 1
[1] => 2 )
)
As you can see here your $data is array with three keys that contain arrays with in them.
So your loop should be like this:
foreach ($data as $key => $value) {
if($key == 'reps_value'){
// Do something with reps_value arrey
}
if($key == 'reps_percent'){
// Do something with reps_percent array
}
if($key == 'id'){
$idArray = $value;
// Here you can loop again over the ids
// your current $id is blank because its an array and not an integer
}
}
You may want to create function to break up your foreach loop so that it doesn't get too complicated with 3 if statments.
Eg:
You can create functions for each key
foreach ($data as $key => $value) {
if($key == 'reps_value'){
reps_value($value);
}
if($key == 'reps_percent'){
reps_percent($value);
}
if($key == 'id'){
ids($value);
}
}
public function reps_value($array){
// Loop over the array and do your functionality
}
public function reps_percent($array){
// Loop over the array and do your functionality
}
public function ids($array){
// Loop over the array and do your functionality
}
Let me know how this works or if you have any questions that in regards to my proposed solution.
Good luck with the project and welcome to StackOverflow
update per OP request:
Here is solution in same function just like you had it:
public function prescriptionUpdateData($data) {
$reps_values = null;
$reps_percents = null;
$ids = null;
foreach( $data as $key => $values){
if($key == 'reps_value'){
$reps_values = $values;
}
if($key == 'reps_percent'){
$reps_percents = $values;
}
if($key == 'id'){
$ids = $values;
}
}
$arrayLength = count($ids);
if($arrayLength == $reps_values && $arrayLength == $reps_percents){
for($i = 0; $i < $arrayLength; $i++){
$result = $this->db->query("UPDATE dev_prescription2 SET `reps_percent`= '".$reps_percents[$i]."',`reps_value`= '".$reps_values[$i]."' WHERE id= '".$ids[$i]."'");
if ($result > 0) {
return TRUE;
} else {
return $this->db->_error_message();
}
}
}else{
// Missmatched arrays
}
// $insertid = $this->db->insert_id(); this line is not required as you are doing the update
}

How to compare key and value in more than two array?

I am comparing three arrays in nested foreach conditions. Following are the arrays
Array
(
[master/city] => City
[master/national_holiday] => National Holiday
[master/operator_comments] => Operator Comments
[master/sensors] => Sensors
[master/modbus] => Modbus
[master/manufacturers] => Manufacturers
[master/make_model] => Make Model
[master/dispatch_vendors] => Dispatch Vendors
)
Array
(
[1] => View
[2] => Write
)
Array
(
[master/city] => 1
[master/national_holiday] => 2
[master/operator_comments] => 1
[master/sensors] => 2
[master/modbus] => 1
[master/manufacturers] => 2
[master/make_model] => 1
)
Now the scenario is as follows:-
My first foreach iteartes first array
Then in the same foreach i m using second foreach which itrates second array
again in second foreach i m using third foreach to iterate third array
In third foreach , i m comparing key of first array with the key of second array and comparing value of second array with key of third array
If above condition is satisfied then in my dropdown the specific option will append selected Like <option value="1" selected="">View</option>
I am using following code
<?php
$first_array = first_array();
$i = 1;
foreach($first_array as $k => $val) {
?>
<tr>
<td>{{ $i }}</td>
<td class="mailbox-name">{{ $val }}</td>
<td><?php $second_array = second_array(); ?>
<select class="form-control master-menu" name="master_menu[{{$k}}]">
<option value="">Select Role</option>
<?php
foreach ($second_array as $key => $value) {
foreach ($third_array as $mkey => $mval) {
?>
<option value="<?php echo $key; ?>"
<?php if (($mkey == $k) && ($mval == $key)) { echo "selected"; } ?>><?php echo $value; ?></option>
<?php } } ?>
</select>
</td>
</tr>
<?php $i++; } ?>
I am using above code and getting issue that in second array there two values and in third array five values so in my dropdown count of option are ten insted of two.
This is my output.
Please suggest me.
Maybe something like this? I have simplified the process to demonstrate what is happening. I have also added the correct select values:
foreach ($first_array as $key => $value) {
?>
<p><?php echo $value; ?></p>
<?php foreach ($second_array as $second_key => $second_value) { ?>
<?php if ($key == $second_key) { ?>
<select>
<?php foreach ($third_array as $third_key => $third_value) { ?>
<option <?php echo ($third_key == $second_value ? 'selected=selected' : null); ?>><?php echo $third_value; ?></option>
<?php } ?>
</select>
<?php } else { ?>
<select>
<?php foreach ($third_array as $third_key => $third_value) { ?>
<option ><?php echo $third_value; ?></option>
<?php } ?>
</select>
<?php } ?>
<?php } ?>
<?php
}
For example you may try this code
foreach ($tmparray as $innerarray) {
//check type
if (is_array($innerarray)) {
//echo through inner loop
foreach ($innerarray as $value) {
echo $value;
}
} else {
//one,two,three
echo $innerarray;
}
}

print array element using foreach loop

In my view i have
forach($array as $arr)
{
$data = array('fname' => $arr['first_name'],lname => $arr['lname']);
<input type="hidden" value="<?php print_r($data);?>" name="fnameData[]">
}
Now i am submitting form to controller and print print_r($this->input->post(fnameData)) it prints following array
Array(
[0] => Array([fname] => abc lname => aaa)
[1] => Array([fname] => xyz lname => bbb)
)
Now I want to print fname and lname both using foreach loop in controller
It gives me Illegal string offset 'fname'
Simple
foreach($your_array as $arr)
{
echo $arr['fname'];
}
UPDATE 2 :
<?php
forach($array as $arr)
{
$data = $arr['first_name'];
?>
<input type="hidden" value="<?php echo $data;?>" name="fnameData[]">
<?php
}
?>
foreach($array as $value){
echo $value["fname"];
}
<?php
foreach($array as $arr)
{
$data = $arr['first_name'];
?>
<input type="hidden" value="<?php echo $data;?>" name="first_name[]">
<?php
}
?>

Problem with array

I am running into issues with the following code:
$ids = '"' . implode('", "', $crumbs) . '"';
$motd = array();
$dober = $db->query("SELECT id, name, msg, datetime FROM tbl_depts td INNER JOIN tbl_motd tm ON td.id = tm.deptid WHERE td.id IN (" . $ids . ")");
while ($row = $dober->fetch_array()) {
$motd[] = $row;
}
A print_r reveals this:
Array
(
[0] => Array
(
[0] => 1
[id] => 1
[1] => Management
[name] => Management
[2] => New Management Rule!
[msg] => New Management Rule!
[3] =>
[datetime] =>
)
[1] => Array
(
[0] => 2
[id] => 2
[1] => Human Resources
[name] => Human Resources
[2] => DPS
[msg] => DPS
[3] =>
[datetime] =>
)
)
As such, I cannot use this code to generate things:
foreach ($motd[] as &$value) {
if ($motd['msg'] != "") {
if ($i == 0) {
?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab active"><?php echo $value['name']; ?></a></li>
<?
} elseif ($i == $len - 1) {
?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab"><?php echo $value['name']; ?></a></li>
<?php } else { ?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab"><?php echo $value['name']; ?></a></li>
<?
}
$i++;
}
}
Any ideas on what I'm doing wrong here?
EDIT: you might find it easier to understand if you read this first: Optimize this SQL query
First - your code will not work because of this two lines:
foreach ($motd[] as &$value) {
if ($motd['msg'] != "") {
You should use $motd, not $motd[] in foreach and check $value['msg'], not $motd['msg']
Second, try to use mysql_fetch_assoc instead of mysql_fetch_array
Third - there is no initial value for $i.
1.) You might have an issue with foreach ($motd[] as &$value) {
perhaps it should be foreach ($motd as &$value) {
2.) I would rather use a for() loop instead of a foreach.
for($a=0, $cnt=count($motd)-1; $a<=$cnt; $a++ )
{
if($motd[$a]["msg"] != "" )
{
#do something here
}
}
I have rewritten your code a bit. No need to define the whole HTML several times only because there is a small change in it (I only spotted active).
$i=0;
foreach ($motd as $value) {
if ($value['msg'] != "") {
$active = $i == 0 ? ' active' : ''; //based on the value of `$i`
?>
<li>
<a href="#"
title="content_<?php echo $value['id']; ?>"
class="tab<?php echo $active?>"><?php echo $value['name']; ?></a></li>
<?php
$i++;
}
}
As I noted in the comments earlier:
In foreach you have to specify the array itself, you do not need [].
Always initialize your $i.
You do not need &$value, you only need that reference if you want to modify your array in the foreach.

Categories