Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数据排序异常-按固定数据id排序_Php_Mysql_Sorting_Sql Order By - Fatal编程技术网

Php 数据排序异常-按固定数据id排序

Php 数据排序异常-按固定数据id排序,php,mysql,sorting,sql-order-by,Php,Mysql,Sorting,Sql Order By,我有一个非常不寻常的排序问题。我必须按照预定义的位置安排项目,如我现在在数据库中的位置: sid - auto increment name - just something to distinguish varchar 100 sorting - this is where we define the exact position of the item 如果我们将sorting=2分配给任何产品,无论发生什么情况,它都将处于第二位。但它应该是由排序定义的 我无法使用排序描述的顺序排列项目

我有一个非常不寻常的排序问题。我必须按照预定义的位置安排项目,如我现在在数据库中的位置:

sid - auto increment
name - just something to distinguish varchar 100
sorting - this is where we define the exact position of the item 
如果我们将
sorting=2
分配给任何产品,无论发生什么情况,它都将处于第二位。但它应该是由排序定义的

我无法使用排序描述的顺序排列项目,因为大多数项目没有分配任何位置。由于某些具有赋值的条目具有
排序=0
,因此根据计划,排序值不等于零的所有项目应放在其他项目之前

这是当前代码:

 <?php 
  $conn=mysqli_connect("localhost","root","","work");
   $getthedetails=mysqli_query($conn,"select sid,sorting,name from sorting order by sorting asc");
  while($thelistis=mysqli_fetch_array($getthedetails)){ ?>
  <div style="width:100px;font-weight:bold;color:#000;font-size:19px;font-   family:arial;height:100px;padding:49px;background:silver;float:left;margin:10px;">
    <?=$thelistis['sorting']?>
   </div>
   <?php } ?>

理想情况下,我希望排序类似于1,0,0,4,5,0,0,8

我想要的是这样的东西。

试试下面的这个

$getthedetails=mysqli_query($conn,"select id,sorting,name from tbl_sorting order by sorting asc");
$arrayList = array();
while($thelistis=mysqli_fetch_array($getthedetails)){
    $arrayList[] = $thelistis;
}

foreach($arrayList as $key=>$list){
    if($list['sorting'] != 0){
        //Keep current element of array
        $temp = $arrayList[$key];
        //Make current element same as with the one to swap
        $arrayList[$key] = $arrayList[$list['sorting']-1];
        //Swap the kept one to its place
        $arrayList[$list['sorting']-1] = $temp;
    }
}


foreach($arrayList as $key=>$list){?>

<div style="width:100px;font-weight:bold;color:#000;font-size:19px;font-   family:arial;height:100px;padding:49px;background:silver;float:left;margin:10px;">
    <?php echo $list['sorting']?>
</div>
<?php } ?>
$getthedetails=mysqli_query($conn,“通过排序asc从tbl_排序顺序中选择id、排序、名称”);
$arrayList=array();
while($thelistis=mysqli\u fetch\u数组($getthedetails)){
$arrayList[]=$thelistis;
}
foreach($arraylistas$key=>$list){
如果($list['sorting']!=0){
//保持数组的当前元素
$temp=$arrayList[$key];
//使当前元素与要交换的元素相同
$arrayList[$key]=$arrayList[$list['sorting']-1];
//将保留的一个换到它的位置
$arrayList[$list['sorting']-1]=$temp;
}
}
foreach($arraylistas$key=>$list){?>
试试下面的方法

$getthedetails=mysqli_query($conn,"select id,sorting,name from tbl_sorting order by sorting asc");
$arrayList = array();
while($thelistis=mysqli_fetch_array($getthedetails)){
    $arrayList[] = $thelistis;
}

foreach($arrayList as $key=>$list){
    if($list['sorting'] != 0){
        //Keep current element of array
        $temp = $arrayList[$key];
        //Make current element same as with the one to swap
        $arrayList[$key] = $arrayList[$list['sorting']-1];
        //Swap the kept one to its place
        $arrayList[$list['sorting']-1] = $temp;
    }
}


foreach($arrayList as $key=>$list){?>

<div style="width:100px;font-weight:bold;color:#000;font-size:19px;font-   family:arial;height:100px;padding:49px;background:silver;float:left;margin:10px;">
    <?php echo $list['sorting']?>
</div>
<?php } ?>
$getthedetails=mysqli_query($conn,“通过排序asc从tbl_排序顺序中选择id、排序、名称”);
$arrayList=array();
while($thelistis=mysqli\u fetch\u数组($getthedetails)){
$arrayList[]=$thelistis;
}
foreach($arraylistas$key=>$list){
如果($list['sorting']!=0){
//保持数组的当前元素
$temp=$arrayList[$key];
//使当前元素与要交换的元素相同
$arrayList[$key]=$arrayList[$list['sorting']-1];
//将保留的一个换到它的位置
$arrayList[$list['sorting']-1]=$temp;
}
}
foreach($arraylistas$key=>$list){?>

试试
ORDER BY-sorting DESC
@TerisL它将不起作用,因为到目前为止我一直在使用它。它将不起作用,因为对于未排序的项,排序的值可以为0列和表都命名为“排序”?是的…但这不是任何问题我将更改表名这只是为了测试目的不知道是否可以通过查询,我将处理一些事情,并让您知道使用PHPtry
ORDER BY-sorting DESC
@TerisL它将不起作用,因为我现在使用它。它将不起作用,因为对于非排序,排序的值可以为0列和表都被命名为“排序”?是的…但这不是任何问题,我将更改表名这只是为了测试目的不知道是否可以通过查询,我将处理一些问题,并让您知道PHPHello的答案是正确的,但当我使用排序desc的顺序时,它失败了。我想要的是用预定义的值排列项,如果如果为空,则最后一次输入的值应排在第一位..您好,答案是正确的,但当我使用order by sorting desc时失败。我想要的是用预定义的值排列项目,如果该值为空,则最后一次输入的值应排在第一位。。