Article classification sorting function
1, modify the categoryListHtml.php code as follows:
<?php <!--展示文章分类功能--> <form method="post" action="?action=order"> <table border="1" cellpadding="3" cellspacing="0" width="20%"> <tr bgcolor="skyblue"> <td align="center">排序</td> <td>分类名称</td> <td align="center">操作</td> </tr> <?php foreach ($category as $v):?> <tr> <td><input type="text" name="<?php echo $v['id'];?>" value="<?php echo $v['sort'];?>"></td> <td><?php echo $v['name'];?></td> <td><a href="#">删除</a>|<a href="">编辑</a> </td> </tr> <?php endforeach;?> </table> <div><input type="submit" value="保存排序"></div> </form>
2, modify the category code: use placeholders to prevent SQL injection Modify the value of the sort field of the database in the way
<?php elseif ($a="order"){ $sql="select id from cms_category"; $result=$db->fetchAll($sql); $data=array(); foreach ($result as $v){ $data[]=array( 'id'=>intval($v['id']), 'sort'=>isset($_POST[$v['id']])?intval($_POST[$v['id']]):0 ); } $sql="update cms_category set sort=:sort where id=:id"; $db->data($data)->query($sql,true); }
3, the display is as follows:
Database display: