-1

编写我的第一个 php 应用程序。我被困在如何添加循环或条件语句...我正在从 Oracle 数据库查询添加我的站点导航...主要和辅助导航...我在我的代码中注意到我需要一个if 或 while 语句来决定是否写入ul标签。当我在主导航下有辅助导航时,我只希望编写ul class = sub标签和后续的li列表。有什么建议可以为我指明正确的方向吗?提前致谢...

`

        $t1_query = "SELECT * FROM PLUS_NAV_T1 ORDER BY POSITION ASC";
        $t1_parse = oci_parse($sm_conn, $t1_query);
        oci_execute ($t1_parse);
        /* Add query error catch*/

        while ($t1_row = oci_fetch_array($t1_parse, OCI_ASSOC)) {
            echo "<li><a href=\"#\" title=\"\" class=\"exp\"><span>{$t1_row["T1_NAME"]}</span></a>";

                $t2_query = "SELECT * FROM PLUS_NAV_T2 WHERE T1_ID_FK = {$t1_row["ID_PK"]} ORDER BY POSITION ASC ";
                $t2_parse = oci_parse($sm_conn, $t2_query);
                oci_execute ($t2_parse);
                /* Add query error catch*/

                /*Need a conditional here so ul tag is only written when the primary nav has secondary nav*/
                    echo "<ul class=\"sub\">";
                    while ($t2_row = oci_fetch_array($t2_parse, OCI_ASSOC)) {
                        echo "<li><a href=\"#\" title=\"\" style=\"color\">{$t2_row["T2_NAME"]}</a></li>";
                    }
                    echo "</ul>";

            echo "</li>";
        }

`

4

1 回答 1

0

我不熟悉 Oracle 驱动程序,但您可能需要该oci_num_rows()功能。发出第二个查询后,您应该能够调用该函数,并检查它是否返回非零值:

if (oci_num_rows($t2_parse)) {
    // your code to output the UL
}

实际上,您可能还应该验证 oci_execute() 返回 true,所以可能类似于:

if (oci_execute($t2_parse) && oci_num_rows($t2_parse)) {
    // ...
}
于 2012-10-01T17:57:49.740 回答