首页 > 编程知识 正文

php数据库排序,php选择排序代码

时间:2023-12-29 13:17:00 阅读:330451 作者:GQRL

本文目录一览:

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;";

如果还有什么问题,欢迎追问~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。