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
Solved

How to sort numbers in either MySQL or php

Posted on 2011-02-22
7
302 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
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 …

829 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