本文目錄一覽:
- 1、php 數據庫搜索和排序同時哪裡錯了,排序運行不了,搜索也不行
- 2、PHP實現的自定義數組排序函數與排序類示例
- 3、php和mysql排序問題
- 4、用PHP為數據中中的字段排序
- 5、php幾種排序算法實例詳解
- 6、php中使用mysqli創建數據庫的時候怎麼指定字符集和排序規則?
php 數據庫搜索和排序同時哪裡錯了,排序運行不了,搜索也不行
?php
$link=mysql_connect(“localhost”,”root”,”000000″);//鏈接服務器
if(!$link){die(“鏈接服務器失敗”.mysql_error());}//判斷服務器鏈接是否成功
$db=mysql_query(“use guest”);//鏈接數據庫
if(!$db){die(“數據庫不存在”);}//判斷數據庫是否存在
mysql_query(‘set names utf8’);//確認字符集為utf8(編碼格式)
$order=$_GET[‘order’];
if($order){
$x=” order by $order desc”;
}else{
$x=” order by G_ID asc”;
}
$ss=$_GET[‘ss’];
if($ss){
$where=” where G_UserName like ‘%”.$ss.”%’ or G_sex like ‘%”.$ss.”%'”;
}else{
$where=””;
}
$sql=”select * from g_users”.$where.$x; //準備查詢語句
$res=mysql_query($sql); //執行語句,獲取結果集
?
body
h1 align=”center”數據/h1
div align=”center” class=”cx”
form id=”form1″ name=”form1″ method=”get” action=”test2.php”
input type=”text” name=”ss” id=”ss” /
input type=”submit” name=”tj” id=”tj” value=”搜索” /
/form
/div
table width=”1300″
tbody
tr
th width=”60″ align=”center” scope=”col”
a href=”?order=G_UserNamess=?php echo $ss;?”G_ID/a/th
th width=”151″ align=”center”
a href=”?order=G_UserNamess=?php echo $ss;?”G_UserName /a/th
th width=”70″ align=”center” scope=”col”G_Sex/th
th width=”82″ align=”center” scope=”col”G_Face/th
th width=”196″ align=”center” scope=”col”G_Email/th
th width=”72″ align=”center” scope=”col”G_QQ/th
th width=”68″ align=”center” scope=”col”G_Url/th
th width=”107″ align=”center” scope=”col”G_Flower/th
th width=”124″ align=”center” scope=”col”G_Date/th
th width=”90″ align=”center” scope=”col”相關操作/th
/tr
?php
//遍歷結果集,收取每個用戶的詳細信息
while($fetch=mysql_fetch_array($res)){?
tr
td?php echo $fetch[0]?/td
td?php echo $fetch[1]?/td
td?php echo $fetch[5]?/td
tdimg src=”?php echo $fetch[‘G_Face’]?”/td
td?php echo $fetch[7]?/td
td?php echo $fetch[‘G_QQ’]?/td
tda href=”?php echo $fetch[‘G_Url’]?”?php echo $fetch[‘G_Url’]?/a/td
td?php echo $fetch[‘G_Flower’]?/td
td?php echo $fetch[‘G_Date’]?/td
td?php echo ‘刪除 重置’ ?/td
/tr ?php } ?
/tbody
/table
PHP實現的自定義數組排序函數與排序類示例
本文實例講述了PHP實現的自定義數組排序函數與排序類。分享給大家供大家參考,具體如下:
/*
*
二維數組自定義排序函數
*
uasort($arr,function_name)
*
**/
$arr
=
array(
array(‘a’=1,’b’=’c’),
array(‘a’=4,’b’=’a’),
array(‘a’=5,’b’=’g’),
array(‘a’=7,’b’=’f’),
array(‘a’=6,’b’=’e’)
);
function
compare_arr($x,$y){
if($x[‘b’]$y[‘b’]){
return
-1;
}else
if($x[‘b’]$y[‘b’]){
return
1;
}else{
return
0;
}
}
uasort($arr,’compare_arr’);
foreach($arr
as
$a){
echo
$a[‘a’].’=’.$a[‘b’].’br/’;
}
手冊里的自定義排序類:
class
multiSort
{
var
$key;
//key
in
your
array
//排序函數
參數依次是
數組
待排列索引
排序類型
function
run
($myarray,
$key_to_sort,
$type_of_sort
=
”)
{
$this-key
=
$key_to_sort;
if
($type_of_sort
==
‘desc’)
uasort($myarray,
array($this,
‘myreverse_compare’));
else
uasort($myarray,
array($this,
‘mycompare’));
return
$myarray;
}
//正序
function
mycompare($x,
$y)
{
if
(
$x[$this-key]
==
$y[$this-key]
)
return
0;
else
if
(
$x[$this-key]
$y[$this-key]
)
return
-1;
else
return
1;
}
//逆序
function
myreverse_compare($x,
$y)
{
if
(
$x[$this-key]
==
$y[$this-key]
)
return
0;
else
if
(
$x[$this-key]
$y[$this-key]
)
return
-1;
else
return
1;
}
}
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《php排序算法總結》、《php字符串(string)用法總結》、《PHP針對XML文件操作技巧總結》、《PHP錯誤與異常處理方法總結》、《PHP運算與運算符用法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
php和mysql排序問題
php方法:
可以查一下「冒泡排序」,可以實現
只需要把往前推的規則改一下即可
mysql方法:
可以添加一個新的字段,name_type 你在輸入數據的時候,姓張的name_type = 1 ,類似
姓王的name_type= 2 ,查詢的時候order by name_type asc 即可;
用PHP為數據中中的字段排序
樓上說的比較正確
?php
首先鏈接你的數據庫
sql=”select
*
from
test
order
by
t
desc
limit
0,100″
$ret=mysql_query($sql,$db);//$db為數據庫連接
$zone=1;
while($row=mysql_fetch_array($ret)){
echo
“名次:”.$zone.”,”;
echo
$row[‘m’];//用戶名
echo
$row[‘t’];//積分
echo
$row[‘u’];//序號
echo
“br/”;
}
?
php幾種排序算法實例詳解
四種排序算法的PHP實現:
1) 插入排序(Insertion Sort)的基本思想是:
每次將一個待排序的記錄,按其關鍵字大小插入到前面已經排好序的子文件中的適當位置,直到全部記錄插入完成為止。
2) 選擇排序(Selection Sort)的基本思想是:
每一趟從待排序的記錄中選出關鍵字最小的記錄,順序放在已排好序的子文件的最後,直到全部記錄排序完畢。
3) 冒泡排序的基本思想是:
兩兩比較待排序記錄的關鍵字,發現兩個記錄的次序相反時即進行交換,直到沒有反序的記錄為止。
4) 快速排序實質上和冒泡排序一樣,都是屬於交換排序的一種應用。所以基本思想和上面的冒泡排序是一樣的。
1. sort.php文件如下:
?php
class Sort {
private $arr = array();
private $sort = ‘insert’;
private $marker = ‘_sort’;
private $debug = TRUE;
/**
* 構造函數
*
* @param array 例如:
$config = array (
‘arr’ = array(22,3,41,18) , //需要排序的數組值
‘sort’ = ‘insert’, //可能值: insert, select, bubble, quick
‘debug’ = TRUE //可能值: TRUE, FALSE
)
*/
public function construct($config = array()) {
if ( count($config) 0) {
$this-_init($config);
}
}
/**
* 獲取排序結果
*/
public function display() {
return $this-arr;
}
/**
* 初始化
*
* @param array
* @return bool
*/
private function _init($config = array()) {
//參數判斷
if ( !is_array($config) OR count($config) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_init_param_invaild”);
}
return FALSE;
}
//初始化成員變量
foreach ($config as $key = $val) {
if ( isset($this-$key)) {
$this-$key = $val;
}
}
//調用相應的成員方法完成排序
$method = $this-sort . $this-marker;
if ( ! method_exists($this, $method)) {
if ($this-debug === TRUE) {
$this-_log(“sort_method_invaild”);
}
return FALSE;
}
if ( FALSE === ($this-arr = $this-$method($this-arr)))
return FALSE;
return TRUE;
}
/**
* 插入排序
*
* @param array
* @return bool
*/
private function insert_sort($arr) {
//參數判斷
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_array(insert)_invaild”);
}
return FALSE;
}
//具體實現
$count = count($arr);
for ($i = 1; $i $count; $i++) {
$tmp = $arr[$i];
for($j = $i-1; $j = 0; $j–) {
if($arr[$j] $tmp) {
$arr[$j+1] = $arr[$j];
$arr[$j] = $tmp;
}
}
}
return $arr;
}
/**
* 選擇排序
*
* @param array
* @return bool
*/
private function select_sort($arr) {
//參數判斷
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_array(select)_invaild”);
}
return FALSE;
}
//具體實現
$count = count($arr);
for ($i = 0; $i $count-1; $i++) {
$min = $i;
for ($j = $i+1; $j $count; $j++) {
if ($arr[$min] $arr[$j]) $min = $j;
}
if ($min != $i) {
$tmp = $arr[$min];
$arr[$min] = $arr[$i];
$arr[$i] = $tmp;
}
}
return $arr;
}
/**
* 冒泡排序
*
* @param array
* @return bool
*/
private function bubble_sort($arr) {
//參數判斷
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_array(bubble)_invaild”);
}
return FALSE;
}
//具體實現
$count = count($arr);
for ($i = 0; $i $count; $i++) {
for ($j = $count-1; $j $i; $j–) {
if ($arr[$j] $arr[$j-1]) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j-1];
$arr[$j-1] = $tmp;
}
}
}
return $arr;
}
/**
* 快速排序
* @by
* @param array
* @return bool
*/
private function quick_sort($arr) {
//具體實現
if (count($arr) = 1) return $arr;
$key = $arr[0];
$left_arr = array();
$right_arr = array();
for ($i = 1; $i count($arr); $i++){
if ($arr[$i] = $key)
$left_arr[] = $arr[$i];
else
$right_arr[] = $arr[$i];
}
$left_arr = $this-quick_sort($left_arr);
$right_arr = $this-quick_sort($right_arr);
return array_merge($left_arr, array($key), $right_arr);
}
/**
* 日誌記錄
*/
private function _log($msg) {
$msg = ‘date[‘ . date(‘Y-m-d H:i:s’) . ‘] ‘ . $msg . ‘\n’;
return @file_put_contents(‘sort_err.log’, $msg, FILE_APPEND);
}
}
/*End of file sort.php*/
/*Location htdocs/sort.php */
2. sort_demo.php文件如下:
?php
require_once(‘sort.php’);
$config = array (
‘arr’ = array(23, 22, 41, 18, 20, 12, 200303,2200,1192) ,
//需要排序的數組值
‘sort’ = ‘select’,
//可能值: insert, select, bubble, quick
‘debug’ = TRUE
//可能值: TRUE, FALSE
);
$sort = new Sort($config);
//var_dump($config[‘arr’]);
var_dump($sort-display());
/*End of php*/
php中使用mysqli創建數據庫的時候怎麼指定字符集和排序規則?
字符集很簡單,但是數據的排序需要通過SQL語句來協助完成,ORDER BY 語句,代碼如下:
// 假設你已經成功連接了數據庫($mysqli變量假設為連接的資源句柄)
// 通過對象方式設置字符編碼
$mysqli – set_charset(‘utf8’);
// 通過函數方式設置字符編碼
mysqli_set_charset($mysqli, ‘utf8’);
// 那麼接下來是數據排序的話,需要編寫一條SQL查詢語句(DESC 倒序排列 | ASC 正序排列)
$sql = “SELECT `字段` FROM `表名` WHERE TRUE ORDER BY `字段` DESC;”;
如果還有什麼問題,歡迎追問~
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/157655.html