Solved

How to sort numbers in either MySQL or php

Posted on 2011-02-22
7
299 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:MrNed
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
DoCBReeD earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot.
0
 
LVL 7

Expert Comment

by:MrNed
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now