/    Sign up×
Community /Pin to ProfileBookmark

PHP foreach() function question on multiple variables

Iam trying to display data coming from three sets of arrays. $product[], $price[] and $platform[]. I am able to display products array of data correctly by using:

[code=php]
foreach ($_POST[“product”] as $product)
{
$product = “, “.trim($product);
$body .= “Product: $product”;
}
[/code]

now if i try to do two variables then it doesn work..does anyone know how to give multiple conditions the the foreach()? such as?

[code=php]
foreach (($_POST[“product”] as $product) && ($_POST[“price”] as $price))
{
$product = “, “.trim($product);
$body .= “Product: $product”;

$price = “, “.trim($price);
$body .= “price: $price”;
}

[/code]

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@Bootsman123Jun 15.2005 — Can't be done as far as I know. But if it are just normal arrays you can use this:
[code=php]
$price = $_POST['price'];
$platform = $_POST['platform'];

foreach($_POST['product'] as $key => $product)
{
echo "
Product: " . $product . "<br />
Price: " . $price[$key] . "<br />
Platform: " . $platform[$key] . "<br />
";
}
[/code]
Copy linkTweet thisAlerts:
@vicpal25authorJun 15.2005 — humm..so how can i pass variables from two different arrays to display continously... $prduct, $price..??
Copy linkTweet thisAlerts:
@Bootsman123Jun 15.2005 — Or you can do this:
[code=php]
$num = count($product);
$product = $_POST['product'];
$price = $_POST['price'];
$platform = $_POST['platform'];

for($i = 0; $i < $num; $i++)
{
echo "
Product: " . $product[$i] . "<br />
Price: " . $price[$i] . "<br />
Platform: " . $platform[$i] . "<br />
";
}
[/code]
Copy linkTweet thisAlerts:
@ShrineDesignsJun 15.2005 — you could use a while loop[code=php]<?php
while($product = each($_POST['product']) && $price = each($_POST['price']))
{
// ...
}
?>[/code]
Copy linkTweet thisAlerts:
@vicpal25authorJun 15.2005 — i tried doing the while loop but it gives me the Array on each result such as

[I]1Array1Array1Array1Array1Array1Array[/I]

I tried using:
[code=php]
while($product = each($_POST['product']) && $price = each($_POST['price']))
{
$product = ", ".trim($product);
$price = ", ".trim($price);

echo $product;
echo $price;
}
[/code]


it gives me:

[I], 1, Array, 1, Array, 1, Array, 1, Array, 1, Array, 1, Array[/I]

??
Copy linkTweet thisAlerts:
@vicpal25authorJun 15.2005 — Hey bootsman123, I got nothing when I use your for loop method.... does the variable i need to be started at 0?
Copy linkTweet thisAlerts:
@vicpal25authorJun 15.2005 — nevermind..it has been started at 0 on the for loop..humm.
Copy linkTweet thisAlerts:
@vicpal25authorJun 15.2005 — I hard coded the $num variable and it works..i guess it is not counting the $products variable..humm..

$num = 5;

$product = $_POST['product'];

$price = $_
POST['price'];

$platform = $_POST['platform'];

for($i = 0; $i < $num; $i++)

{

echo "

Product: " . $product[$i] . "<br />

Price: " . $price[$i] . "<br />

Platform: " . $platform[$i] . "<br />

";

}
Copy linkTweet thisAlerts:
@vicpal25authorJun 15.2005 — okay got it...i placed a counter field on the extraction of records of the previous page and i pass that into a hidden field and into the $num of the for loop..niceeee..thanks you guys...that worked ?
Copy linkTweet thisAlerts:
@Bootsman123Jun 15.2005 — Good =).
Copy linkTweet thisAlerts:
@ShrineDesignsJun 16.2005 — try this[code=php]<?php
while(list(, $product) = each($_POST['product']) && list(, $price) = each($_POST['price']))
{
// ...
}
?> [/code]
Copy linkTweet thisAlerts:
@memonsalmanJun 18.2019 — @ShrineDesigns#387327 Hi, This works for me i was looking for inserting multiple post array through on single while loop however i am able to insert last value in while loop. it always says your first value is empty.

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'musculo_textarea'
Copy linkTweet thisAlerts:
@memonsalmanJun 18.2019 — @memonsalman#1604957

$patient_ID = '18';


$sql10 = "INSERT INTO musculo_name (patient_ID, musculo_title, musculo_textarea) VALUES ($patient_ID, :musculo_title, :musculo_textarea)";

$query10 = $pdo->prepare($sql10);

while(list(, $musculo_textarea) = each($_POST['pdf_generatedtextmsm_textarea']) && list(, $musculo_title) = each($_POST['titleMsm']))

{

$query10->bindParam(':musculo_textarea', $musculo_textarea);

$query10->bindParam(':musculo_title', $musculo_title);

$stmt = $query10->execute();

}

if($stmt){

echo 'success';

}

else{

echo 'error';

}


Hi, This works for me i was looking for inserting multiple post array through on single while loop however i am able to insert last value in while loop. it always says your first value is empty.

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'musculo_textarea'
×

Success!

Help @vicpal25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...