Link to home
Start Free TrialLog in
Avatar of antonius_louis
antonius_louis

asked on

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

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
Avatar of ionutz2k2
ionutz2k2
Flag of Romania image

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.
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...



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
Avatar of antonius_louis
antonius_louis

ASKER

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
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)


ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
works great. thanks a lot...

regards
Louis