print array element using foreach loop - php

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
}
?>

Related

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
}

Creating Forms In a ForEach - PHP

I am working with an array in PHP. For each thing in the array, I want to create a form and button attached to it. In addition to that, an action when the button is pushed. I fear it may have something to do with timing or such.
Here is my code:
foreach($projects as $proj){
echo "<form action='post'><input type='button' name='forminput' Value ='Yup'></form>";
$name = $_POST['forminput'];
}
if($name){
echo "ye";
}
Thanks!
Something like this?
<?php
$arrayName = array('test1' => '1', 'test2' => '2', 'test3' => '3', 'test4' => '4');
foreach ($arrayName as $key => $value) {
$$key = $value;
echo '
<form action="" method="post" enctype="multipart/form-data">
<input type="submit" name="forminput" Value ="'.$$key.'">
</form>
';
}
foreach ($_POST as $key => $value) {
$key = strip_tags($key); // to prevent scripts being injected into the page
$value = strip_tags($value);
echo $key . ' ' . $value;
}
?>

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;
}
}

Foreach giving erroneous $value

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.

PHP how to loop through a post array

I need to loop through a post array and sumbit it.
#stuff 1
<input type="text" id="stuff" name="stuff[]" />
<input type="text" id="more_stuff" name="more_stuff[]" />
#stuff 2
<input type="text" id="stuff" name="stuff[]" />
<input type="text" id="more_stuff" name="more_stuff[]" />
But I don't know where to start.
This is how you would do it:
foreach( $_POST as $stuff ) {
if( is_array( $stuff ) ) {
foreach( $stuff as $thing ) {
echo $thing;
}
} else {
echo $stuff;
}
}
This looks after both variables and arrays passed in $_POST.
Likely, you'll also need the values of each form element, such as the value selected from a dropdown or checkbox.
foreach( $_POST as $stuff => $val ) {
if( is_array( $stuff ) ) {
foreach( $stuff as $thing) {
echo $thing;
}
} else {
echo $stuff;
echo $val;
}
}
for ($i = 0; $i < count($_POST['NAME']); $i++)
{
echo $_POST['NAME'][$i];
}
Or
foreach ($_POST['NAME'] as $value)
{
echo $value;
}
Replace NAME with element name eg stuff or more_stuff
I have adapted the accepted answer and converted it into a function that can do nth arrays and to include the keys of the array.
function LoopThrough($array) {
foreach($array as $key => $val) {
if (is_array($key))
LoopThrough($key);
else
echo "{$key} - {$val} <br>";
}
}
LoopThrough($_POST);
Hope it helps someone.
You can use array_walk_recursive and anonymous function, eg:
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');
array_walk_recursive($fruits,function ($item, $key){
echo "$key holds $item <br/>\n";
});
follows this answer version:
array_walk_recursive($_POST,function ($item, $key){
echo "$key holds $item <br/>\n";
});
For some reason I lost my index names using the posted answers. Therefore I had to loop them like this:
foreach($_POST as $i => $stuff) {
var_dump($i);
var_dump($stuff);
echo "<br>";
}

Categories