how to recieve auto generated array as post variable - php

I have tried this simple code to generate an array which will send and a form data in post method. what is the way of receiving this array in desired page? Here is the code:
$serial = 0;foreach ($results as $row) {$serial = $serial + 1;
Html:
<input class="float-lt" type="radio" value=""; ?>" name="question-<?php echo "{$serial}"; ?>[]"/>
<input class="float-lt" type="radio" value=""; ?>" name="question-<?php echo "{$serial}"; ?>[]"/>

$used_serials = $_POST['serials];
foreach( $used_serials AS &$serial ){
$key = 'question-'.$serial;
$wanted_serial_pack = $_POST[$key];
}
OR better use an multidimensional Array structure like:
name="question['serials'][$serial]"
Then you can loop over $_POST['serials]

PHP
$serial = 0;
$inputs = array();
foreach ($results as $row) {
$serial = $serial + 1;
$inputs[] = "<input name=\"question['keys']['{$serial}'][]\"/>";
}
template.php:
echo implode(' ',$inputs);
Submit.php
$results = $_POST['keys];
foreach($results as $serial_array){
var_dump($serial_array);
}
Something like this may help.

Related

How to search a multidimensional associative array php

With api I get a multidimensional array php. I want to extract data from it with a simple code php.
I extract data from an array this way:
var_dump ($obj['product']);
echo $prise = ($obj['product']['return']['ProductsItem']['0']['Article']);
echo $Brand = ($obj['product']['return']['ProductsItem']['0']['Brand']);
echo $Currency = ($obj['product']['return']['ProductsItem']['0']['Currency']);
echo $Name = ($obj['product']['return']['ProductsItem']['0']['Name']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Price']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['TransferTime']);
echo $StockItem1 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Count']);
echo $StockItem2 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['1']['Count']);
How do I simplify the code? How can the data in the array change?
<?php
foreach ($obj['product']['return']['ProductsItem'] as $productsitem) {
$article = $productsitem['Article'];
$brand = $productsitem['Brand'];
$currency = $productsitem['Currency'];
$name = $productsitem['Name'];
foreach ($productsitem['Stock']['StockItem'] as $stockitem) {
$price = $stockitem['Price'];
$transfertime = $stockitem['TransferTime'];
$count = $stockitem['Count'];
}
}

PHP post foreach value to seperate array

So...
I have this foreach loop with information about a product and I want to save the information to a seperate array for each loop.
I was thinking of doing something like this:
$return_array = array();
foreach($items as $item) {
$return_array[] = $item;
}
But I have troubles doing it like this since I am using input values from html and I need to add those before they get send to a database.
My foreach goes like:
foreach($items as $item) {
<table>
<tr>
<td>
<input value="<?= $item->name ?>" name="item<?= $item->id ?>">
</td>
</tr>
... more table tags
<?php foreach($item as $key) { ?>
<input name ="item<?= $item->id ?>_label<?= $key->label ?>
<?php } ?>
... more table tags
<select name="item<?= item->id ?>_status>
//Choose the state the product is in
<option value="damaged">
<option value="good">
</select>
So after this gets submitted with the form (this is in a btw) I get something like this:
(depending on how many labels the product has this number can increase)
$array =
['item1'] = 'test';
['item1_label1'] = 123;
['item1_label2'] = 213;
['item1_status'] = 'good';
['item2'] = 'test2';
['item2_label1'] = 112;
['item2_label2'] = 1232;
['item2_label3'] = 132;
['item2_status'] = 'broken';`
Now what I want would be:
$array =
['item1'] = array[ //name of this doesn't matter
['item1'] = 'test'; // name
['item1_label1'] = 123; //label
['item1_label2'] = 213; //label
['item1_status'] = 'good'; //status
],
['item2'] = array[
['item2'] = 'test2'; //name
['item2_label1'] = 112; //label
['item2_label2'] = 1232; //label
['item2_label3'] = 132; //label
['item2_status'] = 'broken' //status
]
];
I want to create this information from the form. (also the number of items can increase).
try this,
$result = [];
foreach($array as $k => $v)
{
//$result[substr($k, 0, 5)][$k] = $v;
if(strpos($k, '-') === FALSE)
$result[$k][$k] = $v;
else
$result[substr($k, 0, strpos($k, '-'))][$k] = $v;
}

how to store values of foreach as array

<?php
$samgri = $this->crud_model->get_puja_samagri_by_puja_order($param1,$param);
foreach ($samgri as $row){
$puja_samagri = $row['puja_samagri'];
}
$puja_sam = explode(',', $puja_samagri);
foreach ($puja_sam as $samagri_id){
$samg = $this->crud_model->get_puja_samagri_by_id($samagri_id);
}
if(sizeof($samg) == 0){
echo '<h5 style = "text-align:center">No samagari available</h5>';
}else{
foreach ($samg as $row){
?>
but it was fetching only the last record not total records
You should add brackets after the array name like:
<?php
$samgri = $this->crud_model->get_puja_samagri_by_puja_order($param1,$param);
//Declare the arrays
$puja_samagri = new array();
$samg = new array();
foreach ($samgri as $row){
$puja_samagri[] = $row['puja_samagri'];
}
$puja_sam = explode(',', $puja_samagri);
foreach ($puja_sam as $samagri_id){
$samg[] = $this->crud_model->get_puja_samagri_by_id($samagri_id);
}
if(sizeof($samg) == 0){
echo '<h5 style = "text-align:center">No samagari available</h5>';
}else{
foreach ($samg as $row){
?>
It will use the automatic int position (from 0 going up untill you have all the results). Also it might be a good idea to declare them as arrays before using them.
You have to take array after every foreach veriable as like follows :
<?php
$samgri = $this->crud_model->get_puja_samagri_by_puja_order($param1,$param);
foreach ($samgri as $row){
$puja_samagri[] = $row['puja_samagri'];
}
$puja_sam = explode(',', $puja_samagri);
foreach ($puja_sam as $samagri_id){
$samg[] = $this->crud_model->get_puja_samagri_by_id($samagri_id);
}
if(sizeof($samg) == 0){
echo '<h5 style = "text-align:center">No samagari available</h5>';
}else{
foreach ($samg as $row){
?>
Hope this will help you :)

Array returns "Array"

This is my first time trying to create an array but it's not going so well.
My code scans every $_POST in a form when pressing a submit button below an input field. What I wanted it to do was to only fetch the content of a "filled" input. Well life's full of disappointments so then I tried to remove all the empty array elements. But instead of doing that, it echos the word "Array".
I've always been bad at arrays, ever since my C# days and I've never known why because they always look so simple. If anybody can tell me what I'm doing wrong, I'd appreciate it.
PHP code that goes above the <html>
<?php
//This is what I've tried before
//$klant = array();
foreach($_POST as $k => $v) {
if (substr($k,0,10) == 'newRecord') {
$id = substr($k,10);
$klant[] = $v;
$klant = array_filter($klant);
echo $klant;
}
}
?>
Markup
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<?php
$query = ("select * from table1");
$result = mysqli_query($con, $query) or die (mysqli_error());
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$id = $row['rowid'];
?>
<tr><!--There are multiple rows, that's why I'm using foreach($_POST as $k => $v)-->
<td>
<input class="newRecord<?php echo $id; ?>" type="text" name="newRecord<?php echo $id; ?>" />
<a href="?record=<?php echo $id; ?>">
<button class="saveRecord<?php echo $id; ?>" name="saveRecord<?php echo $id; ?>">Save</button>
</a>
</td>
</tr>
<?php } ?>
</table>
</form>
You cannot echo arrays use print_r or var_dump
for Pretty-printing arrays try this
echo "<pre>";
print_r($your_array);
echo "</pre>";
I believe this is what you want!
<?php
$klant = array();
foreach($_POST as $k => $v) {
if (substr($k,0,10) == 'newRecord' && trim($v)) {
$id = substr($k,10);
$klant[$id] = $v;
}
}
echo "<pre>";var_dump($klant);echo "</pre>";
?>
But I am not that confident that I understand the question clearly. Can you clarify a bit more than this(if this is not what you desire)?
Change this :
echo $klant;
To this :
print_r($klant);
Or this :
var_dump($klant);
echo is just for strings, print_r and var_dump will echo anything (strings, arrays, objects etc).
EDIT for OP EDIT :
This will remove anything that is empty inside the array :
foreach ($klant as $key => $value)
if (empty($value))
unset($klant[$key]);
print_r($klant);
Try this
foreach($_POST as $k => $v) {
if (substr($k,0,10) == 'newRecord') {
$id = substr($k,10);
$klant[] = $v;
}
}
var_dump($klant);

Retrieve post array values

I have a form that sends all the data with jQuery .serialize() In the form are four arrays qty[], etc it send the form data to a sendMail page and I want to get the posted data back out of the arrays.
I have tried:
$qty = $_POST['qty[]'];
foreach($qty as $value)
{
$qtyOut = $value . "<br>";
}
And tried this:
for($q=0;$q<=$qty;$q++)
{
$qtyOut = $q . "<br>";
}
Is this the correct approach?
You have [] within your $_POST variable - these aren't required. You should use:
$qty = $_POST['qty'];
Your code would then be:
$qty = $_POST['qty'];
foreach($qty as $value) {
$qtyOut = $value . "<br>";
}
php automatically detects $_POST and $_GET-arrays so you can juse:
<form method="post">
<input value="user1" name="qty[]" type="checkbox">
<input value="user2" name="qty[]" type="checkbox">
<input type="submit">
</form>
<?php
$qty = $_POST['qty'];
and $qty will by a php-Array. Now you can access it by:
if (is_array($qty))
{
for ($i=0;$i<size($qty);$i++)
{
print ($qty[$i]);
}
}
?>
if you are not sure about the format of the received data structure you can use:
print_r($_POST['qty']);
or even
print_r($_POST);
to see how it is stored.
My version of PHP 4.4.4 throws an error:
Fatal error: Call to undefined function: size()
I changed size to count and then the routine ran correctly.
<?php
$qty = $_POST['qty'];
if (is_array($qty)) {
for ($i=0;$i<count($qty);$i++)
{
print ($qty[$i]);
}
}
?>
try using filter_input() with filters FILTER_SANITIZE_SPECIAL_CHARS and FILTER_REQUIRE_ARRAY as shown
$qty=filter_input(INPUT_POST, 'qty',FILTER_SANITIZE_SPECIAL_CHARS,FILTER_REQUIRE_ARRAY);
then you can iterate through it nicely as
foreach($qty as $key=>$value){
echo $value . "<br>";
}
PHP handles nested arrays nicely
try:
foreach($_POST['qty'] as $qty){
echo $qty
}
I prefer foreach insted of for, because you do not need to heandle the size.
if( isset( $_POST['qty'] ) )
{
$qty = $_POST ['qty'] ;
if( is_array( $qty ) )
{
foreach ( $qty as $key => $value )
{
print( $value );
}
}
}

Categories