newphpcoder Posted March 23, 2012 Share Posted March 23, 2012 Hi.. I started designing stock form. and I need to display automatically the stock number, the format is:yymmddxxx for example : 120323001 I need to display that stock number in my textbox when I first visit in my form... Thank you so much... Quote Link to comment https://forums.phpfreaks.com/topic/259537-auto-generate-stock-number/ Share on other sites More sharing options...
darkfreaks Posted March 23, 2012 Share Posted March 23, 2012 you need to use Ajax auto fill to populate the number from your database into the textbox. http://www.w3schools.com/php/php_ajax_database.asp Quote Link to comment https://forums.phpfreaks.com/topic/259537-auto-generate-stock-number/#findComment-1330413 Share on other sites More sharing options...
Psycho Posted March 23, 2012 Share Posted March 23, 2012 So, let me paraphrase what I think I understand. You have a form for creating new "items". When that form loads you want to auto-populate the stock number field with a new number in the format "yymmddxxx". I assume that the "yymmdd" stands for year, month, day in two digit format, but you did not state/define what the "xxx" should be. is that supposed to be an auto-incrementing number? If that is correct, then you will need some way to determine the last used number for the given day. That means you will need to store the numbers somewhere - which you also did not elaborate on. So, for the sake of argument I will assume that the xxx is an auto-incrementing number that starts over each day and that the numbers are stored in a database. Off the top of my head, here are the steps I would take when the page loads. 1. Create the datestamp portion for the stock number 2. Get the last used value for the current date from the DB, if it exists 3a. If there is no existing value for the current day create the stock number using 001 3b. If there is an existing value for the current day create the stock number with 1 value higher 4. Populate the field Mock code //Create datestamp $datestamp = date('ymd'); //Query DB for today's highest record $query = "SELECT stockno FROM tablename WHERE stockno LIKE '{$datestamp}%' ORDER BY stockno DESC LIMIT 1"; $result = mysql_query($query) or die (mysqlerror()); if(!mysql_num_rows($result) { $no = 1; } else { $last = mysql_result($result, 0); $no = intval(substr($last, -3)); $no++; } //Create new stock number $newStockNo = $datestamp . str_pad($no, 3, '0', STR_PAD_LEFT); //populate input field echo "Stock Number <input type='text' name='stock_no' value='{$newStockNo}' />"; Quote Link to comment https://forums.phpfreaks.com/topic/259537-auto-generate-stock-number/#findComment-1330414 Share on other sites More sharing options...
newphpcoder Posted March 23, 2012 Author Share Posted March 23, 2012 I resolve it using this code: <?php error_reporting(0); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); if (!$con) { echo 'failed'; die(); } mysql_select_db("mes", $con); $sr_date =date('Y-m-d H:i:s'); $sql = "SELECT sr_num FROM sr ORDER BY sr_date DESC LIMIT 1"; $result = mysql_query($sql, $con); if (!$result) { echo 'failed'; die(); } $total = mysql_num_rows($result); if ($total <= 0) { $currentSRNum = 1; } else { //------------------------------------------------------------------------------------------------------------------ // Stock Number iteration.... $row = mysql_fetch_assoc($result); $currentSRNum = (int)(substr($row['sr_num'],0,3)); $currentSRYear = (int)(substr($row['sr_num'],2,2)); $currentSRMonth = (int)(substr($row['sr_num'],0,2)); $currentYear = (int)(date('y')); $currentMonth = (int)(date('m')); $currentDay = (int)(date('d')); if ($currentYear == $currentSRYear) { if ($currentMonth == $currentSRMonth) { $currentSRNum = $currentSRNum + 1; } if ($currentMonth > $currentSRMonth) { $currentSRNum = 1; } if ($currentDay > $currentSRDay) { $currentSRNum = 1; } } if ($currentYear > $currentRefYear) { $currentSRNum = 1; } } //------------------------------------------------------------------------------------------------------------------ $yearMonth = date('ymd'); $currentSR = $yearMonth . sprintf("%03d", $currentSRNum); echo $sr_date; echo $currentSR; ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/259537-auto-generate-stock-number/#findComment-1330415 Share on other sites More sharing options...
Psycho Posted March 23, 2012 Share Posted March 23, 2012 I'm glad you got it solved, but that is a lot of unnecessary code. There may have been some minor errors in what I provided, but I would have been willing to help you work it out. On second look, that can't be working correctly. Well, it may be "working" but it would be doing so by accident. Take a look at these three lines where you define three values based upon the DB result $currentSRNum = (int)(substr($row['sr_num'],0,3)); $currentSRYear = (int)(substr($row['sr_num'],2,2)); $currentSRMonth = (int)(substr($row['sr_num'],0,2)); You are defining $currentSRNum as the first three characters of the returned value and $currentSRMonth as the first two characters. That can't be right based upon your requirements above. But, oh well. You app. Quote Link to comment https://forums.phpfreaks.com/topic/259537-auto-generate-stock-number/#findComment-1330498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.