编写我的第一个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>";
}
`
答案 0 :(得分: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)) {
// ...
}