473,834 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort after adding leading zero to date?

I have code numbers in 2 fields from a table which correspond to month
and date.

(Month, Code number)
Field name = ml_mna
1
2
3
etc up to 12
(Data is entered without a leading zero)
Field name = ml_dya
(Date, Code number)
1
2
3
etc up to 31
(Data is entered without a leading zero)

I can sucessfully add a leading zero if necessary to each number after
retrieving with select using the following....

$ml_mna=mysql_r esult($ml_upg,$ i,"ml_mna");
$ml_mna++;
if (strlen($ml_mna ) < 2) {
$ml_mna = '0' .$ml_mna;
}

for the month and

$ml_dya=mysql_r esult($ml_upg,$ i,"ml_dya");
$ml_dya++;
if (strlen($ml_dya ) < 2) {
$ml_dya = '0' .$ml_dya;
}

for the day

However, I need to display the data in date order.

Using "order by" the dates are sorted before I add this leading zero
and the result is an incorrect and confusing display

For instance the date of 30 5 (30 May) is displayed in the wrong place.
(Its sorted as 1,3,4, and not 01, 04, 30).

05 01 (1st May)
05 30 (30th May)
05 04 (4th May)

So how can I sort the date after selecting, after adding the necessary
leading zeros and before displaying?

Thankfull for any help in this matter

Garry Jones
Sweden

Dec 7 '06 #1
5 3483
GarryJones wrote :
I have code numbers in 2 fields from a table which correspond to month
and date.

(Month, Code number)
Field name = ml_mna
1
2
3
etc up to 12
(Data is entered without a leading zero)
Field name = ml_dya
(Date, Code number)
1
2
3
etc up to 31
(Data is entered without a leading zero)

I can sucessfully add a leading zero if necessary to each number after
retrieving with select using the following....

$ml_mna=mysql_r esult($ml_upg,$ i,"ml_mna");
$ml_mna++;
if (strlen($ml_mna ) < 2) {
$ml_mna = '0' .$ml_mna;
}

for the month and

$ml_dya=mysql_r esult($ml_upg,$ i,"ml_dya");
$ml_dya++;
if (strlen($ml_dya ) < 2) {
$ml_dya = '0' .$ml_dya;
}

for the day

However, I need to display the data in date order.

Using "order by" the dates are sorted before I add this leading zero
and the result is an incorrect and confusing display

For instance the date of 30 5 (30 May) is displayed in the wrong place.
(Its sorted as 1,3,4, and not 01, 04, 30).

05 01 (1st May)
05 30 (30th May)
05 04 (4th May)

So how can I sort the date after selecting, after adding the necessary
leading zeros and before displaying?

Thankfull for any help in this matter

Garry Jones
Sweden
Try to put your dates in an array, and sort this array in PHP, since
the latter has so many sort functions ....

http://php.net/sort

--
Naixn
http://fma-fr.net
Dec 7 '06 #2
naixn skrev:
Try to put your dates in an array, and sort this array in PHP, since the latter has so many sort functions ....
How do I do that? I need to display the data with a different colour
background on alternate rows. Currently I have this page which sorts
everything according to another field.

http://www.scfmotion.se/index15ce.php

I want to be able to sort using the date (datum) field instead.

This is the nearest I can get.... (dont know how to use arrays for
this)

First I get the data from several tables with this

(This is the code that sorts it in the wrong order as it has no leading
zeros in its raw data format from the table).

I would like resort all the data once the leading zeros have been
added, but the way I am doing it is adding the leading zero on each row
one by one and each row is processed in its original sort order which
is wrong because of the fact there are no leading zeros.

$ml_collect='SE LECT * FROM ml_lopp LEFT JOIN scfmforening ON
(scfmforening.s cfmnum=ml_lopp. scfmnum) LEFT JOIN ml_tidplats ON
(ml_tidplats.lo ppnum=ml_lopp.l oppnum) ORDER BY ml_mna,ml_dya';
$ml_upg=mysql_q uery($ml_collec t);
$num_ml=mysql_n umrows($ml_upg) ;

