/    Sign up×
Community /Pin to ProfileBookmark

Sort Query Results

Hello,
I’m querying a Mysql database & displaying the results on a html page. This part is working fine.

I now want to sort the query results ascending by clicking on the column headers. I’m using a code which I got from another forum but I don’t know how to edit it. This code is working fine but it re-queries & sorts all the records in the whole database. I don’t want it to re-query & sort all the records in the whole database. I want it to sort only the query results from the 1st query.

Please help me to edit this code. Thank you.

[code=php]<?php
//the 1st query that comes from a html form
$Text_Box_17 = $_POST[‘Text_Box_17’];
$query=”SELECT * FROM land WHERE full_add LIKE ‘%$Text_Box_17%'”;

//the sorting part
$field = $_GET[‘sortby’];
if(isset($field))
{
$query = $query.’ ORDER BY ‘.$field.’ ASC’;
}

//the connection to the mysql database
mysql_connect (“$host”,”$username”,”$password”);
mysql_select_db($database) or die( “Where’s the database man?”);

$result=mysql_query($query);
$num=mysql_numrows($result);
//the table to display the results
?>

<table width=700 border=1 height=”0″ style=”border-collapse: collapse” bordercolor=”#111111″ cellpadding=”0″ cellspacing=”0″>
<tr bgcolor =”cyan”>
<td width=”80″ align=”center” bgcolor=”#CCFFFF”><b><font face=”arial” size=”2.5″><a href=”test_metro_agents.php?sortby=date”>date</a></font></strong></td>
<td width=”570″ align=”center” bgcolor=”#CCFFFF”><b><font face=”arial” size=”2.5″><a href=”test_metro_agents.php?sortby=full_add”>full_add</a></font></strong></td>
<td width=”50″ align=”center” bgcolor=”#CCFFFF”><b><font face=”arial” size=”2.5″><a href=”test_metro_agents.php?sortby=status”>status</a></font></strong></div></td>
</tr>

<tr>
<?php
while($row = mysql_fetch_assoc($result)){
?>
<tr>
<td align=”center” bgcolor=”#CCFFFF”><div align=”center”><font face=”arial” size=”2″><?php echo $row[‘date’] ?></font>&nbsp;</div></td>
<td align=”center” bgcolor=”#CCFFFF”><div align=”left”><font face=”arial” size=”2″><?php echo $row[‘full_add’] ?></font>&nbsp;</div></td>
<td align=”center” bgcolor=”#CCFFFF”><div align=”center”><font face=”arial” size=”2″><?php echo $row[‘status’] ?></font>&nbsp;</div></td>
<tr>
<?php
}
?>
</table>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@creedoFeb 21.2007 — 

Please help me to edit this code. Thank you.
[code=php]
<?php
//the 1st query that comes from a html form
$Text_Box_17 = $_POST['Text_Box_17'];
$query="SELECT * FROM land WHERE full_add LIKE '%$Text_Box_17%' ORDER BY '.$field.' ASC";

//the connection to the mysql database
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");

$result=mysql_query($query);

while($row = mysql_fetch_assoc($result)){
?>
<table>
<tr>
<td align="center" bgcolor="#CCFFFF"><div align="center"><font face="arial" size="2"><?php echo $row['date'] ?></font>&nbsp;</div></td>
<td align="center" bgcolor="#CCFFFF"><div align="left"><font face="arial" size="2"><?php echo $row['full_add'] ?></font>&nbsp;</div></td>
<td align="center" bgcolor="#CCFFFF"><div align="center"><font face="arial" size="2"><?php echo $row['status'] ?></font>&nbsp;</div></td>
<tr>
<?php
}
?>
</table>
[/QUOTE]

i dont know what you mean but this is a basic example of how to sort in ascending using php mysql.
Copy linkTweet thisAlerts:
@transfieldauthorFeb 21.2007 — this is a basic example of how to sort in ascending using php mysql[/QUOTE]Could you show me a complex example of how to sort in ascending using php mysql instead?
×

Success!

Help @transfield spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...