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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.