简体   繁体   中英

Increase serial number in php posting

I am using one HTML form to fetch the data in php code. Below is the PHP code:

for ($i = 0; $i < count($sl); $i++) {
$email_body .= "SL:" . $sl[$i] . "NAME:" . $name[$i] . "EMAIL:" . $mail[$i] . "\n";
}

This gives the output in he following format:

SL   NAME    EMAIL
0    Peter   peterxyz@xyz.com
1    Purker  purker234@xyz.com

But what I need it in following format:

SL   NAME    EMAIL
1    Peter   peterxyz@xyz.com
2    Purker  purker234@xyz.com

I can rectify the code by writing "$i=1;" but it is not the correct solution, as this is code again being used in javascript. Hence, please advise me how can I make SL as 1 when $i =0. Kindly help me please?

This should work for you in this case:

$a = 1;
for ($i = 0; $i < count($sl); $i++) {    
    $email_body .= "SL:" . $a++ . "NAME:" . $name[$i] . "EMAIL:" . $mail[$i] . "\n";
}

Demo: https://eval.in/317273

looks like your $sl is 0 indexed and should be 1 indexed, you could do

for ($i = 0; $i < count($sl); $i++) {
    $email_body .= "SL:" . $sl[$i] + 1 . "NAME:" . $name[$i] . "EMAIL:" . $mail[$i] . "\n";
}

or 

for ($i = 0; $i < count($sl); $i++) {
    $email_body .= "SL:" . $sl[$i + 1] . "NAME:" . $name[$i] . "EMAIL:" . $mail[$i] . "\n";
}

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