PHP bilan bir nechta o'zgaruvchini qo'shish

Shuning uchun men pizza topshirig'ini topshirishim kerak bo'lgan topshiriq ustida ishlayapman. Men yangi PHP-ga kirishga harakat qilaman va matematikani ishlashga harakat qilaman. Ya'ni, bu mening HTML

<!DOCTYPE HTML>
<meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta http-equiv="expires" content="0" />

<head>
  <title> HW05 </title>
  <link rel="stylesheet" href="hw05.css">
</head>
<body>
<form method="post" action="hw05.php">

Pizza Order


Customer Information

Name: <input type="text" name="name" size="15" />

Phone: <input type="text" name="number" size="15" />

<input type="submit" value="Order" />


Pizze Size

<input type="radio" name="size1" value="small"> Small
<input type="radio" name="size2" value="medium"> Medium
<input type="radio" name="size3" value="large"> Large


Toppings

<input type="checkbox" name="checkbox1" value="box"> Pepperoni
<input type="checkbox" name="checkbox2" value="box"> Sausage
<input type="checkbox" name="checkbox3" value="box"> Mushroom
<input type="checkbox" name="checkbox4" value="box"> Ham
<input type="checkbox" name="checkbox5" value="box"> Olives
<input type="checkbox" name="checkbox6" value="box"> Onions


Topping Price

$1.00
$1.50
$0.80
$1.25
$0.75
$0.50

</form>
</body>
</html>

Xullas, HTML dan men balonlarning sonini hisoblaydigan va kattalikning umumiy bahosini va to'planishlarni qo'shadigan PHP sahifasini yaratish kerak. PHP ni har bir tovush narxini va pizza kattaligini birgalikda qanday qilib qo'shishni aniqlay olmayman. Men PHP ni yutishlarni hisoblashga muvaffaq bo'ldim. Har qanday yordam katta rahmat.

0
bizga hw05.php ni ham ko'rsatishingiz kerak
qo'shib qo'ydi muallif Angelin Calu, manba

8 javoblar

Siz kabi bir narsa qilishingiz mumkin:

<input type="checkbox" name="topping[]" value="pepperoni"> Pepperoni 
<input type="checkbox" name="topping[]" value="sausage"> Sausage
<input type="checkbox" name="topping[]" value="mushroom"> Mushroom
<input type="checkbox" name="topping[]" value="ham"> Ham
<input type="checkbox" name="topping[]" value="olives"> Olives
<input type="checkbox" name="topping[]" value="onions"> Onions

hw05.php faylida:

