How to generate a ticket number using given sequence in PHP - javascript

I need to generate one ticket number using the given sequence which is given below using PHP.
1-W1(mandotary).
2-date(yy-dd-mm)format.
3-001-999(each day it will start from 001).
One example is given below.
e.g-W120160121001
I have done the below part which is incomplete.
if($_REQUEST['typeOfApp']=="WEB"){
$ticket_id='W1'.date("Y-m-d").'';
}
Suppose one click event I will generate this ticket number and each day the last 3 digit of this ticket number will start from 001 and up to 999.

PHP does not have a mecanism to synchronize a bloc code execution
2 users can obtains the same ticket number !!
also, you have to store the current ticket number somewhere !!
Personally, i've wrote an open source project that holds ans synchronize the access of variables, you can get it from here in github :PHP - Synchronized Data Structure Server
And there are other solutions, like storing and incrementing in synchronized flavor using database

Add one more field like id in your table, which stores id number of your ticket booking. and one more query you have to add before inserting the record in the table.
First check that how many records it will have for the same date? and then increment the count of that record and append your dynamic generating ticket code as follows.
$ticketnumber = 'W1' . date('YdM'). str_pad(($tempid + 1), 3, '0', STR_PAD_LEFT);
$tempid is the key which is added/started from 1 for the same date.

$file = 'ticketfile.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Increase ticket number
$current=$current+1;
// Write the contents back to the file
file_put_contents($file, $current);
$ticketNum = "W1".date('dmy').$current;
echo($ticketNum);
// This will echo out something like this
// W121012016001
You can also save the ticket number to a database and call last recored before creating new ticket.

