php
$link=mysql_connect("localhost","root","000000");//链接服务器
if(!$link){die("链接服务器失败".mysql_error());}//判断服务器链接是否成功
$db=mysql_query("use?guest");//链接数据库
if(!$db){die("数据库不存在");}//判断数据库是否存在
$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."%'";
$where="";
$sql="select?*?from?g_users".$where.$x;?//准备查询语句
$res=mysql_query($sql);?//执行语句,获取结果集
body
h1?align="center"数据/h1
input?type="text"?name="ss"?id="ss"?/
input?type="submit"?name="tj"?id="tj"?value="搜索"?/
/form
tbody
tr
a?href="?order=G_UserNamess=?php?echo?$ss;?"G_ID/a/th
a?href="?order=G_UserNamess=?php?echo?$ss;?"G_UserName?/a/th
/tr
//遍历结果集,收取每个用户的详细信息?
while($fetch=mysql_fetch_array($res)){?
td?php?echo?$fetch[0]?/td
td?php?echo?$fetch[1]?/td
tdimg?src="?php?echo?$fetch['G_Face']?"/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实现的自定义数组排序函数与排序类.分享给大家供大家参考,具体如下:
/*
*
二维数组自定义排序函数
uasort($arr,function_name)
**/
$arr
=
array(
array('a'=1,'b'='c'),
);
function
compare_arr($x,$y){
if($x['b']$y['b']){
return
-1;
}else
0;
uasort($arr,'compare_arr');
foreach($arr
as
$a){
echo
$a['a'].'='.$a['b'].'br/';
手册里的自定义排序类:
class
multiSort
{
var
$key;
//key
in
your
array
//排序函数
参数依次是
数组
待排列索引
排序类型
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
'mycompare'));
$myarray;
//正序
mycompare($x,
$y)
(
$x[$this-key]
$y[$this-key]
)
//逆序
myreverse_compare($x,
更多关于PHP相关内容感兴趣的读者可查看本站专题:<
希望本文所述对大家PHP程序设计有所帮助.
php方法:
可以查一下"冒泡排序",可以实现
只需要把往前推的规则改一下即可
mysql方法:
可以添加一个新的字段,name_type 你在输入数据的时候,姓张的name_type = 1 ,类似
楼上说的比较正确
首先链接你的数据库
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)){
"名次:".$zone.",";
$row['m'];//用户名
$row['t'];//积分
$row['u'];//序号
"br/";
四种排序算法的PHP实现:
①.)?插入排序(Insertion?Sort)的基本思想是:?
每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止.
每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,直到全部记录排序完毕.
两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止.
①?sort.php文件如下:
class?Sort?{
private?$arr?=?array();?
private?$sort?=?'insert';
private?$marker?=?'_sort';
private?$debug?=?TRUE;
/**
*?构造函数
*?@param?array?例如:
$config?=?array?(
'sort'?=?'insert',?//可能值:?insert,?select,?bubble,?quick
'debug'?=?TRUE?//可能值:?TRUE,?FALSE
*/
public?function?construct($config?=?array())?{
if?(?count($config)?0)?{
$this-_init($config);
*?获取排序结果
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))?{
$this-_log("sort_method_invaild");
if?(?FALSE?===?($this-arr?=?$this-$method($this-arr)))
return?TRUE;
*?插入排序
*?
private?function?insert_sort($arr)?{
if?(?!?is_array($arr)?OR?count($arr)?==?0)?{
$this-_log("sort_array(insert)_invaild");
//具体实现
$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;
*?选择排序
private?function?select_sort($arr)?{
$this-_log("sort_array(select)_invaild");
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;
*?冒泡排序
private?function?bubble_sort($arr)?{
$this-_log("sort_array(bubble)_invaild");
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?
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];
$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?*/
require_once('sort.php');
//需要排序的数组值
'sort'?=?'select',
//可能值:?insert,?select,?bubble,?quick
'debug'?=?TRUE
//可能值:?TRUE,?FALSE
$sort?=?new?Sort($config);
//var_dump($config['arr']);
/*End?of?php*/
字符集很简单,但是数据的排序需要通过SQL语句来协助完成,ORDER BY 语句,代码如下:
//?假设你已经成功连接了数据库($mysqli变量假设为连接的资源句柄)
//?通过对象方式设置字符编码
//?通过函数方式设置字符编码
//?那么此时此刻呢是数据排序的话,需要编写一条SQL查询语句(DESC?倒序排列?|?ASC?正序排列)
$sql?=?"SELECT?+字段+?FROM?+表名+?WHERE?TRUE?ORDER?BY?+字段+?DESC;";
如果还有什么问题,欢迎追问~
以上就是土嘎嘎小编为大家整理的php数据库排序相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!