sstoveld Posted September 25, 2009 Share Posted September 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/ Share on other sites More sharing options...
mikesta707 Posted September 25, 2009 Share Posted September 25, 2009 you could make each a link with a get variable, and you order by that get variable. Quote Link to comment https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/#findComment-924530 Share on other sites More sharing options...
sstoveld Posted September 25, 2009 Author Share Posted September 25, 2009 you could make each a link with a get variable, and you order by that get variable. sorry could you elaborate a bit, im pretty new to php and dont quite understand Quote Link to comment https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/#findComment-924532 Share on other sites More sharing options...
sstoveld Posted September 30, 2009 Author Share Posted September 30, 2009 sorry to bump a post, but i dont quite understand how to do this Quote Link to comment https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/#findComment-927783 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 heres an example: SELECT * FROM table ORDER BY organization ASC SELECT * FROM table ORDER BY organization DESC Quote Link to comment https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/#findComment-927788 Share on other sites More sharing options...
sstoveld Posted September 30, 2009 Author Share Posted September 30, 2009 heres an example: SELECT * FROM table ORDER BY organization ASC SELECT * FROM table ORDER BY organization DESC right, but what im not getting is making my table headers into get variable links so the user can sort the table using them Quote Link to comment https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/#findComment-927789 Share on other sites More sharing options...
sstoveld Posted September 30, 2009 Author Share Posted September 30, 2009 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>'); } Quote Link to comment https://forums.phpfreaks.com/topic/175442-solved-sorting-values-in-a-table/#findComment-927893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.