?
Solved

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

Posted on 2008-10-30
7
Medium Priority
?
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

718 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