如何组合选择列表以确定哪个选择语句 [英] How to combine a list of choices to determine which select statement

查看:28
本文介绍了如何组合选择列表以确定哪个选择语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a mysql db and am using php 5.2

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

What I am trying to do is offer a list of options for a person to select (only 1). The chosen option will cause a select, update, or delete statement to be ran.

语句的结果不需要显示,虽然,显示旧的和那么新的会很好(那部分没有问题).

The results of the statement do not need to be shown, although, showing the old and then the new would be nice (no problems with that part tho'.).

伪代码:

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

Use either case (switch) or if/else statements to run the users 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 中分配为:

I figured the (3) statements would be assigned in php as:

$sql1="...".
$sql2="SELECT * FROM Ships"
$sql3="SELECT * FROM Ships WHERE nPort="1"

我的想法是使用 switch 语句,但我迷失了.:(

My idea was to use the switch statement, but got lost on it. :(

我希望这些选项一遍又一遍地可用,直到一个变量 ($choice)被选中.在哪种情况下,此特定页面已完成并返回主菜单"?

I would like the options to be available over and over again, until a variable ($choice) is selected. In which case, this particular page is done and goes back to the "Main Menu"?

编码和显示,如果我使用它,我可以做到.只是不确定如何编写选择方式我想要哪个.我可能会运行所有查询,而其他时候,只运行一,所以这就是我想要选择的原因.我感到困惑的一个领域是正确的形式使用诸如 -- ' ' " " 和 ...??

The coding and display, if I use it, I can do. Just not sure how to write the way to select which one I want. It is possible that I would run all of the queries, and other times, only one, so that is why I would like the choice. An area I get confused in is the proper forms to use such as -- ' ' " " and ...??

不确定我最终会得到的选项数量,但每页将超过 5 个但少于 20 个.因此,如果我让系统关闭 2-3 个选择,我可以根据需要复制它.而且,一如既往,如果存在更好的方法,我愿意尝试.

Not sure the # of options I will end up with, but it will be more than 5 but less than 20 / page. So if I get the system down for 2-3 choices, I can replicate it for as many as I may need. And, as always, if a better way exists, I am willing to try it.

再次感谢...

拉里

推荐答案

Switch 是一个不错的方法.if-then 也可以,这取决于需要.

Switch is a nice way to go. if-then is fine, too, depending on the needs.

就你的例子而言,你会谈论这样的事情:

For your example, you'd be talking about something like so:

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
}

如果您希望它可以重复使用,可以将其放入一个函数中:

If you'd like it to be re-usable, you could put it into a function:

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;
}

然后,你可以这样称呼它:

Then, you could just call it like so:

$mysql = getSQL($choice);

这篇关于如何组合选择列表以确定哪个选择语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