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 or some source code which can be a references???
this is my code test.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mining</title>
</head>
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="country">
<option value="US">United States</option>
<option value="NZ">New Zealand</option>
<option value="JP">Japan</option>
</select>
</td>
</tr>
<td>
<input type="submit" name="formSubmit" value-"Submit">
</td>
</table>
</form>
</body>
</html>
showDB.php
<?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>";
?>
Change this:
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";
To
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value='Asc'></input></th></tr>";
And this
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
to
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']))
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mining</title>
</head>
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th colspan="3">test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td>
<select name="country">
<option value="US">United States</option>
<option value="NZ">New Zealand</option>
<option value="JP">Japan</option>
</select>
</td>
<td><input type="checkbox" name="asc" value="1" /> Ascending?</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" name="formSubmit" value-"Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Few things:
Example Code:
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
//select the database
mysql_select_db("fak_databases");
//submit button
if(!empty($_POST['formSubmit'])&&($_POST['formSubmit']=="Submit")&&(!empty($_POST['country']))){
$country = $_POST['country'];
} else {
$country = '';
}
if(!empty($_POST['asc']))
{
$append = " ORDER BY invention_title ASC";
}else{
$append = " ORDER BY invention_title DESC";
}
switch($country){
case 'US':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'$append";
break;
case 'NZ':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'$append";
break;
case 'JP':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'$append";
break;
default:
//all records
$query = "SELECT * FROM auip_wipo_sample$append";
}
//query the database
if($result = mysql_query($query)){
//fetch the result
print "<table border cellpadding=3>";
while($row = mysql_fetch_array($result))
{
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
print "</table>";
} else {
//For Testing
echo "Query Failed:<br />$query";
}
?>