Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to sort numbers in either MySQL or php

Posted on 2011-02-22
7
Medium Priority
?
306 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:Lennart Ericson
[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:Lennart Ericson
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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:Lennart Ericson
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

604 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