Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

sort data using button

Codes here !

Moderators: macek, egami, gesf

sort data using button

Postby kanonyqkaskdn » Fri Apr 12, 2013 7:05 am

I tried to create a button which the function is to sort data either descending or ascending. However, I don't have idea how to do it

I did some research in internet, but none of them give the answer.

anyone know how to do it??? :help:

this is my code

Code: Select all
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");

//connect to database
//select the database
mysql_select_db("fak_databases");

//submit button
if($_POST['formSubmit'] == "Submit")
{
   $country = $_POST['country'];
}

//query the database
if($country == 'US') { 
    // query to get all US records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'"); 

elseif($country == 'NZ') { 
    // query to get all AUD records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'");
}elseif($country == 'JP') { 
    // query to get all AUD records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'"); 
} else { 
    // query to get all records 
    $query = mysql_query("SELECT * FROM auip_wipo_sample");

//fetch the result
Print "<table border cellpadding=3>";
//ascending descending button
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";

while($row = mysql_fetch_array($query))
{
   
   Print "<tr>";
   Print "<td>".$row['invention_title'] . "</td>";
   Print "<td>".$row['invention-title'] . " </td></tr>";
}
   //sorting the data, I got from internet but doesn't work
   if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
   {
       $query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title ASC";

   }else{

      $query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title DESC";
   }

Print "</table>";
?>
kanonyqkaskdn
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon Apr 08, 2013 6:01 am

Re: sort data using button

Postby egami » Tue Apr 16, 2013 1:35 pm

This is best done with JQuery and is slightly complicated. You can do it with a Session or Post variable, just do a check at the top of your script to see if the variable is set, and use that to sort your table.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron