Incredinot Posted December 1, 2009 Share Posted December 1, 2009 Hi.. How can i make my headers "link" so (when clicked) they order desc/asc? <?php global $user; mysql_connect("xxxxx", "xxxxx", "xxxxx") or die(mysql_error()); mysql_select_db("xxxxx") or die(mysql_error()); ?> <table width="186" border="0" class="class"> <tr> <td width="41" valign="top"><strong>ID</strong></td> <td width="135" valign="top"><strong>Title</strong></td> </tr> <?php $result = mysql_query("SELECT * FROM node WHERE uid = '$user->uid' AND type ='something' ") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { $nid = $row['nid']; $result2 = mysql_query("SELECT * FROM content WHERE nid = '$nid' ") or die(mysql_error()); while($row2 = mysql_fetch_array( $result2 )){ ?> <tr> <td valign="top"><?php echo "ID" . $row['nid']; // Show the contents ID ?></td> <td valign="top"><?php echo $row2['title']; //Show the contents title ?></td> <?php }}?> </table> And can i change something in my code to make it smaller - more readalbe? Quote Link to comment https://forums.phpfreaks.com/topic/183571-order-by-maybe-better-coding/ Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 Do you mean order the result of the sql query. If so you can use the ORDER BY claus i.e SELECT * FROM tablename ORDER BY fieldname DESC Quote Link to comment https://forums.phpfreaks.com/topic/183571-order-by-maybe-better-coding/#findComment-968915 Share on other sites More sharing options...
Incredinot Posted December 1, 2009 Author Share Posted December 1, 2009 Do you mean order the result of the sql query. If so you can use the ORDER BY claus i.e SELECT * FROM tablename ORDER BY fieldname DESC Might be a stupid question, but either way - how can i do this in practice? I want it to be like.. If you click on "ID", then it order it by that.. So do i have to wrap a tags around "ID" and instert something og PHP - or how is it done? Quote Link to comment https://forums.phpfreaks.com/topic/183571-order-by-maybe-better-coding/#findComment-968919 Share on other sites More sharing options...
JonnoTheDev Posted December 1, 2009 Share Posted December 1, 2009 Along the lines of (make sure you change the filenames and sql queries appropriately) <td width="41" valign="top"><strong>ID</strong><a href="filename.php?order=asc">[asc]</a> <a href="filename.php?order=desc">[desc]</a></td> <?php // default to asc order unless clicked $order = ($_GET['order']) ? strtoupper($_GET['order']) : "ASC"; $result = mysql_query("SELECT * FROM node WHERE uid = '$user->uid' AND type ='something' ORDER BY fieldname ".$order); // ... rest of code ?> Quote Link to comment https://forums.phpfreaks.com/topic/183571-order-by-maybe-better-coding/#findComment-968950 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.