PHP infinite cl...LOGIN

PHP infinite classification navigation LINK style

Implementation schematic

面包屑.png

Navigation LINK style:

<?php
include('conn.php');
function getCatePath($cid, &$result = array()) {
   $sql = "SELECT * FROM deepcate WHERE id=$cid";
   $rs = mysql_query($sql);
   $row = mysql_fetch_assoc($rs);
   if ($row) {
       $result[] = $row;
       getCatePath($row['pid'], $result);
   }
   krsort($result); //krsort对数组按键名逆向
   return $result;
}
?>

Code explanation:

Same as drop-down style, create getCatePath function, Execute the sql statement to query the id, and pay the obtained value to $rs. Use mysql_fetch_assoc to obtain the array, and call its own getCatePath to query the pid and its own id.

Return $result and sort the array using the reverse method.

Then continue to beautify and encapsulate.

<?php
include('conn.php');
function getCatePath($cid, &$result = array()) {
    $sql = "SELECT * FROM deepcate WHERE id=$cid";
    $rs = mysql_query($sql);
    $row = mysql_fetch_assoc($rs);
    if ($row) {
        $result[] = $row;
        getCatePath($row['pid'], $result);
    }
    krsort($result); //krsort对数组按键名逆向
    return $result;
}
function displayCatePath($cid,$url='cate.php?cid=') {
    $res = getCatePath($cid);
    $str = '';
    foreach ($res as $key => $val) {
        $str.= "<a href={$url}{$val['id']}>{$val['catename']}</a>>";
    }
    return $str;
}
echo displayCatePath(10);
?>

In this way, the LINK style of infinite classification is completed.


Difficulties in this chapter

1. The link style starts from the parent node and searches downward for its descendant nodes to form a tree shape. The link style determines the current node. The pid is equal to the id of the previous node.

<?php include('conn.php'); function getCatePath($cid, &$result = array()) { $sql = "SELECT * FROM deepcate WHERE id=$cid"; $rs = mysql_query($sql); $row = mysql_fetch_assoc($rs); if ($row) { $result[] = $row; getCatePath($row['pid'], $result); } krsort($result); //krsort对数组按键名逆向 return $result; } function displayCatePath($cid,$url='cate.php?cid=') { $res = getCatePath($cid); $str = ''; foreach ($res as $key => $val) { $str.= "<a href={$url}{$val['id']}>{$val['catename']}</a>>"; } return $str; } echo displayCatePath(10); ?>
submitReset Code
ChapterCourseware