使PHP函数相互协作


Making PHP Functions work with one another

我正在构建一个网页,使用户可以查看他们选择的某个产品的报告。步骤如下:

User chooses interface(Beta或Cloud)->User Chooses Product(产品基于所选界面)->相应显示信息。

显示信息的页面如下所示:

HTML

<div class="roundiv" >
        <h4>*Interface: </h4>
        <select class="form-control " style='width:250px;' onclick="INSERT SOMETHING HERE" >
            <option value="-1"  >Select Interface </option>
            <?php
                config::selectInterface($cao);
            ?>
        </select>
        <h4>
            *Product:
        </h4>
        <select class="form-control" style="width:250px;">
            <option value="-1" >Select Product</option>
            <?php
                config::selectIntProduct($cao);
            ?>
        </select>
        <button class="btn btn-success " value="Submit" onclick="" style="margin-left: auto; display:block;"  > Submit </button>
        <!-------------INFO WILL BE DISPLAYED HERE------------->
    </div>

我已经开始在config类中编写函数,并希望继续使用这种方法。

这是我在配置类中写的函数:

功能1

public static function selectInterface(CGenDb $cao) {
    $sel_interface_options = new CGenRS("select type_desc, type_id from lu_type where type_status = 't' and type_group = 'cloud' and type_sub_group = 'db'", $cao);
    $sel_interface_options->first();
    if ($sel_interface_options->rowcount()) {
        while (!$sel_interface_options->eof()) {
            echo "<option value='" . $sel_interface_options->valueof('type_id') . "'>" . $sel_interface_options->valueof('type_desc') . "</option>";
            $sel_interface_options->next();
        }
    }
    $interface_bool = True;
    return $interface_bool;
}

selectIntProducts看起来像这样:

功能2

public static function selectIntProduct(CGendb $cao,$selected_interface){
    $selected_interface=$cao_interface= "The interface ID"
    $sel_product_options = new CGenRS("select prod_name, prod_id from lu_products where prod_active = 't'", $cao_interface);
    $sel_product_options->first();
            if ($sel_product_options->rowcount()) {
                while (!$sel_product_options->eof()) {
                    echo "<option value='" . $sel_product_options->valueof('prod_id') . "'>" . $sel_product_options->valueof('prod_name') . "</option>";
                    $sel_product_options->next();
                }
            }
}

这就是我需要帮助的地方:

  1. 仅当选择了界面时,我才显示产品选择器。(我的想法是从函数1返回$interface_bool,测试它是否为真,如果为真,则显示下一部分。)

  2. 在函数1中选择的值应该是函数2 中的$selected_interface

我怎样才能做到这一点?

只有在重新加载页面并将所选接口发送回php时,才能使用php实现。

更好更方便的方法是使用javascript来填充选择,为此我建议使用knockout.js。

对于php,我建议这样做:

<div class="roundiv" >
    <h4>*Interface: </h4>
    <select class="form-control " style='width:250px;' onchange="document.location.href='https://url-to-same-page&interface=' + this.options[this.selectedIndex].value" >
        <option value="-1"  >Select Interface </option>
        <?php
            config::selectInterface($cao, $_GET['interface']);
        ?>
    </select>
<?php if (!empty($_GET['interface'])): ?>
    <h4>
        *Product:
    </h4>
    <select class="form-control" style="width:250px;">
        <option value="-1" >Select Product</option>
        <?php
            config::selectIntProduct($cao, $_GET['interface']);
        ?>
    </select>
    <button class="btn btn-success " value="Submit" onclick="" style="margin-left: auto; display:block;"  > Submit </button>
    <!-------------INFO WILL BE DISPLAYED HERE------------->
<?php endif; ?>
</div>

并更新selectInterface以包括所选接口:

public static function selectInterface(CGenDb $cao, $selected_interface) {
    // ... code
    while (!$sel_interface_options->eof()) {
        echo "<option value='" . $sel_interface_options->valueof('type_id') . "' "
             . ($selected_interface == $sel_interface_options->valueof('type_id') ? 'selected="selected"' : '')
             . ">" . $sel_interface_options->valueof('type_desc') . "</option>";
        $sel_interface_options->next();
    }
    // .. code continues
}