我的 HTML 輸入為
<input type="number" name="no_pi" id="no_pi" class="form-control" placeholder="Number of Pieces">
和其他兩個輸入作為
<input type="number" name="ht" id="ht" placeholder="Height of Package" class="form-control">
<input type="number" name="bd" id="bd" placeholder="Breadth of Package" class="form-control">
我想用戶3在件數欄位中輸入,然后20, 20, 30在重量和10, 10, 20寬度欄位中輸入。
然后 PHP 將對每一20, 10對和下一對進行計算,依此類推x,在 number ofpieces 欄位中分配的次數。
PS:上面定義的陣列是示例,如果用戶輸入 5 塊,那么兩個陣列都應該是 5 個值,PHP 將計算這 5 對。
任何想法如何實作這一目標
uj5u.com熱心網友回復:
您可以使用explode和array_map來實作這一點。
function inputToArray(string $string)
{
$string = preg_replace('/\s /', '', $string);
$string = trim($string, ",");
$array = array_map('intval', explode(',', $string));
return $array;
}
$no_of_pieces = (int)$_POST['no_pi'];
$ht = $_POST['ht']; //make sure it is in format 1,2,3,4,.....
$ht = inputToArray($ht); //array(20,30,10)
$bd = $_POST['bd']; //make sure it is in format 1,2,3,4,.....
$bd = inputToArray($bd); // array(10,20,30)
if ((count($ht) === count($bd)) && (count($bd) === $no_of_pieces)) {
for ($piece = 0; $piece < $no_of_pieces; $piece ) {
$areaOfPiece = $ht[$piece] * $bd[$piece];
echo $areaOfPiece;
}
} else {
echo "Number of inputs does not match";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/345930.html
