简体   繁体   中英

how do i generate series of numbers?

i need help to generate series of number and it should be generate series of number when form is submitted i have code but it not working properly?

For Example I Want Like This

00500
00501
00502
00503  and so on...

Here My Code

$random = rand(500,999);
$new_val = $random+1;
for($i=1;$i<=$new_val;$i++)
{
if( strlen($i)==1 )
{
$say='0000'.$i;
}
elseif( strlen($i)==2 )
{
$say='000'.$i;
}
elseif( strlen($i)==3 )
{
$say='00'.$i;
}
elseif( strlen($i)==4 )
{
$say='0'.$i;
}
elseif( strlen($i)==5 )
{
$say=$i;
}
}

<form method="post">
<input class="input_field" readonly="readonly" type="text" 
name="sno" id="sno" value="<?=$say?>"  >
<input type="submit" name="test" value="submit" />
</form>

Use sprintf to format it:

<?php

$start = rand(500, 999);

for ($i = 1; $i <= $start; $i++) {
    $num = sprintf("%05d", $i);
    echo $num;
}

?>

As Blender says, use sprintf to format your numbers

$start = rand(500, 999);
for ($i = $start; $i <= 999; $i++) {
    $num = sprintf("%1$05d", $i);
    echo $num . '<br>';
}

Note that the form must contain an "action" attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM