Jump to content

auto generate stock number


newphpcoder

Recommended Posts

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}' />";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.