返回循环结果PHP

时间:2016-11-12 20:06:46

标签: php oop

我试图在PHP中返回循环的内容(对于选择) 这是我的代码:

public function select($name, $field, $table) {
    $query = $this->db->query("SELECT $field FROM $table", []);
    $datas = $query->fetchAll();
    $number = $query->rowCount();
    return '<select class="form-control" name="' . $name . '">
                <option value="">' . $name . '</option>'
                    for ($i=0; $i < $number; $i++) { 
                        '<option value="' . $datas[$i]->$field . '" >' . $datas[$i]->$field . '</option>'
                    }
            '</select>';
}

我收到错误:(!)解析错误:语法错误,意外&#39;对于&#39; (T_FOR)

我知道我的代码是错的,但我无法找到一个简单的解决方案来实现我想要的目标。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

做类似的事情:

//Allow user to type in commands
void commandLine(){
    bool flag = true;
    do {
        cout << "Enter a command (format 'command x y z') : ";
        vector<string> v;
        string line;
        cin.ignore() //edit: seg fault without this
        getline(cin, line);
        char space = ' ';
        int p1, p2, p3, p4, p5, p6;
        auto i = 0;
        auto pos = line.find(space);
        while(pos != string::npos){
            v.push_back(line.substr(i, pos-i));
            i = ++pos;
            pos = line.find(space, pos);
            if (pos != string::npos)
                v.push_back(line.substr(i, line.length()));
        }
        if (v[0]=="size"){
            p1 = stoi(v[1]);
            p2 = stoi(v[2]);
            //call asm
            int ret = mysetSize(p1, p2);
            if (ret == 0){
                cout << "Something went wrong with the setSize command..." << endl;
            } else {
                cout << "Size set to " << p1 << "x" << p2 << endl;
            }
        }else if (v[0] == "clear"){
            //call asm
            int ret = myclear();
            if (ret == 0){
                cout << "Something went wrong with the clear command..." << endl;
            } else {
                cout << "Screen Cleared" << endl;
            }

        }else if (v[0] == "setBkgndColor"){
            p1 = stoi(v[1]);
            p2 = stoi(v[2]);
            p3 = stoi(v[3]);
            //call asm
            int ret = mysetBkgndColor(p1, p2, p3);
            if (ret==0){
                cout << "Something went wrong with the setBkgndColor command..." << endl;
            } else {
                cout << "Background color set to " << p1 << ", " << p2 << ", " << p3 << endl;
            }

        }else if (v[0] == "setDrawColor"){
            p1 = stoi(v[1]);
            p2 = stoi(v[2]);
            p3 = stoi(v[3]);
            //call asm
            int ret = mysetDrawColor(p1, p2, p3);
            if (ret == 0){
                cout << "Something went wrong with the setDrawColor command..." << endl;
            } else {
                cout << "Draw color set to " << p1 << ", " << p2 << ", " << p3 << endl;
            }

        }else if (v[0] == "rect"){
            p1 = stoi(v[1]);
            p2 = stoi(v[2]);
            p3 = stoi(v[3]);
            p4 = stoi(v[4]);
            vertex a;
            a.x = p1;
            a.y = p2;
            drawRect(a, p3, p4);

        }else if (v[0] == "circle"){
            p1 = stoi(v[1]);
            p2 = stoi(v[2]);
            p3 = stoi(v[3]);
            vertex a;
            a.x = p1;
            a.y = p2;
            drawCircle(a, p3);
        }else if (v[0] == "triangle"){
            p1 = stoi(v[1]);
            p2 = stoi(v[2]);
            p3 = stoi(v[3]);
            p4 = stoi(v[1]);
            p5 = stoi(v[2]);
            p6 = stoi(v[3]);
            vertex a, b, c;
            a.x = p1;
            a.y = p2;
            b.x = p3;
            b.y = p4;
            c.x = p5;
            c.y = p6;
            drawTriangle(a, b, c);
        }

    } while(flag == true);

}