Re: Re: Assigning a file number

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Thanks,

Bastien

> On Nov 3, 2013, at 3:46 AM, Tsvetan Nikolov <live.webscore@xxxxxxxxx> wrote:
> 
> Hi John,
> 
> You can easily express 33 as 0033 by using the mysql command LPAD when
> selecting the field. So essentially the id in the database will be 33 but
> you will re-format it during selection to 0033. As a general rule do not
> mess with data in DB. Store data in the classical way and if you need it
> differently work on it after/during selection.
> 
> Hope this helps some!
> 
> Good luck
> 
> 
> On Sun, Nov 3, 2013 at 7:10 AM, John Taylor-Johnston <
> jt.johnston@xxxxxxxxxxxxxx> wrote:
> 
>> John Taylor-Johnston wrote:
>> 
>>> I need to assign a unique file number
>>> 
>>> 50-20131103-0001
>>> 
>>> Ussers work in secteur 50
>>> Today is 20131101
>>> The first file number possible today is 0001 (of probably no more than
>>> 9999.
>>> 
>>> How should I build this?
>>> 
>>> Should I
>>> create a varchar in MySQL and stock it all in one field?
>>> or
>>> have three separate fields?
>>> 
>>> secteur: '50' (INT) default=50
>>> date: (DATESTAMP)
>>> filenumber 0001 (INT)
>>> 
>>> Then here comes the PHP question?
>>> 
>>> When a user opens up my php page, my code will automatically assign
>>> $filenumber sequentially.
>>> 
>>> So how do I check if $filenumber = 0001 exists already? Using a while or
>>> if seems like a lot of work. Swatting a fly with a baseball bat.
>>> 
>>> There must be an easier trick, maybe a MySQL function to says if 0001 is
>>> taken, check and see if 0002 is available ++ until one is free. Then INSERT
>>> it so my user can enter his data.
>>> 
>>> Failing that I could use an INT field that auto_increments everytime I
>> INSERT. But a I will want to express a value of 33 as 0033, or 2 as 0002 -
>> in four digits. How can I do that?
>> 
>> 
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>> 
>> 

Please note that when manually generating file numbers in this method you leave yourself open to race conditions. This is where two users touch the db at the same time generating the same ID. So my suggestion is to use an auto number field as the true ID and not worry too much about patterns for file names. It may look nice but the race conditions are truly problematic to deal with. 

Also from a practical standpoint I suggest storing files in folder by year-month (2013-11 for example) so that you don't run into issues when viewing the folder. It will also make backups easier
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux