sort data using button

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
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>";
?>

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Fri Apr 12, 2013 1:38 pm

you have a response in the other copy of this thread

User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Sat Apr 13, 2013 12:38 am

are you posting asc_sort from your form?
in the below condition if should go to else part and execute the query.

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

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest