Solved

Php sorting mysql results based on id's that have different numbers of digits

Posted on 2012-12-25
11
289 Views
Last Modified: 2013-01-05
Hi

I have a database that needs to be displayed based on their  id, in phpmyadmin it works correctly

1
23
156
276
1245

but in php it sorts like

1
1245
156
23
276


I have tried to search for this here but did not solve it, hope anyone can help me, the id's are all digits

could or should I just insert digits into the numbers that are:
1
23
156

like they will become

0001
0023
0156

if so, how should i do that?

I prefer filling up the id's with more digits, the numbers will never become greater than 5 digits but if for display php can do this so i do not have to alter the database that is even safer i think
0
Comment
Question by:PeterdeB
[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
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38720054
SELECT the column and add 0 to it (or maybe multiply it by 1).  Best wishes, ~Ray
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 38720057
If it is defined as an integer (data type = INT) it should be expected to ORDER correctly in a MySQL query.  If you want numeric ordering, but you also want to carry leading zeros or spaces, you might make the column INT and use PHP function str_pad() to format the output.  The problem you can run into if you try to store leading zeros or space is that "loose" data typing can cause PHP to transform the character strings into numeric values.
0
 

Author Comment

by:PeterdeB
ID: 38720239
it orders correctly in mysql as i have stated, it is defined as int, i have no idea what you mean by your answer with all respect, i did not register here because of my genius in mysql haha anyway thanks ill try
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38720289
OK, perhaps I had it backwards!  To order this information in PHP try natsort().
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 166 total points
ID: 38720374
You are seeing the difference between ordering by number (binary) (your first example from phpMyAdmin) and ordering by character (text) (your second example from PHP).  When phpMyAdmin displays the data, it always does 'ORDER BY' and I'm pretty sure the initial column is the first column which is usually where an 'id' column is.  

However, the data returned to PHP in a query is in Text format.  If you want PHP to consider the data in your 'id' a number for sorting by PHP, you have to tell PHP to do that.  The two simplest ways are to add a 0 or multiply by 1.  PHP will automatically convert the result to a number instead of text when you do that.
0
 
LVL 78

Assisted Solution

by:arnold
arnold earned 167 total points
ID: 38720592
Could you post your php sorting code?

As others pointed out, mysql will sort it based on the data type.
Which fetch_row are you using?

http://php.net/manual/en/function.mysql-fetch-row.php
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38720982
In your php, try to form your query like that:

SELECT * FROM yourtable ORDER BY id DESC

Open in new window


That should do it. I think that's what phpmyadmin is doing also. Just try and let me know what happened. But the experts are right to demand your actual php code to help you better.
0
 

Author Comment

by:PeterdeB
ID: 38724340
hey sorry for the late response while being busy with this project my system does suddenly not execute anything anymore except for a few firefox windows
0
 

Author Comment

by:PeterdeB
ID: 38726738
 printf( "%05d", $rows['products_id'] 

Open in new window


using sprintf solved all the issues, thanks all for participating, to your opinion, what should i do with the points, because some of you did actually point me in that direction ofcourse
0
 
LVL 78

Expert Comment

by:arnold
ID: 38726971
Choose the answers that helped you and assign points as you see it.
0
 

Author Closing Comment

by:PeterdeB
ID: 38746617
Thanks heaps
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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 …

749 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