Add Order By to sql_select statement

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 18, 2015   (RSS)

By nmsinc - May 17, 2015

I need to add a ORDER BY statement to the following and I'm having a problem finding the correct syntax. I do not want to add the ODER BY to the $where statement as the $where statement is added by an "included page" and it is very lengthy.

If I can sort the output in the 'sql_select' or the 'foreach' statement that would be great - I need to sort by 'last_name' - any help would be appreciated!

Thanks

nmsinc

       <?php $submissionRecords = mysql_select('submission', $where); ?>
                                                        
       Name:&nbsp;
       <select name="num">
       <?php foreach ($submissionRecords as $submissionRecord): ?>
       <option value = "<?php echo$submissionRecord['num'];?>">#<?php echo $submissionRecord['num']; ?> - <?php echo $submissionRecord['last_name']; ?>, <?php echo $submissionRecord['name']; ?></option>
       <?php endforeach ?>
       </select>

nmsinc

By Djulia - May 17, 2015 - edited: May 18, 2015

Hi,

Try this :

<?php $submissionRecords = mysql_select('submission', $where); ?>

Name:&nbsp;
<select name="num">
<?php 
    $lastName = array();
    foreach ($submissionRecords as $key => $row) {
        $lastName[$key] = $row['last_name'];
    }
    array_multisort($lastName, SORT_ASC, $submissionRecords); // or SORT_DESC
    foreach ($submissionRecords as $submissionRecord): ?>
<option value = "<?php echo$submissionRecord['num'];?>">#<?php echo $submissionRecord['num']; ?> - <?php echo $submissionRecord['last_name']; ?>, <?php echo $submissionRecord['name']; ?></option>
<?php endforeach ?>
</select>

Thanks!

Djulia