Possible you only need something like this:
$ticketnumber = 'W1'.date('YdM' time()).str_pad($value, 3, '0', STR_PAD_LEFT);
$value is the last ticket key entered for the day. This could be handled by a function to check the last ticket day. If the same date +1 the count. if it is not same date reset the counter.
So lets say you have the following function to generate the ticket code:
function ticket_id_generator($ticketnumber){
$dateline = intval(substr($ticketnumber, 2, 8));
$value = intval(substr($ticketnumber, -3));
if($dateline == date('YdM', time())
return $value++;
else
return '1';
}

Related

How to make php get last row from a Colum from mysql and make the result a variable that I can take with javascript?

I want php to get the last row of a specific Colum. Then I want the result to be a variable that javascript can take and have it a javascript variable.
something like this maybe
var result = [php variable]
My question is not just about how to make a php variable into javascript varibale but also on how to make php get the last row of a colum. Thanks, BTW (I'm new to this stuff so please be clear in your explainations.)
In order to get the last row of a specific colum, you need to query your database in a way such that your desired "last row colum value" is easily obtainable by php. This is commonly done using the SQL ORDER BY and LIMIT clauses.
For example:
SELECT name FROM my_table ORDER BY id DESC LIMIT 1;
This query's result-set will contain only the name column value for the my_table record with the highest id.
PHP can then assume that this data is contained by the first row in the result-set. For example (using PDOStatement):
<?php
//...
$data = $pdoStatement->fetch(PDO::FETCH_ASSOC);
echo $data['name']; // The name column value of the row with the highest id
The specifics of the query for your use case are not made clear by your original post, but the above example logic should apply anyhow. Make sure you provide the appropriate query and have PHP fetch the first record in the result-set.
You can then parse this data into Javascript variables:
<?php
<script>
var result = "<?php echo $data['name']; ?>";
</script>
Warning: This example is very basic, make sure you protect yourself against XSS vulnerabilities. Do not let database data provided by PHP be evaluated by Javascript without protection.

Need help in Automated userid generation using jquery

I want to generate a login id automatically. Logic is to take the user's last name and append a number(like 1, then 2 , then 3). system will also check if this user id exist in database. I have cached the database data in a local variable. If the user id is unique, it will be populated in a text box, otherwise it will append a new number. But This process may go for 10 times, if 10 user has same last name. Could anybody provide a simple solution for this. I want this to be done in JQuery.
Let's say John Doe tries to register an account. You need to:
Send 'Doe' to backend via ajax
Execute a query like this to find the greatest Doe* in DB:
SELECT user_id FROM users WHERE surname = 'Doe' ORDER BY added_ts DESC LIMIT 1
If a record is found, extract the number part out of the user id and send it back, if no record is found simply send 0 back
When ajax response arrives, increment the number you get and concatenate it to the surname.
I am assuming you have a record timestamp of some sort to use in the query (added_ts).
Edit: #Jaromanda X 8 pointed correctly that the query might return wrong results and it should be the surname field we need to check.

To store and retrieve schedule from database the exact same location

I'm trying to create a class schedule here.
I know I'm not supposed to ask before trying anything, the problem is I can't think of any logic to do this.
As you can see I have a table as below
What I want to do is, I want to store the data in the database, and when I retrieve the data, I want to display it in the exact same location as the picture. Please help me out.
EDIT : I'm a beginner in programming please go easy on me
Code the PHP to create the HTML to display the schedule as it is in the image above, but without the courses. Hint: you should use a two dimensional array of days and times.
Create a two dimensional array of the schedule, using array keys that match those you created in step 1.
Write the code to put the schedule from step 2 in the display from step 1.
Create your database and a table for the schedule.
Write the code that reads the schedule from the database, discard the code that put the schedule into the display from the array.
Here is an example :
store data like this:
this is the query.
$query = "SELECT time_format(schedule,'%T') as schedule,
if(dayname(schedule)='Sunday',subject,'') as Sunday,
if(dayname(schedule)='Monday',subject,'') as Monday,
if(dayname(schedule)='Tuesday',subject,'') as Tuesday,
if(dayname(schedule)='Wednesday',subject,'') as Wednesday,
if(dayname(schedule)='Thursday',subject,'') as Thursday,
if(dayname(schedule)='Friday',subject,'') as Friday,
if(dayname(schedule)='Saturday',subject,'') as Saturday
FROM scheduleTable
WHERE schedule between '2016-7-25' and '2016-7-31 23:59'";
3.this is the result from the query
Then you can use PHP to style the table or send it to Ajax let javascript do the job.

Laravel count the IP adressess from this month

I have a visitor counter that works. Now, I need to get the results back to display them into a chart. What I have now in my controller to display them works, but the date is wrong.
This is what I have:
$select_stats = DB::table('visitor')->where('visit_date', '>', '2015-04-01')->where('visit_date', '<', '2015-04-30')->get();
foreach($select_stats as $statics) {
//change our object to an array
$statics = array();
}
return View::make('admin.home.index')->with('stats', $select_stats);
I need to get the 'stats' from the current month, so in this case month 4, but if it is may, then it needs to select automatically the stats from the 5th month.
So that is already a problem for me.
Now, I need to loop them in my blade themplate, that works... But the date is in the format
YYY-MM-DD
I don't want it displaying like that I do prefer the format:
DD/MM/YYYY
Okay, so far, so good.
I also need to count the IP's that are stored in the database. Also from THAT month.
And also need to loop and display the count.
The database model I have:
The table name is called visitor.
My blade template for the chart:
<script type="text/javascript">
var visitors = [
#foreach($stats as $stat)
['{{ $stat->date }}', 500],
#endforeach
];
</script>
Yes, this works in the script.
If you're always dealing with the current month, try this:
$select_stats = DB::table('visitor')->whereBetween('visit_date', array(date('Y-m-01'), date('Y-m-t')))->get();
the lowercase t in the date function returns the number of days in the given month (http://php.net/manual/en/function.date.php).
Simply put, to convert that date format in your template, you could echo it as the following:
{{ date('Y/m/d', strtotime($stat->date)) }};
To get the count of the IP's you could perform the same query but add replace ->get() with ->count() like so:
DB::table('visitor')->whereBetween('visit_date', array(date('Y-m-01'), date('Y-m-t')))->count();
You already have the IP field from the first query, so just echo out {{$stat->ip}}
There's always different & better ways to achieve certain results, but without context, here's a solution that works.

How to read from another row in javascript step of Pentaho?

I'm working on an ETL process with Pentaho Data Integration (Spoon, before Kettle).
In the Modified Javascript step of Pentaho you can set a start, end and transform script. In the transform script you can write code that it will be executed only for each row, and from here I don't know how to access to data of the previous row (if it's possible).
I need access to the previous row because all rows are ordered by product, store and date (respectively), and the goal is to get the quantity on hand from the previous row and add the quantity sell or received on the current row (this would be the same product, same store but different date). I also need accessing to the previous row to compare the product and store of the current row with the previous row, because if someone of them changes I must to restart the field quantity_on_hand (I do it with a field of all columns named initial_stock).
On pseudocode would be something like this (if I hadn't the restriction of that the code written on the step is executed only for each row):
while(all_rows_processed()){
current_row.quantity_on_hand = current_row.initial_stock;
while(id_product_current_row == id_product_previous_row && id_store_current_row == id_store_previous_row){
current_row.quantity_on_hand = previous_row.quantity_on_hand + current_row.stock_variation;
}
}
This question related couldn't help me.
Any ideas to solve my problem would be appreciated.
May I ask you to reconsider Group By step? It seems suitable for your scenario.
If you sort the stream accordingly to your combination date/store/article, you can calculate cumulative sum for sell/received quantity. This way you can have a running total of inventory variation that would be reset on a group basis.
Also give a look both at this blog post and at the forum post it quotes.
I doubt you need to go to JavaScript for this. Check out the Analytic query step. That will allow you to bring a value from the previous row into the current.
The JavaScript step gives you tremendous flexibility, but if you can do it with the regular transform steps, it will typically be much faster.
use Analytic Query. By Using this Step u can access the previous / next record. Actually, not only prev and next record that you can read, but you can read N Rows Fordward or N Rows Back Wards.
Check the following URL for clearer expalanation :
http://wiki.pentaho.com/display/EAI/Analytic+Query
http://www.nicholasgoodman.com/bt/blog/2009/01/30/the-death-of-prevrow-rowclone/
Thanks for all, I've got the solution to my problem.
I've combined all your suggestions and I've used the Analytic Query, Modified Javascript and Group by steps.
Although the question wasn't very well formulated, the problem I had was to calculate the stock level on each row (there was one row for each product, date and store combination).
First (obviously later than sort rows by product_id, store_id and date ascending), I used the Analytic Query step to group by product_id and store_id, because with this step I've got a new field previous_date to identify the first row of each group (previous_date=null on the row of the group where date was the oldest).
Then I needed to calculate the quantity_on_hand of each group [product,store] at first row (first date of each group because it's sorted by date) because the initial_stock is different for each group. This is because of (sum(quantity_received) - sum(quantity sold)) != quantity_on_hand.
Finally (and the key was here), I used the Group by step like #andtorg suggested and do it as the next image shows.
This link that #andtorg suggested was very useful. It includes even two .ktr example files.
Thank you so much for help!

Categories

Resources