Jump to content

Order by? (Maybe better coding)


Incredinot

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/183571-order-by-maybe-better-coding/
Share on other sites

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?

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
?>

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.