Jump to content

Sorting PHP table


Bravat

Recommended Posts

Here is the code:

<table class="sortable">
<?php
    while($row = mysql_fetch_array($result))        
   {    $title = $row["Rim"];  
        $model = $row["model"];
        $dimenzija = $row["Dimenzija"];
        $ime = $row['name'];
        $cena = $row['price']; 
        $cena = number_format($cena, 2, ',', '.');
        $id = $row['product_id'];
                   
   if($i == 0)
          echo "<tr>";
          echo "<td>" ?><a href="index.php?route=product/product&product_id=<?php echo $id?>"><?php echo $model ?></a>
          <?php echo "<br>$dimenzija<br>$ime<br>"; ?>
          <span style="color: #900; font-weight: bold;"><?php echo $cena . "din"?> </span>
          <?php echo "</td>";
           if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       } }}
       if($i > 0)
{
   for($j=$i; $j<$max_columns;$j++) echo "<td> </td>";
   echo "</tr>";
}
$prev = $start - $per_page;
$next = $start + $per_page; 

  if (!($start<=0)){
echo "<a href='{$_SERVER['PHP_SELF']}&start=$prev'>Prethodna  strana </a>";
}
$page = 1;

for($x=0;$x<$record_count;$x=$x+$per_page){
 if($start != $x){
 		echo " <a href='{$_SERVER['PHP_SELF']}&start=$x'> $page </a> ";}
		else{
		echo " <a href='{$_SERVER['PHP_SELF']}&start=$x'> <b>$page</b> </a> ";}
	$page++;
 }

  if (!($start>=$record_count - $per_page)){
   echo "<a href='{$_SERVER['PHP_SELF']}?route=product/search&start=$next'> Sledeća strana</a>";


}
?>
             </table>

 

How can i create sorting button? Is it possible to do it without javascript (I am new to PHP and JS is still unknown to me)?

Link to comment
https://forums.phpfreaks.com/topic/226226-sorting-php-table/
Share on other sites

Well for some crazy reason it won't work  :confused:. I followed instruction but table remain inactive  :confused:

 

The easiest way to sort without js is to alter your sql query on click. Once you figure that out it's just a few simple js automations to do it without page reload.

 

example:

in your table headers give each heading an <a> like, <a href="#?sort=name">Name</a>

 

php:

<?php
    $sort = $_GET['sort'];
    $sql = "SELECT * FROM myTable SORT BY $sort ASC";
?>

 

That's the  basic idea behind it.

 

Hope this helped.

 

E

Link to comment
https://forums.phpfreaks.com/topic/226226-sorting-php-table/#findComment-1168065
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.