adman4054 Posted June 8, 2013 Share Posted June 8, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/ Share on other sites More sharing options...
AbraCadaver Posted June 8, 2013 Share Posted June 8, 2013 Just add ORDER by position ASC to the end of the query. Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434853 Share on other sites More sharing options...
adman4054 Posted June 8, 2013 Author Share Posted June 8, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434854 Share on other sites More sharing options...
AbraCadaver Posted June 8, 2013 Share Posted June 8, 2013 Where is the $contact query? Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434855 Share on other sites More sharing options...
Eddy_J1 Posted June 8, 2013 Share Posted June 8, 2013 (edited) 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. Edited June 8, 2013 by Eddy_J1 Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434856 Share on other sites More sharing options...
adman4054 Posted June 8, 2013 Author Share Posted June 8, 2013 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434861 Share on other sites More sharing options...
AbraCadaver Posted June 8, 2013 Share Posted June 8, 2013 So add it to the end of the query in $qcontacts. Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434868 Share on other sites More sharing options...
adman4054 Posted June 8, 2013 Author Share Posted June 8, 2013 So add it to the end of the query in $qcontacts. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/278934-sort-by-ascending/#findComment-1434869 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.