1

我有一个 mysql 数据库并且正在使用 php 5.2

我想要做的是提供一个供人们选择的选项列表(只有 1 个)。选择的选项将导致运行选择、更新或删除语句。

不需要显示语句的结果,尽管显示旧的然后新的会很好(那部分没有问题。)。

伪代码:

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 语句来运行用户选择1

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 个选项中关闭系统,我可以根据需要复制它。而且,一如既往,如果存在更好的方法,我愿意尝试。

再次感谢...

拉里

4

1 回答 1

0

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);
于 2012-11-14T22:57:49.417 回答