我有一个mysql数据库,正在使用php 5.2。
我正在尝试做的是提供一个选项列表供一个人选择(只有1个)。所选选项将导致运行select、update或delete语句。
语句的结果不需要显示,尽管,先显示旧的,然后显示新的将会很好(这部分没有问题)。
伪代码:
Assign $choice = 0
Check the value of $choice // This way, if it = 100, we do a break
Select a Choice:<br>
1. Adjust Status Value (+60) // $choice = 1<br>
2. Show all Ships <br> // $choice = 2
3. Show Ships in Port <br> // $choice = 3
...
0. $choice="100" // if the value =100, quit this part使用case (switch)或if/else语句运行用户choice1
If the choice is 1, then run the "Select" statement with the variable of $sql1
-- "SELECT ....
If the choice is 2, then run the "Select" statement with the variable of $sql2
--- SELECT * FROM Ships
If the choice is 3, then run the "Select" statement with the variable of $sql3 <br>
....
If the choice is 0, then we are done.我计算出(3)语句将在php中赋值为:
$sql1="...".
$sql2="SELECT * FROM Ships"
$sql3="SELECT * FROM Ships WHERE nPort="1"我的想法是使用switch语句,但却迷失了方向。:(
我希望这些选项一次又一次地可用,直到选择一个变量($choice)。在哪种情况下,此特定页面完成并返回到“主菜单”?
编码和显示,如果我使用它,我可以做到。只是不知道该怎么写来选择我想要的。有可能我会运行所有的查询,而其他时候,只运行一个,所以这就是我喜欢这个选择的原因。我感到困惑的一个方面是正确的形式,如--‘’“”和...??
我不确定我最终会有多少选项,但它将超过5个,但不到20个/页。因此,如果我让系统有2-3个选择,我可以复制它,因为我可能需要更多。而且,像往常一样,如果有更好的方法,我愿意尝试一下。
再次感谢..。
拉里
发布于 2012-11-15 06:57:49
Switch是一个很好的选择。根据需要,if-then也很好。
对于您的示例,您将讨论如下内容:
switch($choice) {
case 1:
$sql = $sql1="...".
break;
case 2:
$sql2="SELECT * FROM Ships"
break;
case 3:
$sql3="SELECT * FROM Ships WHERE nPort="1"
break;
default:
// Do some default behavior - as you mentioned
}如果你希望它是可重用的,你可以把它放入一个函数中:
function getSQL($choice) {
switch($choice) {
case 1:
$sql = $sql1="...".
break;
case 2:
$sql="SELECT * FROM Ships"
break;
case 3:
$sql="SELECT * FROM Ships WHERE nPort="1"
break;
default:
// Do some default behavior - as you mentioned
}
return $sql;
}然后,你可以这样叫它:
$mysql = getSQL($choice);https://stackoverflow.com/questions/13388736
复制相似问题