PHP development classification technology connection navigation style classification

In this section we will create a similar link navigation style

For example: Pictures>>Car Pictures>>Mercedes-Benz Pictures>>Mercedes-Benz C260 Pictures.

Creating databases and tables has been explained in the previous chapters, and will not be explained in detail in this section.

The idea is basically the same as the drop-down menu in the previous chapter:

Create the getCatePath function and execute SQL The statement queries the id. Through the queried id, different numbers of navigations can be displayed by entering different id values.

Use the custom function getCatePath to reverse the output using the krsort function.

<?php
function getCatePath($cid, &$result = array()) {
  global $link;
  $sql="SELECT * FROM class where id = $cid";
  $rs = mysqli_query($link,$sql);
  $row = mysqli_fetch_assoc($rs);
  if ($row) {
    $result[] = $row;
    getCatePath($row['pid'], $result);
  }
  krsort($result); //krsort对数组按键名逆向
  return $result;
}
?>

By creating the displayCatePath function, use a foreach loop to output and add the ">>" style

<?php
header("content-type:text/html;charset=utf8");

$link = mysqli_connect('localhost','username','password','test');
mysqli_set_charset($link, "utf8");
if (!$link) {
 die("连接失败:".mysqli_connect_error());
}

function getCatePath($cid, &$result = array()) {
    global $link;
    $sql="SELECT * FROM class where id = $cid";
    $rs = mysqli_query($link,$sql);
    $row = mysqli_fetch_assoc($rs);
    if ($row) {
        $result[] = $row;
        getCatePath($row['pid'], $result);
    }
 krsort($result); //krsort对数组按键名逆向
 return $result;
}
function displayCatePath($cid,$url='fen.php?cid=') { //fen.php为当前执行程序的PHP页面
    $res = getCatePath($cid);
    $str = '';
    foreach ($res as $key => $val) {
        $str.= "<a href={$url}{$val['id']}>>{$val['title']}</a>>";
     }
     return $str;
}
    echo displayCatePath(7); //输出当前的id为7
?>


Continuing Learning
||
<?php header("content-type:text/html;charset=utf8"); $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } function getCatePath($cid, &$result = array()) { global $link; $sql="select * from class where id = $cid"; $rs = mysqli_query($link,$sql); $row = mysqli_fetch_assoc($rs); if ($row) { $result[] = $row; getCatePath($row['pid'], $result); } krsort($result); //krsort对数组按键名逆向 return $result; } function displayCatePath($cid,$url='fen.php?cid=') { //fen.php为当前执行程序的PHP页面 $res = getCatePath($cid); $str = ''; foreach ($res as $key => $val) { $str.= "<a href={$url}{$val['id']}>>{$val['title']}</a>>"; } return $str; } echo displayCatePath(7); ?>
submitReset Code