0

I have got the following, but in the results is a long string, contained within that string is a server name which I am wanting to sort the results by, is this possible?

    <?php
$dbQuery = mysql_query("SELECT * FROM opencall where cust_id = 'user.name@jpress' and status < 6  order by fixbyx asc") or die(mysql_error());//find call ref of open call with correct id
    while ($PRTGdbresults = mysql_fetch_array($dbQuery)){
    $SplitText = explode("\n", $probtext); //split the string by line break
    echo'<div class="row">';
    echo'<div class="inf_div" title="Current Server">';
            echo $SplitText[1]; //this is the server name I wish to sort by
        echo'</div></div>';
 }
?>

1 Answer 1

1

You can define your own function for sorting and then use this function with the usort sorting function.

Using the code you gave I will simply compare the strings of the server names and sort them in alphabetical order. Here is the code;

<?php
    $dbQuery = mysql_query("SELECT * FROM opencall where cust_id = 'user.name@jpress' and status < 6  order by fixbyx asc") or die(mysql_error());
    $results = array();

    while ($PRTGdbresults = mysql_fetch_array($dbQuery)){
        array_push($results,$probtext);
    }

    usort($results, "sortProbtext");

    foreach($results as $key => $probtext){
        $SplitText = explode("\n", $probtext);
        echo'<div class="row">';
        echo'<div class="inf_div" title="Current Server">';
        echo $SplitText[1];
        echo'</div></div>';   
    }

    function sortProbtext($a, $b){
       $SplitTextA = explode("\n", $a);
       $SplitTextB = explode("\n", $b);
       if ($SplitTextA[1]  == $SplitTextB[1] ) {
          return 0;
       }
       return ($SplitTextA[1]  < $SplitTextB[1] ) ? -1 : 1;
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.