Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 895895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:38:03+00:00 2026-05-15T14:38:03+00:00

I have a query that I want to sort alphabetically, but the trick is

  • 0

I have a query that I want to sort alphabetically, but the trick is that I want the sorting to treat two columns equally. For instance, if the first row of first_col equals apple and the second row of second_col equals aardvark I want the value in the second row of second_col to be listed before the value in the first row of first_col. A value (not NULL or '') will always exist in every row of second_col, but the value in first_col can be ''. Hopefully I have explained this good enough. I don’t care if I have to use MySQL or PHP for this, but once sorted, the array is read through and echoed into an HTML table. Any thoughts?

EDIT

This is what I have for code right now. In my MySQL query I need b_name and l_name to be equal. The column b_name does not always have a value. When I put the values into the table it is based on the existence of b_name. If b_name does not exist the f_name and l_name are combined to replace b_name.

                $query = "SELECT * FROM customers ORDER BY b_name, l_name";
                $result = mysql_query($query);
                mysql_close($link);

                $num = mysql_num_rows($result);                         

                for ($i = 0; $i < $num; $i++){

                    $row = mysql_fetch_array($result);

                    $class = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";

                    if($row[b_name]!=''){
                        echo "<tr class=".$class.">";

                            echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[c_id]."</a></td>";
                            echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[b_name]."</a></td>";
                            echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[phone]."</a></td>";

                        echo "</tr>";

                    }

                    else{
                        echo "<tr class=".$class.">";

                            echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[c_id]."</a></td>";
                            echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[f_name]." ".$row[l_name]."</a></td>";
                            echo "<td><a href=Edit_Customer.php?c_id=".$row[c_id].">".$row[phone]."</a></td>";

                        echo "</tr>";

                    }


                }

            ?>      

        </table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:38:04+00:00Added an answer on May 15, 2026 at 2:38 pm

    Thanks for all your help guys, but none of your answers allowed me to sort the data AND echo it into the HTML table correctly once sorted. A UNION might have worked, but I think my solution was faster as far as figuring it all out goes.

                    $query = "SELECT c_id, b_name, l_name, f_name, phone FROM customers";
                    $result = mysql_query($query);
                    mysql_close($link);
    
                    $num = mysql_num_rows($result);
    
                    for ($i = 0; $i < $num; $i++){
                        $row = mysql_fetch_array($result);
    
                        if($row[b_name]!=''){
                            $new_result[$i]['c_id'] = $row[c_id];
                            $new_result[$i]['c_name'] = $row[b_name];
                            $new_result[$i]['phone'] = $row[phone];
                        }
    
                        else{
                            $new_result[$i]['c_id'] = $row[c_id];
                            $new_result[$i]['c_name'] = $row[l_name].", ".$row[f_name];
                            $new_result[$i]['phone'] = $row[phone];                         
                        }
                    }                       
    
                    foreach ($new_result as $key => $row) {
                       $c_id[$key]  = $row['c_id'];
                       $c_name[$key] = $row['c_name'];
                       $phone[$key] = $row['phone'];
                    }
    
                    array_multisort($c_name, SORT_ASC, $c_id, SORT_ASC, $new_result);   
    
                    for ($i = 0; $i < $num; $i++){
    
                        $class = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
    
                        echo "<tr class=".$class.">";
    
                            echo "<td><a href=Edit_Customer.php?c_id=".$new_result[$i]['c_id'].">".$new_result[$i]['c_id']."</a></td>";
                            echo "<td><a href=Edit_Customer.php?c_id=".$new_result[$i]['c_id'].">".$new_result[$i]['c_name']."</a></td>";
                            echo "<td><a href=Edit_Customer.php?c_id=".$new_result[$i]['c_id'].">".$new_result[$i]['phone']."</a></td>";
    
                        echo "</tr>";                       
    
                    }
    
                ?>      
    
            </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that does what i want joining table but i need
I have a database that I want to sort but I dont really understand
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
i have a query that returns rows that i want, e.g. QuestionID QuestionTitle UpVotes
I have the following query that I want it to show Not Yet instead
I have a an query that I want to work on so I formatted
Hi I have a really complicated dynamic query that i want to use to
I have a query that returns one row. However, I want to invert the
I have the following query that runs in my Oracle database and I want
I want to add an extra field to my query that will have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.