使用循环来排序值php

时间:2015-07-21 09:55:42

标签: php mysql

我有下一个代码:

<td><a href="index.php?order_var=<?php echo $col0_var; ?>"><?php echo $col[0]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col1_var; ?>"><?php echo $col[1]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col2_var; ?>"><?php echo $col[2]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col3_var; ?>"><?php echo $col[3]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col4_var; ?>"><?php echo $col[4]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col5_var; ?>"><?php echo $col[5]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col6_var; ?>"><?php echo $col[6]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col7_var; ?>"><?php echo $col[7]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col8_var; ?>"><?php echo $col[8]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col9_var; ?>"><?php echo $col[9]; ?></a></td>
<td><a href="index.php?order_var=<?php echo $col10_var; ?>"><?php echo $col[10]; ?></a></td>

和其他php文件

<?php
include 'connect.php';
//include 'countfields.php';
if ( isset ( $_GET['order_var'] ) )
{
        $order_var = $_GET['order_var'];
       for($i = 0 ; $i < 11; $i++) { 
        switch ( $order_var )
        {
        case 'col'.$i.'_asc':
                        $order_var = $col[$i]." ASC";
                        break;
                case 'col'.$i.'_dsc':
                        $order_var = $col[$i]." DESC";
                        break;

        }
        }
}
else
{
        $order_var = $col[0]." ASC";
}

$col0_var = "col0_asc";
$col1_var = "col1_asc";
$col2_var = "col2_asc";
$col3_var = "col3_asc";
$col4_var = "col4_asc";
$col5_var = "col5_asc";
$col6_var = "col6_asc";
$col7_var = "col7_asc";
$col8_var = "col8_asc";
$col9_var = "col9_asc";
$col10_var = "col10_asc";

if ( $order_var == $col[0]." ASC" )
{
        $col0_var = "col0_dsc";
}
else if ( $order_var == $col[0]." DESC" )
{
        $col0_var = "col0_asc";
}

if ( $order_var == $col[1]." ASC" )
{
        $col1_var = "col1_dsc";
}
else if ( $order_var == $col[1]." DESC" )
{
        $col1_var = "col1_asc";
}

if ( $order_var == $col[2]." ASC" )
{
        $col2_var = "col2_dsc";
}
else if ( $order_var == $col[2]." DESC" )
{
        $col2_var = "col2_asc";
}

 if ( $order_var == $col[3]." ASC" )
{
        $col3_var = "col3_dsc";
}
else if ( $order_var == $col[3]." DESC" )
{
        $col3_var = "col3_asc";
}
if ( $order_var == $col[4]." ASC" )
{
        $col4_var = "col4_dsc";
}
else if ( $order_var == $col[4]." DESC" )
{
        $col4_var = "col4_asc";
}

if ( $order_var == $col[5]." ASC" )
{
        $col5_var = "col5_dsc";
}
else if ( $order_var == $col[5]." DESC" )
{
        $col5_var = "col5_asc";
}

if ( $order_var == $col[6]." ASC" )
{
        $col6_var = "col6_dsc";
}
else if ( $order_var == $col[6]." DESC" )
{
        $col6_var = "col6_asc";
}

if ( $order_var == $col[7]." ASC" )
{
        $col7_var = "col7_dsc";
}
else if ( $order_var == $col[7]." DESC" )
{
        $col7_var = "col7_asc";
}

if ( $order_var == $col[8]." ASC" )
{
        $col8_var = "col8_dsc";
}
else if ( $order_var == $col[8]." DESC" )
{
        $col8_var = "col8_asc";
}

if ( $order_var == $col[9]." ASC" )
{
        $col9_var = "col9_dsc";
}
else if ( $order_var == $col[9]." DESC" )
{
        $col9_var = "col9_asc";
}

if ( $order_var == $col[10]." ASC" )
{
        $col10_var = "col10_dsc";
}
else if ( $order_var == $col[10]." DESC" )
{
        $col10_var = "col10_asc";
}

?>

$col数组包含:

array('IDClient',
      'DenClient',
      'DataContract',
      'TipAbonament',
      'CUI',
      'AdresaClient',‌​
      'PersContact',
      'TelefonClient',
      'EmailClient',
      'BancaClient',
      'ContBancarClient');

查询是:

$sql_query = "SELECT * FROM $table1 ORDER BY $order_var";

我想使用循环对mysql表中的值进行排序,因为我有各种表,因此我不需要更改php代码。我试过各种各样的方法,但没有一个是有效的。也许你是最聪明的...谢谢你

1 个答案:

答案 0 :(得分:3)

如果你改变你的处理方式,这个过程似乎要简单得多,这样你的HTML锚点就有2个这样的参数

<td>
  <a href="index.php?var=<?php echo $col[0];?>&order=desc"><?php echo $col[0];?></a>
  <a href="index.php?var=<?php echo $col[1];?>&order=desc"><?php echo $col[1];?></a>

  ...
</td>

然后构建您的查询非常简单

<?php
   include 'connect.php';
   //include 'countfields.php';
   if ( !isset($_GET['var']) || !isset($_GET['order']) )
   {
       // throw errors
   }

   $sql = "SELECT * FROM $table1 
           ORDER BY {$_GET['var']} {$_GET['order']}";

我当然错过了$ _GET卫生和验证检查,这些都归你所有。

您不确定如何确定排序应该是升序还是降序但我希望这有助于您简化处理。一个简单的过程更容易构建,维护和调试。