Jump to content

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>');
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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