// set $i to 0 and print a row "a"

$i=0;

// test to see if last row has been processed
while ($i < $num_ml){
// obtain info from the current row
$loppnmn=mysql_ result($ml_upg, $i,"loppnmn");
$scfmfor=mysql_ result($ml_upg, $i,"scfmfor");
$scfmnum=mysql_ result($ml_upg, $i,"scfmnum");
$ml_mna=mysql_r esult($ml_upg,$ i,"ml_mna");
$ml_dya=mysql_r esult($ml_upg,$ i,"ml_dya");

// add a leading zero to the month if necessary
$ml_mna++;
if (strlen($ml_mna ) < 2) {
$ml_mna = '0' .$ml_mna;

// add a leading zero to the day if necessary
$ml_dya++;
if (strlen($ml_dya ) < 2) {
$ml_dya = '0' .$ml_dya;
$montest[$ml_mna]
}
// print with row a code

echo '<tr><td width="275" class="scfmotio n_rowa">';
print $loppnmn;
echo '</td><td width="275" class="scfmotio n_rowa">';
print $scfmfor;
echo '</td><td width="150" class="scfmotio n_rowa">';
print "2007";
print "-";
print $ml_mna;
print "-";
print $ml_dya;
echo '</td><td width="50" class="scfmotio n_rowa_right">' ;
print $scfmnum;
echo '</td></tr>';

// add one to $i
$i++;
// test to see if last row has been processed
while ($i < $num_ml){

// obtain info from the current row
$loppnmn=mysql_ result($ml_upg, $i,"loppnmn");
$scfmfor=mysql_ result($ml_upg, $i,"scfmfor");
$scfmnum=mysql_ result($ml_upg, $i,"scfmnum");
$ml_mna=mysql_r esult($ml_upg,$ i,"ml_mna");
$ml_dya=mysql_r esult($ml_upg,$ i,"ml_dya");

// add a leading zero to the month if necessary
$ml_mna++;
if (strlen($ml_mna ) < 2) {
$ml_mna = '0' .$ml_mna;
// add a leading zero to the day if necessary

$ml_dya++;
if (strlen($ml_dya ) < 2) {
$ml_dya = '0' .$ml_dya;
$montest[$ml_mna]
}

// print with row b code

echo '<tr><td width="275" class="scfmotio n_rowb">';
print $loppnmn;
echo '</td><td width="275" class="scfmotio n_rowb">';
print $scfmfor;
echo '</td><td width="150" class="scfmotio n_rowb">';
print "2007";
print "-";
print $ml_mna;
print "-";
print $ml_dya;
echo '</td><td width="50" class="scfmotio n_rowb_right">' ;
print $scfmnum;
echo '</td></tr>';
$i++;

}
}

This loops until all rows printed, alternatly with a and b code
Any help appreciated

Garry Jones
Sweden

Dec 7 '06 #3
GarryJones wrote :
[...]
You can do your query like this :
SELECT
*, DATE(CONCAT('20 07', ml_mna, '-', ml_dya)) AS thedate
FROM
ml_lopp
LEFT JOIN
scfmforening
ON
(scfmforening.s cfmnum = ml_lopp.scfmnum )
LEFT JOIN
ml_tidplats
ON
(ml_tidplats.lo ppnum = ml_lopp.loppnum )
ORDER BY
thedate

This idea is to concatenate 2007, the month, and the day, then you get
2007-2-1 for the 1st of january, and the function :
DATE() receives it [ DATE('2007-2-1') ] and return :
2007-02-01
And this value is put in "thedate", which is used for the sorting ;)

Could you try and tell the result?

--
Naixn
http://fma-fr.net
Dec 8 '06 #4
"GarryJones " <mo****@algonet .sewrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.com.. .
>I have code numbers in 2 fields from a table which correspond to month
and date.

(Month, Code number)
Field name = ml_mna
1
2
3
etc up to 12
(Data is entered without a leading zero)
Field name = ml_dya
(Date, Code number)
1
2
3
etc up to 31
(Data is entered without a leading zero)

I can sucessfully add a leading zero if necessary to each number after
retrieving with select using the following....

$ml_mna=mysql_r esult($ml_upg,$ i,"ml_mna");
$ml_mna++;
if (strlen($ml_mna ) < 2) {
$ml_mna = '0' .$ml_mna;
}

for the month and

$ml_dya=mysql_r esult($ml_upg,$ i,"ml_dya");
$ml_dya++;
if (strlen($ml_dya ) < 2) {
$ml_dya = '0' .$ml_dya;
}

for the day

However, I need to display the data in date order.

Using "order by" the dates are sorted before I add this leading zero
and the result is an incorrect and confusing display
Well the problem might be in the database structure rather than how yo use
the date. If the numbers are sorted like you claim - 1,30,4 - it indicates
that the data type of the field is a textual format such as varchar rather
than an integer. What you should be doing is use a datetime data type field
to store the date instead of a couple of varchar fields and you'll have no
worries at all with ORDER BY.

And for simpler leading zero formatting, just use sprintf:
echo sprintf('%02d', $m1_dya);

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net | rot13(xv***@bhg byrzcv.arg)
Dec 8 '06 #5
I tried that but its not working, I got Parse error: syntax error,
unexpected T_LNUMBER in
/customers/scfmotion.se/scfmotion.se/httpd.www/scfml_lst.php on line 6

I think this is because
SELECT
*, DATE(CONCAT('20 07', ml_mna, '-', ml_dya)) AS thedate
FROM
ml_lopp
LEFT JOIN
scfmforening
ON
(scfmforening.s cfmnum = ml_lopp.scfmnum )
LEFT JOIN
ml_tidplats
ON
(ml_tidplats.lo ppnum = ml_lopp.loppnum )
ORDER BY
thedate

This idea is to concatenate 2007, the month, and the day, then you get
2007-2-1 for the 1st of january, and the function :
DATE() receives it [ DATE('2007-2-1') ] and return :
2007-02-01
And this value is put in "thedate", which is used for the sorting ;)
ml_mna and ml_dya are in the table ml_tidplats and that info is
extracted with the left join function. So how can I refer to the fields
ml_mna and ml_dya ?

