当这些元素显示在菜单上时,我想反转它们的顺序:
function buscarSubCateg($categ_id) {
global $con;
$buscar_subcateg = "SELECT * FROM subcateg WHERE categ_id = '$categ_id'";
$run_subcateg = mysqli_query($con, $buscar_subcateg);
$i=0;
while ($row_subcateg = mysqli_fetch_array($run_subcateg)) {
$subcateg_id = $row_subcateg['subcateg_id'];
$subcateg_name = $row_subcateg['subcateg_name'];
echo '<div class="sectionSub"><a class="linkSubCatergoria" href="#">'.$subcateg_name.'</a></div>'.buscarTipos($subcateg_id);
$i++;
}
}
function buscarTipos($subcateg_id) {
global $con;
$buscar_tipo = "SELECT * FROM tipoprod WHERE subcateg_id = '$subcateg_id'";
$run_tipo = mysqli_query($con, $buscar_tipo);
while ($row_tipo = mysqli_fetch_array($run_tipo)) {
$tipo_id = $row_tipo['tipoprod_id'];
$tipo_name = $row_tipo['tipoprod_name'];
echo "<div class='sectionTipo'><a class='linkTipo' href='#'>$tipo_name</a></div>";
}
}
在此回显中:
echo '<div class="sectionSub"><a class="linkSubCatergoria" href="#">'.$subcateg_name.'</a></div>'.buscarTipos($subcateg_id);`
我希望函数buscarTipos()
的结果显示在$subcateg_name
之后。现在的做法与此相反,首先显示buscarTipos()
的结果,然后显示$subcateg_name
。
谢谢。
答案 0 :(得分:0)
用以下代码替换当前代码:
function buscarSubCateg($categ_id) {
global $con;
$buscar_subcateg = "SELECT * FROM subcateg WHERE categ_id = '$categ_id'";
$run_subcateg = mysqli_query($con, $buscar_subcateg);
$i=0;
while ($row_subcateg = mysqli_fetch_array($run_subcateg)) {
$subcateg_id = $row_subcateg['subcateg_id'];
$subcateg_name = $row_subcateg['subcateg_name'];
$tip = buscarTipos($subcateg_id);
echo $tip;
echo '<div class="sectionSub"><a class="linkSubCatergoria" href="#">'.$subcateg_name.'</a></div>';
$i++;
}
}
function buscarTipos($subcateg_id) {
global $con;
$buscar_tipo = "SELECT * FROM tipoprod WHERE subcateg_id = '$subcateg_id'";
$run_tipo = mysqli_query($con, $buscar_tipo);
$str = '';
while ($row_tipo = mysqli_fetch_array($run_tipo)) {
$tipo_id = $row_tipo['tipoprod_id'];
$tipo_name = $row_tipo['tipoprod_name'];
$str = $str."<div class='sectionTipo'><a class='linkTipo' href='#'>$tipo_name</a></div>";
}
return $str;
}
答案 1 :(得分:0)
尝试更改第34行
echo "<div class='sectionTipo'><a class='linkTipo' href='#'>$tipo_name</a></div>";
进入
return "<div class='sectionTipo'><a class='linkTipo' href='#'>$tipo_name</a></div>";
然后,调用函数将在回显sectionSub
之前回显sectionTipo
或将您的SQL更改为
$sql = 'select a.categ_id, a.subcateg_id, b.tipoprod_id, b.tipoprod_name
from subcateg as a, tipoprod as b where categ_id='$categ_id'
and a.subcateg_id = b.subcateg_id';
并完全失去辅助功能。这样一来,MySQL便可以在两个表之间进行联接,并且易于使用一个数量级。