我正在制作一个简单的菜单应用程序。我做了一个特殊的类Dish,我想在我的应用程序中按下按钮。首先,你必须选择是否要吃晚餐或饮料,它的工作完美。然后你必须选择你的菜肴(波兰语意大利墨西哥菜)并且它的效果很好,但是后来你应该可以选择是想要一顿正餐还是只是晚餐而且由于某种原因它不起作用。我像以前一样处理相同的规则,但事实并非如此。希望我能得到一些答案,而不是说我的代码有多糟糕。
<?php
class Dish
{
private $Price;
private $Name;
public function setData($Price, $Name)
{
$this->Price = $Price;
$this->Name = $Name;
}
function getPrice()
{
return $this->Price;
}
function getName()
{
return $this->Name;
}
}
// It could be much faster by using database...
$Polish_Dinner = new Dish;
$Polish_Dinner->setData("3$", "Kapuśniak");
$Polish_Desser = new Dish;
$Polish_Desser->setData("3$", "Kapuśniak z cukrem");
$Mexican_Dinner = new Dish;
$Mexican_Dinner->setData("4$", "Fasola");
$Mexican_Desser = new Dish;
$Mexican_Desser->setData("4$", "Fasola z cukrem");
$Italy_Dinner = new Dish;
$Italy_Dinner->setData("5$", "Pizza");
$Italy_Desser = new Dish;
$Italy_Desser->setData("5$", "Pizza z cukrem");
?>
<div style="background-color: gray;">
<?php
$zamowienie[10] = new Dish;
$i=0;
?>
<form action ="index.php" method="post">
<input type="submit" value="Dinner" name="Dinner" style="margin: 35px; display: inline; ;width: 35%; height: 300px" />
<input type="submit" value="Cash" name="Cash" style="display: inline; width: 10%; height: 75px" />
<input type="submit" value="Drink" name="Drink" style="margin: 35px ; display: inline; width: 35%; height: 300px"/></br>
</form>
</br></br></br>
<?php
if(isset($_POST['Dinner']))
{
?>
<form action ="index.php" method="post" style="background-color: darkgreen;">
<input type="submit" value="Mexican" name="Mexican" style="display: inline; width: 10%; height: 100px" />
<input type="submit" value="Polish" name="Polish" style="display: inline; width: 10%; height: 100px" />
<input type="submit" value="Italy" name="Italy" style=" display: inline; width: 10%; height: 100px"/></br>
</form>
<?php
}
elseif(isset($_POST['Drink']))
{
?>
<form action ="index.php" method="post" style="background-color: darkgreen;">
<input type="submit" value="ice" name="ice" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="lemon" name="lemon" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
}
switch(true)
{
case isset($_POST['Mexican']):
?>
<form action ="index.php" method="post" style="background-color: darkblue;">
<input type="submit" value="Mex_Lunch" name="Mex_Lunch" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="Mex_Desser" name="Mex_Desser" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
echo "Mexican";
if(isset($_POST['Mex_Desser']))
{
echo "Mex_Des";
$zamowienie[$i]=$Mexican_Desser;
$zamowienie[$i]->getName();
$i++;
$zamowienie[$i]=$Mexican_Dinner;
$zamowienie[$i]->getName();
$i++;
}
elseif(isset($_POST['Mex_Lunch']))
{
echo "Mex_Din";
$zamowienie[$i]=$Mexican_Dinner;
$i++;
}
echo $_POST['Mex_desser'];
break;
case isset($_POST['Polish']):
?>
<form action ="index.php" method="post" style="background-color: darkblue;">
<input type="submit" value="Pol_Lunch" name="Pol_Lunch" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="Pol_Desser" name="Pol_Desser" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
echo "POLISh";
if(isset($_POST['Pol_Desser']))
{
$zamowienie[$i]=$Polish_Desser;
$i++;
$zamowienie[$i]=$Polish_Dinner;
$i++;
}
elseif(isset($_POST['Pol_Lunch']))
{
$zamowienie[$i]=$Polish_Dinner;
$i++;
}
break;
case isset($_POST['Italy']):
?>
<form action ="index.php" method="post" style="background-color: darkblue;">
<input type="submit" value="It_Lunch" name="It_Lunch" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="It_Desser" name="It_Desser" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
echo "ITALY";
if(isset($_POST['It_Desser']))
{
echo "LUNCH + DESSER ITALY";
$zamowienie[$i]=$Italy_Desser;
$i++;
$zamowienie[$i]=$Italy_Dinner;
$i++;
}
elseif(isset($_POST['It_Lunch']))
{
echo "Lunch italy";
$zamowienie[$i]=$Italy_Dinner;
$i++;
}
break;
}
?>
</div>
此外:我真的愿意建议如何做得更好(我必须像这样使用css
,所以请跳过此评论。)
答案 0 :(得分:0)
这是代码的伪部分。
if ($dinner) {
mexican or polish or italy
also unsetting $dinner, so we won't get back here
}
if ($mexican) {
Lunch or dessert
also unsetting $mexican
if ($lunch) {
mexican lunch
}
}
当您点击以下路径时:
Dinner->Mexican->Lunch
您想输入if($mexican){ if ($lunch){}}
,换句话说:if $mexican AND $lunch
。
但是当您按下午餐按钮时,您取消设置$ mexican。所以$mexican AND $lunch
不会成真。你的嵌套是错误的。
我认为我的解释很难理解。我没有找到最好的单词,但我希望你理解它。
答案 1 :(得分:0)
就像我在上面的评论中写的那样,主要的问题是,如果显示墨西哥表格,那么在提交表格时就不再有$ _POST [&#39;墨西哥&#39;]。 / p>
以下解决方案用&#34; if-else&#34;替换您的开关。并移动你的&#34; if-isset-mex_desser&#34;这是在&#34; if-isset-mexican&#34;到一个独立的&#34; if-isset-mex_desser&#34;。
很难解释。请查看代码并尝试了解每个表单上发生的情况。
如果您希望在最后一次提交后显示第3个表单,那么您必须稍微修改它。
希望它有所帮助。
<?php
class Dish
{
private $Price;
private $Name;
public function setData($Price, $Name)
{
$this->Price = $Price;
$this->Name = $Name;
}
function getPrice()
{
return $this->Price;
}
function getName()
{
return $this->Name;
}
}
// It could be much faster by using database...
$Polish_Dinner = new Dish;
$Polish_Dinner->setData("3$", "Kapusniak");
$Polish_Desser = new Dish;
$Polish_Desser->setData("3$", "Kapusniak z cukrem");
$Mexican_Dinner = new Dish;
$Mexican_Dinner->setData("4$", "Fasola");
$Mexican_Desser = new Dish;
$Mexican_Desser->setData("4$", "Fasola z cukrem");
$Italy_Dinner = new Dish;
$Italy_Dinner->setData("5$", "Pizza");
$Italy_Desser = new Dish;
$Italy_Desser->setData("5$", "Pizza z cukrem");
?>
<div style="background-color: gray;">
<?php
$zamowienie[10] = new Dish;
$i=0;
?>
<form action ="index.php" method="post">
<input type="submit" value="Dinner" name="Dinner" style="margin: 35px; display: inline; ;width: 35%; height: 300px" />
<input type="submit" value="Cash" name="Cash" style="display: inline; width: 10%; height: 75px" />
<input type="submit" value="Drink" name="Drink" style="margin: 35px ; display: inline; width: 35%; height: 300px"/></br>
</form>
</br></br></br>
<?php
if(isset($_POST['Dinner']))
{
?>
<form action ="index.php" method="post" style="background-color: darkgreen;">
<input type="submit" value="Mexican" name="Mexican" style="display: inline; width: 10%; height: 100px" />
<input type="submit" value="Polish" name="Polish" style="display: inline; width: 10%; height: 100px" />
<input type="submit" value="Italy" name="Italy" style=" display: inline; width: 10%; height: 100px"/></br>
</form>
<?php
}
elseif(isset($_POST['Drink']))
{
?>
<form action ="index.php" method="post" style="background-color: darkgreen;">
<input type="submit" value="ice" name="ice" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="lemon" name="lemon" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
}
if(isset($_POST['Mexican'])) {
?>
<form action ="index.php" method="post" style="background-color: darkblue;">
<input type="submit" value="Mex_Lunch" name="Mex_Lunch" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="Mex_Desser" name="Mex_Desser" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
} else if(isset($_POST['Polish'])) {
?>
<form action ="index.php" method="post" style="background-color: darkblue;">
<input type="submit" value="Pol_Lunch" name="Pol_Lunch" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="Pol_Desser" name="Pol_Desser" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
} else if(isset($_POST['Italy'])) {
?>
<form action ="index.php" method="post" style="background-color: darkblue;">
<input type="submit" value="It_Lunch" name="It_Lunch" style=" display: inline;width: 10%; height: 100px" />
<input type="submit" value="It_Desser" name="It_Desser" style=" display: inline;width: 10%; height: 100px" />
</form>
<?php
} else if(isset($_POST['Mex_Desser'])) {
echo "Mex_Des";
$zamowienie[$i]=$Mexican_Desser;
$zamowienie[$i]->getName();
$i++;
$zamowienie[$i]=$Mexican_Dinner;
$zamowienie[$i]->getName();
$i++;
} else if(isset($_POST['Mex_Lunch'])) {
echo "Mex_Din";
$zamowienie[$i]=$Mexican_Dinner;
$i++;
} else if(isset($_POST['Pol_Desser'])) {
echo "LUNCH + DESSER POL";
$zamowienie[$i]=$Polish_Desser;
$i++;
$zamowienie[$i]=$Polish_Dinner;
$i++;
} else if(isset($_POST['Pol_Lunch'])) {
echo "Lunch pol";
$zamowienie[$i]=$Polish_Dinner;
$i++;
} else if(isset($_POST['It_Desser'])) {
echo "LUNCH + DESSER ITALY";
$zamowienie[$i]=$Italy_Desser;
$i++;
$zamowienie[$i]=$Italy_Dinner;
$i++;
} else if(isset($_POST['It_Lunch'])) {
echo "Lunch italy";
$zamowienie[$i]=$Italy_Dinner;
$i++;
}
?>
</div>