The $order value is there only to say what SQL Query to run when user clicks on a button, the program prints a table with values and it has a button to select everything by alphabetic order that's when I am passing the value to $order.
My question is to find out why the code doesn't work because of the first foreach loop on this line
Your results do not come out in the order you expect because you are doing multiple queries in arbitrary order. You are doing one query for each value in %expal.
It is not the same thing to do this:
SELECT * FROM foo WHERE expal = 5 ORDER BY expal;
SELECT * FROM foo WHERE expal = 3 ORDER BY expal;
SELECT * FROM foo WHERE expal = 2 ORDER BY expal;
SELECT * FROM foo WHERE expal = 4 ORDER BY expal;
SELECT * FROM foo WHERE expal = 1 ORDER BY expal;