An example of sorting function of infinite columns in ThinkPHP framework

Time:2021-3-4

In this paper, an example is given to show how to realize the sorting function of infinite columns in ThinkPHP framework. The details are as follows:

In the title, we didn’t explain whether it is TP5’s infinite ranking or TP3’s infinite ranking, just to let the new players understand that the implementation of these functions has nothing to do with the framework you use, whether you are TP5 or TP3, laravel or Yii framework. What we emphasize is the idea and the method to solve the problem. When I demonstrate, I have nothing to do because I am using TP3 That’s it.

The ranking of infinite columns is very simple. This time, I will share the solution with you in the form of blog.

Above:

The figure above shows our infinite level classification. We should pay attention to two fields, ID and sort. At present, the value of sort field is 50, which is the default value. Next, we take a screenshot of the data table structure

In the figure above, sort is used for sorting, and PID is used for infinite level classification

The key to realizing infinite level classification is how to write the sorting field. We get the whole code, but only one line is used

<form method="post" action="">
<table>
<thead>
<tr>
<th width="6%">ID</th>
<th width="6%">pid</th>
< th width = "8%" > sort < / th >
< th > column name
< th width = "16%" > operation < / th >
</tr>
</thead>
<tbody>
<volist name="cateRes">
<tr>
<td align="center">{$cate.id}</td>
<td align="center">{$cate.pid}</td>
<td align="center">
<input type="text" name="sort[{$cate.id}]" value="{$cate.sort}" /></td>
<td><?php echo str_repeat('-', $cate['level']*8);?>{$cate.cate_name}</td>
<td align="center">
<a href=""external nofollow" btn btn-primary btn-sm shiny">
<i> < / I > Edit
</a>
< a = "############################
<i> < / I > delete
</a>
</td>
</tr>
</volist>
<tr>
<td colspan="4">
< button type = "button" tooltip = "sort" style = "margin- left:225px;  width:50px; "> sort < / button >
</td>
</tr>
</tbody>
</table>
</form>

From the above code, we can see that the whole table is wrapped in form, because we need to submit the sorting field, so we need the form.

We realize the core code of unlimited column sorting


<input type="text" name="sort[{$cate.id}]" value="{$cate.sort}" />

In fact, we have assembled a sort [] array. The key of each element in the whole array is the ID of the current column, and the value is the sorted value of the current column. In this way, once we submit the array, we can modify the sort according to the ID

Full code:

public function lst(){
$cate=D('Cate');
if(IS_ Post) {// sort
$data=I('sort');
foreach ($data as $k => $v) {
$cate->where(array('id'=>$k))->save(['sort'=>$v]);
}
return;
}
$cateres = $cate - > catetree(); // infinite level classification tree
$this->assign([
'cateRes'=>$cateRes,
]);
$this->display('list');
}

For more information about ThinkPHP, interested readers can check out the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, CI (CodeIgniter) framework advanced course, Zend framework introduction course and PHP template technology summary.

I hope this article will be helpful to the design of PHP program based on ThinkPHP framework.