Solved

How to sort numbers in either MySQL or php

Posted on 2011-02-22
7
304 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 1

Accepted Solution

by:
DoCBReeD earned 500 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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