Jump to content

[SOLVED] Sorting values in a table


sstoveld

Recommended Posts

hey guys, ive got a small issue with my project here. i have an assignment to create an enemy list (like nixon's enemy list) using a database, and display it on the page.

 

i have all that done, but i need to be able to let the user sort through the values by rank, first name, last name, organization, or comment.

 

i believe what i need to do it make the table headers a link that the user can click which will sort it ascending or descending, problem is, im not sure how to do that...

 

here's what i've got so far:

<?php
// Connect to the Database //
$dbhost = '*****';
$dbuser = '*****';
$dbpass = '*****';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

// Select the Database //
$dbname = '*****';
mysql_select_db($dbname);

// Query the Database to select the entries in the list //
$querylist = mysql_query('SELECT * FROM enemylist ORDER BY rank');

// Make an HTML table to hold the data for the list //
?>

<h2>Enemy List</h2>

<table>
<tr>
    	<td><strong>Rank</strong></td>
        <td><strong>First Name</strong></td>
        <td><strong>Last Name</strong></td>
        <td><strong>Organization</strong></td>
        <td><strong>Comment</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($querylist)) {
	echo('<tr><td>' .$row['rank'] . '</td><td>' . $row['fname'] . '</td><td>' . $row['lname'] . '</td><td>' . $row['organization'] . '</td><td>' . $row['comment'] . '</td></tr>');
}

?>
</table>

 

can anyone lend a hand? thanks

Link to comment
https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/
Share on other sites

ah i figured it out. if anyone else has the same type of problem, this might help:

<?php
// Sort //
$sort = $_GET['sort'];
if ($sort == NULL){
        $sort = "ranking";
}

?>

<h2>Enemy List</h2>

<table>
<tr>
    	<td><strong><a href="a3.php?sort=rank">Rank</a></a></strong></td>
        <td><strong><a href="a3.php?sort=fname">First Name</a></strong></td>
        <td><strong><a href="a3.php?sort=lname">Last Name</a></strong></td>
        <td><strong><a href="a3.php?sort=organization">Organization</a></strong></td>
        <td><strong><a href="a3.php?sort=comment">Comment</a></strong></td>
    </tr>

<?php

// Query the Database to select the entries in the list //
$querylist = @mysql_query('SELECT * FROM enemylist ORDER BY '.$sort.';');
if (!querylist) {
	exit ('<p>Error in query.'.mysql_error().'</p>');
}

 

Archived

This topic is now archived and is closed to further replies.

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