$toppingToPriceMap = [
    "pepperoni" => 1,
    "sausage" => 0.8,
    "mushroom" => 1.25,
    "ham" => 0.75,
    "olives" => 0.5,
    "onions" => 0.75
];
if (isset($_POST["topping"]) {
   $numberOfToppings = count($_POST["topping"]);
   $cost = 0;
   foreach ($_POST["topping"] as $topping) {
       if (isset($toppingToPriceMap[$topping])) {
          $cost += $toppingToPriceMap[$topping]; 
       }
   }       
}

echo "Selected $numberOfToppings toppings at a cost of $".$cost;

Eslatma: Bu sizning kodingiz talab qiladigan tuzatishlardan biridir.

1
qo'shib qo'ydi

Siz kabi bir narsa qilishingiz mumkin:

<input type="checkbox" name="topping[]" value="pepperoni"> Pepperoni 
<input type="checkbox" name="topping[]" value="sausage"> Sausage
<input type="checkbox" name="topping[]" value="mushroom"> Mushroom
<input type="checkbox" name="topping[]" value="ham"> Ham
<input type="checkbox" name="topping[]" value="olives"> Olives
<input type="checkbox" name="topping[]" value="onions"> Onions

hw05.php faylida:

$toppingToPriceMap = [
    "pepperoni" => 1,
    "sausage" => 0.8,
    "mushroom" => 1.25,
    "ham" => 0.75,
    "olives" => 0.5,
    "onions" => 0.75
];
if (isset($_POST["topping"]) {
   $numberOfToppings = count($_POST["topping"]);
   $cost = 0;
   foreach ($_POST["topping"] as $topping) {
       if (isset($toppingToPriceMap[$topping])) {
          $cost += $toppingToPriceMap[$topping]; 
       }
   }       
}

echo "Selected $numberOfToppings toppings at a cost of $".$cost;

Eslatma: Bu sizning kodingiz talab qiladigan tuzatishlardan biridir.

1
qo'shib qo'ydi

html nuqtai nazaridan quyidagi o'zgarishlarni bajarish kerak:


<input type="radio" name="size" value="small"> Small
<input type="radio" name="size" value="medium"> Medium
<input type="radio" name="size" value="large"> Large

name özniteliğinin barcha 3-variant uchun o'lchovidir, shuning uchun foydalanuvchining 3 birini tanlashi mumkin.

Bekor qilish uchun siz shunga o'xshash biror narsa qilishni xohlaysiz, faqatgina farq bu variantlarni qatorda saqlamoqchi bo'lishidir.


<input type="checkbox" name="checkbox1" value="1"> Pepperoni
<input type="checkbox" name="checkbox[]" value="2"> Sausage
<input type="checkbox" name="checkbox[]" value="3"> Mushroom
<input type="checkbox" name="checkbox[]" value="4"> Ham
<input type="checkbox" name="checkbox[]" value="5"> Olives
<input type="checkbox" name="checkbox[]" value="6"> Onions

Qolganlari biz uchun taqdim etmagan hw05.php da bo'lishi kerak.

0
qo'shib qo'ydi

html nuqtai nazaridan quyidagi o'zgarishlarni bajarish kerak:


<input type="radio" name="size" value="small"> Small
<input type="radio" name="size" value="medium"> Medium
<input type="radio" name="size" value="large"> Large

name özniteliğinin barcha 3-variant uchun o'lchovidir, shuning uchun foydalanuvchining 3 birini tanlashi mumkin.

Bekor qilish uchun siz shunga o'xshash biror narsa qilishni xohlaysiz, faqatgina farq bu variantlarni qatorda saqlamoqchi bo'lishidir.


<input type="checkbox" name="checkbox1" value="1"> Pepperoni
<input type="checkbox" name="checkbox[]" value="2"> Sausage
<input type="checkbox" name="checkbox[]" value="3"> Mushroom
<input type="checkbox" name="checkbox[]" value="4"> Ham
<input type="checkbox" name="checkbox[]" value="5"> Olives
<input type="checkbox" name="checkbox[]" value="6"> Onions

Qolganlari biz uchun taqdim etmagan hw05.php da bo'lishi kerak.

0
qo'shib qo'ydi

To'plash bahosining narxini qanday qilib egallashingizdan qat'iy nazar, ammo har bir son uchun o'zgarmaydigan qiymat yaratish uchun $ ni ishlating va ularni qo'shing.

0
qo'shib qo'ydi
<? PHP $ topp1 = 4.2; $ topp2 = 4.3; $ topp3 = 5.0; $ toppxy = xy; $ sum = $ topp2 + $ topp2 + $ topp3 + $ toppxy; echo $ sum; ?>
qo'shib qo'ydi muallif Wembo Mulumba, manba

To'plash bahosining narxini qanday qilib egallashingizdan qat'iy nazar, ammo har bir son uchun o'zgarmaydigan qiymat yaratish uchun $ ni ishlating va ularni qo'shing.

0
qo'shib qo'ydi
<? PHP $ topp1 = 4.2; $ topp2 = 4.3; $ topp3 = 5.0; $ toppxy = xy; $ sum = $ topp2 + $ topp2 + $ topp3 + $ toppxy; echo $ sum; ?>
qo'shib qo'ydi muallif Wembo Mulumba, manba

Kod juda etarlicha sodda. Hukning mavjudligini tekshirib ko'rishingiz va qiymatlarni tekshirishingiz kerak. Shunday qilib, bir narsa.

$addonCost =0;
if (isset($_POST['checkbox1'])) {
    if ($_POST['checkbox1'] == "box") {
        $addonCost += 1; //adding $1.00
    }
}    
0
qo'shib qo'ydi

Kod juda etarlicha sodda. Hukning mavjudligini tekshirib ko'rishingiz va qiymatlarni tekshirishingiz kerak. Shunday qilib, bir narsa.

$addonCost =0;
if (isset($_POST['checkbox1'])) {
    if ($_POST['checkbox1'] == "box") {
        $addonCost += 1; //adding $1.00
    }
}    
0
qo'shib qo'ydi
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin