0

i have the next code:

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

and in other php file

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

?>

The $col array contains :

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

The query is:

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

I want to sort the values from a mysql table using a loop, because i have various tables, so i won't be needed to change the php code. I tried various ways but none of them is working. Maybe you are smartest... thank you

6
  • Why don't you sort them in the query? Commented Jul 21, 2015 at 9:59
  • Show us the SQL code that $order_var is being used in please Commented Jul 21, 2015 at 10:00
  • Use "Order by" to sort the data in SQL. Commented Jul 21, 2015 at 10:00
  • 1
    @Code-Monk I think she is, this code is just generating the ORDER BY clause in a wierd and hopefully wonderful way Commented Jul 21, 2015 at 10:02
  • $sql_query = "SELECT * FROM $table1 ORDER BY $order_var"; Commented Jul 21, 2015 at 10:02

1 Answer 1

3

This process would seem a whole lot simpler if you changed your processing so that your HTML anchors had 2 parameters like this

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

Then building your query would be very simple

<?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']}";

I have of course missed out the $_GET sanitisation and validation checks, thats down to you.

Not quite sure how you decide on whether the sort should be Ascending or Descending but I hope this helps you simplify your processing. A simple process is so much easier to build, maintain and debug.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.