Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to make a running number(ex:001,002,003,etc) appear in a textbox

Posted on 2008-10-30
7
Medium Priority
?
385 Views
Last Modified: 2013-12-13
Dear Experts,
i wanna ask, how to make a running number (ex:001,002,003,...etc) appear in a textbox. note,  i dont want to make this running number from auto increment in mysql database, but i wanna describe it manually.also this running number automatically increment after i push a submit button(this submit button are a Saved Query into database).in other word, after i click my submit button,this running number automatically increased.i am using php 5,javascript and mysql database.

many thanks...
Regards
Louis
0
Comment
Question by:antonius_louis
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:ionutz2k2
ID: 22838771
I think what you need is a $_SESSION variable (ex. $_SESSION['myIndex']) that stores that number you want to increment. The script that is run when you press the submit button increments that variable and the script that creates the page and shows that number will show the incremented value.
0
 
LVL 17

Expert Comment

by:psimation
ID: 22839089
This will submit the form to itself and have an incremented number already in textbox when you submit.

Add your SQL insert code below the if statement ( check for $_POST[submit] etc before you do your sql)

<?php
session_start();
if (isset($_SESSION[count]) {
$_SESSION[count]++;
} else {
$_SESSION[count] = "1";
}
?>
<form name="form" method="post" action="">
<input type="text" name="textbox" value="<?=$_SESSION[count]"?>">
<input type="submit" name="submit>
</form>

This code will work "per user" and for the length of the session only.

If you want a number that increments for ALL users who use your site, then it's going to become really tricky.

1st - you will need to use mysql table to first retreive the last number used, then display it, then increment it, and then update the table holding this number.

HOWEVER, this will possibly cause duplicate numbers on busy sites, since there is a timelag between reading, incrementing and updating, meaning two users who hit your site can potentially get the same number back from the table...

What is the purpose of this number you want to have displayed ( also, remeber that displaying the number inside a texfield means a user can change it...)

If you want to indicate or use it as part of a membership number, then I suggest you only generate that number AFTER the data has been entered into the table, then get the "last insert id" back and use that number as part of your unique sequenced number...



0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22844432
If you want to control this number for the duration of a session, put the number in the session.

If you want to control this number on a per-user basis and have it persist beyond the session, put the number into the user records in your data base.

If you want to control this number across the entire population of uses in the data base, use LOCK TABLES while retrieving and updating the number.

Tell us a little more about what you want to use the number for, and we can give you a more succinct explanation.

Best regards, ~Ray
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:antonius_louis
ID: 22848538
for All  Experts:Thanks for the explanation...

for Psimation : could you give me a specific expalanation about your comment ("1st - you will need to use mysql table to first retreive the last number used, then display it, then increment it, and then update the table holding this number.").

regards
0
 
LVL 17

Expert Comment

by:psimation
ID: 22848667
OK, very basic example...

//create a new table called number_table containing only one collumn called "number" of type int size 255
//connection strings here
$sql = "select number from number_table";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$number = $row[number]; // <<== now you have the last number that was writen to table
//now use the number
echo '<input type="text" name="whatever" value="'.$number.'">';
//now increment number and write back to table
$number++;
$sql_update = "update number_table set number = \"$number\";
$result_update = mysql_query($sql_update);
//fin

Now, just remeber the comment from Ray_Paseur re locking tables.

I'm sure he can give you an example of doing that ( you need to add extra SQL to do that)


0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 22854097
Man Page here: http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

The query sequence would be like this:

LOCK TABLE number_table WRITE
SELECT number FROM number_table
/* USE NUMBER as $number */
$number++;
UPDATE number_table SET number = $number
UNLOCK TABLES

Now in the spirit of 100% truth, you do not absolutely have to use MySQL for this - you could also use a flat file, but the ease with which you can lock and unlock tables makes MySQL the easier-to-code answer.

And another note: Minimize the time and risk of script termination between SELECT and UPDATE- you do not want to keep a table locked longer than the minimum!

Best regards, ~Ray
0
 

Author Closing Comment

by:antonius_louis
ID: 31511564
works great. thanks a lot...

regards
Louis
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question