Dec 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13790
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10 digits), the first 4 being the month and year (mmyy)
2
4571
by: Myk | last post by:
Hello All, None of the solutions I have found in the archives seem to solve my problem. I have a date column in my tables (stored as a char(10)) which I would like to append a leading zero to for those dates that start with 9 or lower. Any ideas? Thanks,
10
3302
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: http://www.ipwebdesign.net/kaelisSpace/useful_tableSort.html This works great except it doesn't sort my UK formatted dates properly, and I end up with something like this: Birth Date (dd/mm/yyyy)
15
8107
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
3
4889
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
2
2380
by: csgraham74 | last post by:
Hi Guys, Just wondering if someone could help me with an arraylist issue. I have an array of integers Arraylist 4 , 2, 11, 2, 21, 3 ,15 , 30 i want the arraylist to now be sorted according to the values either
24
2484
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How can I see in javascript if a web browser accepts cookies? ----------------------------------------------------------------------- Writing a cookie, reading it back and checking if it's the same. http://www.w3schools.com/js/js_cookies.asp Additional Notes:
8
4826
by: Andrew Poulos | last post by:
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a positive integer, that is less than 100, by 100 I get a leading zero in front of the decimal point. For example 80/100 gives 0.8 This is what the server app is expecting a leading zero (it does indeed fail without it). Do I need to do anything to ensure the leading zero is there?
1
7948
by: lornab | last post by:
Good Afternoon I need a column to show in a report as 10 digits long by adding zero's to the left of the result. My select statement is like this: SELECT a.client, a.apar_id FROM agltransact a where a.client = 'SD'
0
10510
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7758
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6954
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5627
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3081
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.