Jump to content

Sort by ascending


adman4054

Recommended Posts

I'm trying to sort this using a field in the same table (contact) named "position" in asending order and I'm not sure where to add it. any help would be appreciated.

<?php
$num_rows = mysql_num_rows($contact);
if($num_rows != NULL)
{
?>
<br />
<p>
<strong>Key Personnel</strong><br />
<?php
}
    while ($personnel = mysql_fetch_assoc($contact)) {
         echo $personnel['firstName'] .' '.$personnel['lastName'];
         //if(!empty($personnel['position'])){ echo ', '.$personnel['position']; }
         if(!empty($personnel['title'])){ echo ', '.$personnel['title'];}
         /*if(!empty($personnel['secondTitle'])){ 
             $query = mysql_query("SELECT name FROM contact_titles WHERE id = ".$personnel['secondTitle']);
             while ($title = mysql_fetch_array($query)) {
                echo ', '.$title['name'];
             }
         }
         if(!empty($personnel['thirdTitle'])){ 
             $query = mysql_query("SELECT name FROM contact_departments WHERE id = ".$personnel['thirdTitle']);
             while ($title = mysql_fetch_array($query)) {
                echo ', '.$title['name'];
             }
         }*/
         echo " <br />";
    }
?>
</p>

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/278934-sort-by-ascending/
Share on other sites

Just add ORDER by position ASC to the end of the query.

Thank you, but I should have been clearer,

<?php
$num_rows = mysql_num_rows($contact);
if($num_rows != NULL)
{
?>
<br />
<p>
<strong>Key Personnel</strong><br />
<?php
}
    while ($personnel = mysql_fetch_assoc($contact)) {
         echo $personnel['firstName'] .' '.$personnel['lastName'];
         
         if(!empty($personnel['title'])){ echo ', '.$personnel['title'];}
        
         echo " <br />";
    }
?>
</p>

The queries are commented out, correction is above. Really new to this stuff, can you point me in the right direction? Thanks again

Link to comment
https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434854
Share on other sites

Have a look at asort, just run that on your array and it should sort it out :)

 

http://php.net/manual/en/function.asort.php

 

Edit: Except, what Abra said above, wheres the query because what he said before will sort it in the query, but if you want to sort it after fetching them then use asort.

Link to comment
https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434856
Share on other sites

Thanks for looking at it!

Where is the $contact query?

 $qcontacts = "SELECT * FROM contact WHERE branchID = '".$branch['id']."' ";
         echo '<br />';
         $contacts = mysql_query($qcontacts)or die(mysql_error());
         while ($contact1 = mysql_fetch_assoc($contacts)) {
             if(!empty($contact1['firstName']) && trim($contact1['firstName'])!=''){ echo "<strong>First Name: </strong>" . $contact1['firstName'] . " <br />"; }
             if(!empty($contact1['lastName']) && trim($contact1['lastName'])!=''){ echo "<strong>Last Name: </strong>" . $contact1['lastName'] . " <br />"; }
             if(!empty($contact1['suffix']) && trim($contact1['suffix'])!=''){ echo "<strong>Suffix: </strong>" . $contact1['suffix'] . " <br />"; }
             if(!empty($contact1['position']) && trim($contact1['position'])!=''){ echo "<strong>Position: </strong>" . $contact1['position'] . " <br />"; }
             if(!empty($contact1['title']) && trim($contact1['title'])!=''){ echo "<strong>Title(s): </strong>" . $contact1['title'] . " <br />"; }
             //echo '<br />';
         }
         //echo " <br />";
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434861
Share on other sites

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.