I have a code with an array containing a bunch of fruits with product id and price as keys for the different values.
I get the correct price, but how do I get the name of chosen product? For example if you choose the "apple 1", I want the answer to be "The product apple costs 5.95" where it now just says "The product costs 5.95". As you may see, I've tried to do an array_search without good results.
How should I do this?
<?php
$a3 = array('apple' => array('productid' => '1',
'price' => '5.95'),
'banana' => array('productid' => '2',
'price' => '122'),
'squash' => array('productid' => '3',
'price' => '47.2'),
'watermelon' => array('productid' => '4',
'price' => '1.2'),
'potato' => array('productid' => '5',
'price' => '6.04')
);
?>
<!DOCTYPE html>
<html>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<select name="products">
<?php
foreach ($a3 as $key => $value){ ?>
<option value="<?php echo $value['price']; ?>"><?php echo $key, " ", $value['productid']; ?></option>
<?php
} ?>
</select>
<input type="submit" name="showprice" value="Show the price">
<?php
if (isset($_POST['showprice'])){
$price = $_POST['products'];
$product = array_search("$price", $a3);
echo "The product ". $product ." costs ". $price .".";
}
?>
</form>
The option value shoud be a key. Because products price can be same then eachother. I mean when you post the form, your post variable need to be unique value "apple". Then you can get the product node from the array.
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<select name="products">
<?php
foreach ($a3 as $key => $value){ ?>
<option value="<?php echo $key; ?>"><?php echo $key, " ", $value['productid']; ?></option>
<?php
} ?>
</select>
<input type="submit" name="showprice" value="Show the price">
<?php
if (isset($_POST['showprice'])){
$name = $_POST['products'];
$product = $a3[$name];
$price = $product['price'];
$productid = $product['productid'];
echo "The product ". strtoupper($name) ." costs ". $price .".";
}
?>
</form>
This array_search function doesn't work as expected when you have this kind of a multi dimensional array. You could try and write a custom search function like this on top of the page:
function multiDimSearch($price, $arr){
foreach ($arr as $fruit => $val) {
if ($val['price'] == $price) {
return $fruit;
}
}
return false;
}
And then change:
$product = array_search("$price", $a3);
To
$product = multiDimSearch($price, $a3);
Hope this helps.
Peace! xD
Try this..
<?php
if (isset($_POST['showprice'])){
$price = $_POST['products'];
$product = "";
foreach($a3 as $k=>$a) {
if($a['price'] == $price) {
$product = $k;
break;
}
}
echo "The product ". $product ." costs ". $price .".";
}
?>
As per your array, i think there is no need to use array_search()
, you can also concatenate the $key
with product value as:
Example:
HTML:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<select name="products">
<?php
foreach ($a3 as $key => $value){ ?>
<option value="<?php echo $value['price']; ?>-<?php echo $key; ?>"><?php echo $key. " ". $value['productid']; ?></option>
<?php
} ?>
</select>
<input type="submit" name="showprice" value="Show the price">
</form>
PHP:
<?php
if (isset($_POST['showprice'])){
$data = explode('-',$_POST['products']);
$price = $data[0];
$product = $data[1];
echo "The product ". $product ." costs ". $price .".";
}
?>
Using concatenation for <?php echo $value['price']; ?>-<?php echo $key; ?>