Here is the page for this study

Open the relevant files and define the entire Expert class

class Expert extends CI_Controller{}
Copy the code

The Expert class defines several parameters and describes which models it uses

function \_\_construct() {
    $this->whitelist = "index";
    parent::\_\_construct (  );
    $this->load->model ( 'category\_model' );
    $this->load->model ( "expert\_model"); }Copy the code

Then we look at the code in function index(), line by line

$navtitle = "Problem specialist";
$cid = intval ( $this->uri->segment ( 3))?$this->uri->segment ( 3 ) : 'all'; $status = null! == $this->uri->segment ( 4 ) ? $this->uri->segment ( 4 ) : 'all'; / / sorting
Copy the code

You can do one on the page

if ($cid! ='all') {
    $category = $this->category \[$cid\].// Get the classification information
    $navtitle = $category\ ['name'\]."Expert List";
    $cfield = 'cid' . $category\ ['grade'\].// Get the classification level}
Copy the code

The second line is a little hard to understand. If you check the category reference, you can see that the current category is a global variable from the controller.php file in the System/core directory.

private static $instance;
var $cache;
var $currentuid = array(a);var $setting = array(a);var $category = array(a);var $usergroup = array(a);var $whitelist;
var $time;var $ip;
Copy the code

Also, right-click $category to see quick references and you’ll find the following lines of code

$this->load->database ();$category = $this->category = $this->cache->load ( 'category'.'id'.'displayorder' );
Copy the code

Load () to load the id and displayOrder fields in the database table named “category”

Note: Before data access, the following functions are used to access data from the cache. If the data fails, it is automatically read and written to the cache

function fromcache($cachename.$cachetime = 3){}
Copy the code

$category = $category; $category = $category

$category = $this->category [$cid]; // Get the classification information
Copy the code

Expert defines $category as db.category. Id = cid for the entire database

$cid == 'all'
else {  
    $category\ ['name'\] = ' ';
    $category\ ['id'\] = 'all';
    $cfield = ' ';
    $category\ ['pid'\] = 0;
}
if ($cid! ='all') 
    $category = $this->category\_model->get ( $cid );
$sublist = $this->category\_model->list\_by\_cid\_pid ( $cid.$category\ ['pid'\]);// Get subcategories
Copy the code

This code contains the following functions:


t h i s > c a t e g o r y _ m o d e l > g e t ( this->category\_model->get (
cid );

This method also includes a method to retrieve the category cover image from the data table catagory via the cid value


t h i s > c a t e g o r y _ m o d e l > l i s t _ b y _ c i d _ p i d ( this->category\_model->list\_by\_cid\_pid (
Cid, $category [‘ pid ‘])

function list\_by\_cid\_pid($cid.$pid) {
    $cid=intval($cid);
    $pid=intval($pid);
    $sublist = array(a);// set cid, pid = 'all' to flat class
    if ($cid= ='all') {
        $cid = 0;
    }
    if ($pid= ='all') {
        $pid = 0;
    }
    $where=" and onlybackground! = 1";
    
    // Find the same class ID as pid in the table
    // make pid subclass of ID
    $query = $this->db->query ( "select \* from " . $this->db->dbprefix . "category where pid=$cid and isuseask=1 $where order by displayorder asc,id asc" );
    
    // Add cover thumbnails and large images to subcategories
    foreach ( $query->result\_array () as $category ) {
        $category\ ['image'\] = get\_cid\_dir ( $category\ ['id'\].'big' );
        $category\ ['bigimage'\] = get\_cid\_dir ( $category\ ['id'\].'big' );
        $sublist\ [\] =$category;
    }
    return $sublist; }Copy the code

We can access the relevant database

The hierarchical relationship between the categories is here

We find that there is no sub-classification under the default classification, but PHP under the computer classification. Combining with the data table, we can know that the PID attribute value of classification A is equal to the ID value of A certain classification B, so classification A is the sub-classification of classification B, and the other categories are level except the sub-classification

Then we go to see how the function displays sub-categories in the browsing interface, and we can better understand the source code

// Determine if the page is paid content
$orderwhere = ' ';
switch ($status) {
    case 'all' : / / all
        $orderwhere = ' ';
        break;
    case '1' : / / pay
        $orderwhere = ' and mypay>0 ';
        break;
    case '2' : / / free
        $orderwhere = " and mypay=0 ";
        break;
    default:
        $orderwhere = ' ';
        break;
}

$page = max ( 1, intval ( $this->uri->segment ( 5)));$pagesize = $this->setting \['list\_default'\].$startindex = ($page - 1) \ *$pagesize;
Copy the code

** page=max(1,intval(page = max ( 1, intval ( page=max(1,intval(this->uri->segment ( 5 ) ) ); $this-> segment (5); $this->uri->segment (5)

** pagesize=pagesize =pagesize =this->setting [‘ list_default ‘]; Setting, like $category above, is a defined global variable

Setting stores the database’s “setting” table, which calls the value of the field ‘list_default’, meaning that a page displays 15 lists by default

** startIndex =(startIndex =(startIndex =(page – 1) * $pagesize; ** The start page number of the page is set to the current page number minus 1, because the computer count starts at 0

// Long, the last statement of this page
$rownum = $cid= ='all' ? returnarraynum ( $this->db->query ( getwheresql ( 'user'." expert=1 " . $orderwhere , $this->db->dbprefix ) )->row\_array () ) : returnarraynum ( $this->db->query ( getwheresql ( 'user'." expert=1 " . $orderwhere . "and uid IN (SELECT uid FROM " . $this->db->dbprefix . "user\_category WHERE cid=$cid)" , $this->db->dbprefix ) )->row\_array () );
Copy the code

The following methods are called in this statement

About meaning:

Select * from user where expert = 1 where cid = ‘all’

Current CID <> ‘all

Expert = 1 and user_category = cid = current CID. Orderwhere is the number of free items in order

// Display expert list, get\_list get all expert user ids in user data table
// And judge whether he is authenticated, the last login time, the number of followers, the number of followers, the individual is good at classification
$expertlist = $this->expert\_model->get\_list ( 1.$startindex.$pagesize.$cid.$status );

// Through the page function to classify the ID number, whether to free re-create two pages
$departstr = page ( $rownum.$pagesize.$page."expert/default/$cid/$status" );

// Make the current page, each page can handle up to 15 data
$questionlist = $this->expert\_model->get\_solves ( 0.15 );

// Implement the code page include template ('expert');
Copy the code