?
Solved

How to sort numbers in either MySQL or php

Posted on 2011-02-22
7
Medium Priority
?
305 Views
Last Modified: 2012-05-11
Having a series from 10 to 199, I'd like to order it so it comes out like
10, 11, 12 ,13 ..., 20, 21, 22, 23, ...,98, 99, 100,101 etc.
not
10, 100, 101, ..., 11, 110 ,111, ..., etc.
How can it be done?
0
Comment
Question by:lericson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:MrNed
ID: 34956551
PHP's sort function will do it, but you need to pass the SORT_NUMERIC argument, e.g:

sort($array, SORT_NUMERIC);

http://php.net/manual/en/function.sort.php
0
 

Author Comment

by:lericson
ID: 34956701
MrNed, thanks for your reply. I need more help. Let me give you my code snippet:
<?php
////error_reporting(E_ALL);
////ini_set('display_errors', '1');
    include "../../dbconfig/dbconfig.php";
    include "paging2.php";
    // Connect to SQL database
    $global_db = mysql_connect($host, $usr, $pwd);
    mysql_select_db($db, $global_db);
    $SQL = " SELECT * FROM matrikel WHERE Bryggplats > '0' AND Kategori = 'A' ORDER BY Bryggplats limit ".$start.", ".$limit; ;
$retid = mysql_query($SQL);
if (!$retid) { echo( mysql_error()); }
else {
//echo ("<Div align=\"center\"><TABLE width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >\n");
while ($row = mysql_fetch_array($retid)) {
        $fornamn = $row["Fornamn"];
        $efternamn = $row["Efternamn"];
        $bryggplats = $row["Bryggplats"];
        $telnr = $row["Tel_hem"];
        $mobilnr = $row["MobilNr"];
        $id = $row ["id"];
        $bgcolor = ( ++$counter & 1 ) ? 'E5EDF6' : 'FFFFFF';
        $plats = sort($bryggplats, SORT_NUMERIC);echo ("<tr>");
echo ("<td style=\"background-color: #$bgcolor\">&nbsp;</td>\n");
echo ("<td style=\"background-color: #$bgcolor\" align=\"left\"><b>$plats</b></td>\n");
echo ("<td style=\"background-color: #$bgcolor\" align=\"left\">$efternamn $fornamn</td>\n");
echo ("<td style=\"background-color: #$bgcolor\" align=\"left\">$telnr</font></td>\n");
echo ("<td style=\"background-color: #$bgcolor\" align=\"left\">$mobilnr</font></td>\n");
echo ("<td style=\"background-color: #$bgcolor\">&nbsp;</td>");
echo ("</tr>");
}

In the output I get no $plats. What am I doing wrong?
0
 
LVL 1

Expert Comment

by:DoCBReeD
ID: 34956811
What is your sql query line? just added:

ORDER BY <FIELDNAME> ASC

at the end of your query.  <fieldname> is the field you want to order by and ASC or DESC.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Accepted Solution

by:
DoCBReeD earned 2000 total points
ID: 34956833
I'm sorry, i didn't see the code above being yours..  change your query to:

$SQL = " SELECT * FROM matrikel WHERE Bryggplats > '0' AND Kategori = 'A' ORDER BY Bryggplats ASC limit ".$start.", ".$limit; ;
0
 
LVL 1

Expert Comment

by:DoCBReeD
ID: 34956893
Sorry for the extra posts..  hope this helps:

<?php
////error_reporting(E_ALL);
////ini_set('display_errors', '1');
include "../../dbconfig/dbconfig.php";
include "paging2.php";
// Connect to SQL database
$global_db = mysql_connect($host, $usr, $pwd);
mysql_select_db($db, $global_db);
$SQL = " SELECT * FROM matrikel WHERE Bryggplats > '0' AND Kategori = 'A' ORDER BY Bryggplats ASC limit ".$start.", ".$limit; ;
$retid = mysql_query($SQL);

if (!$retid) { 
    echo( mysql_error());
}else{
    //echo ("<Div align=\"center\"><TABLE width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >\n");
    while ($row = mysql_fetch_array($retid)) {
        $fornamn = $row["Fornamn"];
        $efternamn = $row["Efternamn"];
        $bryggplats = $row["Bryggplats"];
        $telnr = $row["Tel_hem"];
        $mobilnr = $row["MobilNr"];
        $id = $row ["id"];
        $bgcolor = ( ++$counter & 1 ) ? 'E5EDF6' : 'FFFFFF';

        echo ("<tr>");
        echo ("<td style=\"background-color: #$bgcolor\">&nbsp;</td><br>");
        echo ("<td style=\"background-color: #$bgcolor\" align=\"left\"><b>$bryggplats</b></td><br>");
        echo ("<td style=\"background-color: #$bgcolor\" align=\"left\">$efternamn $fornamn</td><br>");
        echo ("<td style=\"background-color: #$bgcolor\" align=\"left\">$telnr</font></td><br>");
        echo ("<td style=\"background-color: #$bgcolor\" align=\"left\">$mobilnr</font></td><br>");
        echo ("<td style=\"background-color: #$bgcolor\">&nbsp;</td>");
        echo ("</tr>");
    }
}
?>

Open in new window

0
 

Author Closing Comment

by:lericson
ID: 34956934
Thanks a lot.
0
 
LVL 7

Expert Comment

by:MrNed
ID: 34956948
I'm guessing the MySQL order by won't work because it seems to be a character typed field judging from your where clause.

The php sort() function returns a bool and modifies the passed in array, so don't echo $plats, echo $bryggplats instead.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question