本篇內(nèi)容主要講解“PHP如何實現(xiàn)排序功能”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PHP如何實現(xiàn)排序功能”吧!
堯都網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(chuàng)新互聯(lián)。
-- phpMyAdmin SQL Dump -- version 4.5.1 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: 2022-03-17 17:19:09 -- 服務器版本: 10.1.13-MariaDB -- PHP Version: 5.6.21 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `a` -- -- -------------------------------------------------------- -- -- 表的結構 `search` -- CREATE TABLE `search` ( `id` int(11) NOT NULL DEFAULT '0', `content` text COLLATE utf8_vietnamese_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_vietnamese_ci; -- -- 轉(zhuǎn)存表中的數(shù)據(jù) `search` -- INSERT INTO `search` (`id`, `content`) VALUES (666, 'cyg'), (2, 'liwen'), (555, 'liwen&cyg'); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
核心問題:
1.怎么鏈接數(shù)據(jù)庫呢?
$link=MySQLi_connect('localhost','root','','a');
解析:鏈接數(shù)據(jù)庫,在自己的電腦本地地址上localhost。數(shù)據(jù)庫軟件用戶名:root.密碼"", 數(shù)據(jù)庫名:a
2.怎么設置鏈接的數(shù)據(jù)庫的字符編碼呢?
mysqli_set_charset($link,'utf8');
設置這種utf8編碼,不至于有漢字亂碼。
3.怎么運行php中的sql呢?
mysqli_query($link,$sql);
解析:第一個參數(shù)是數(shù)據(jù)庫鏈接賦值的變量。第二個參數(shù)是sql語句變量
4.怎么在插入語句中寫變量呢?
$sql = "INSERT INTO search(id,content) VALUES ('{$id}','{$content}')";
解析:按照這種格式來就行了
5.排序的sql語句,升序怎么寫?從小到大的是升序。越來越大
$sql = "SELECT id,content FROM search ORDER BY id";
6.從大到小的降序sql怎么寫?越來越小
$sql = "SELECT id,content FROM search ORDER BY id desc";
7.mysqli_query遍歷出來的數(shù)據(jù)要轉(zhuǎn)化為數(shù)組才能運行.
$row=mysqli_fetch_array($result)
解析:因為foreach不支持mysqli_query數(shù)據(jù)直接輸出
代碼如下(示例):
<?php $link=mysqli_connect('localhost','root','','a'); //然后是指定php鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); $sql="select * from search"; $result=mysqli_query($link,$sql);//運行sql ?> <!--顯示的效果--> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table border="1" cellpadding="5"> <tr> <td>id</td> <td>標題</td> <td>內(nèi)容</td> <?php while ($row=mysqli_fetch_array($result)) {//把對象變成數(shù)組輸出,不然會報錯哦 ?> <tr> <td><?=$row['id'];?></td> <td><?=$row['content'];?></td> </tr> <?php } ?> <td><a href="create.php">創(chuàng)建才能排序哦</a></td> <td><a href="asc.php">升序</a></td><!--從小到大--> <td><a href="desc.php">降序</a></td><!--從大到小--> </tr> </table> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="create.php" method="POST"> <input type="text" name="id"> <input type="text" name="content"> <input type="submit" value="提交"> </form> </body> </html> <?php if(!$_POST['content']||!$_POST['id']) { exit(); } $content=$_POST['content']; $id=$_POST['id']; $link=mysqli_connect('localhost','root','','a'); //然后是指定php鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); $sql = "INSERT INTO search(id,content) VALUES ('{$id}','{$content}')"; $result=mysqli_query($link,$sql); echo "<script>alert('創(chuàng)建成功');</script>"; ?> <button><a href="cyg.php">返回</a></button>
<?php $link=mysqli_connect('localhost','root','','a'); //然后是指定php鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); $sql = "SELECT id,content FROM search ORDER BY id"; $result=mysqli_query($link,$sql); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table border="1" cellpadding="5"> <tr> <td>id</td> <td>標題</td> <td>內(nèi)容</td> <?php while ($row=mysqli_fetch_array($result)) {//把對象編程數(shù)組輸出,不然會報錯哦 ?> <tr> <td><?=$row['id'];?></td> <td><?=$row['content'];?></td> </tr> <?php } ?> <td><a href="create.php">創(chuàng)建才能排序哦</a></td> <td><a href="asc.php">升序</a></td><!--從小到大--> <td><a href="desc.php">降序</a></td><!--從大到小--> </tr> </table> </body> </html>
<?php $link=mysqli_connect('localhost','root','','a'); //然后是指定php鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); $sql = "SELECT id,content FROM search ORDER BY id desc"; $result=mysqli_query($link,$sql); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table border="1" cellpadding="5"> <tr> <td>id</td> <td>標題</td> <td>內(nèi)容</td> <?php while ($row=mysqli_fetch_array($result)) {//把對象編程數(shù)組輸出,不然會報錯哦 ?> <tr> <td><?=$row['id'];?></td> <td><?=$row['content'];?></td> </tr> <?php } ?> <td><a href="create.php">創(chuàng)建才能排序哦</a></td> <td><a href="asc.php">升序</a></td><!--從小到大--> <td><a href="desc.php">降序</a></td><!--從大到小--> </tr> </table> </body> </html>
到此,相信大家對“PHP如何實現(xiàn)排序功能”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
網(wǎng)頁題目:PHP如何實現(xiàn)排序功能
文章網(wǎng)址:http://chinadenli.net/article22/ppiccc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、微信小程序、外貿(mào)網(wǎng)站建設、網(wǎng)站設計公司、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)