1

I have the results as MA1, MA2.... I want them to be MA0001, MA0002 ... for both checkboxes labels, and the data stored in DB.

My PHP :

<?php
        for ($i = 1; $i < 102; $i++):

            echo '<div class="each_checkboxes">';

            echo '<label class="contact" for="checkbox'.$i.'"></label>';
            echo '<input type="checkbox" name="tape[]" id="checkbox'.$i.'" value=""/>';

            echo '</div>';

        endfor;
    ?>

the js :

$(document).ready(function () {
    $('#method').change(function () {
        var method = $('option:selected').val(),
            text = "";

        if (method == "CSBSFSR20003") {
            text = "MA";
        } else if (method == "CSBSAPP20029") {
            text = "SAS";
        }

        $('.contact').each(function (i) {
            var value = text + (++i);
            $(this).text(value);
            $('#' + $(this).attr('for')).val(value);
        });
    });
});
Billal Begueradj
  • 13,551
  • 37
  • 84
  • 109
Xibition
  • 177
  • 1
  • 3
  • 11

2 Answers2

0

try like this

<?php
        for ($i = 1; $i < 102; $i++):
             $ii=$i;
            if($i<1000)
                $ii="0".$ii;
            if($i<100)
                $ii="0".$ii;
            if($i<10)
                $ii="0".$ii;
            echo '<div class="each_checkboxes">';

            echo '<label class="contact" for="checkbox'.$ii.'"></label>';
            echo '<input type="checkbox" name="tape[]" id="checkbox'.$ii.'" value=""/>';

            echo '</div>';

        endfor;
    ?>
ashkufaraz
  • 4,730
  • 4
  • 46
  • 69
0

Try this one (with a couple of different algorithms for padding the number:

PHP:

<?php
    function addZeros($num){
        if (strlen($num)==3 ){
            $num = '0' . $num;
        }elseif (strlen($num)==2{
            $num = '00' . $num;
        }elseif (strlen($num)==1{
            $num = '000' . $num;
        }
        return $num;
    }
    for ($i = 1; $i < 102; $i++):
        $num = addZeros($i);
        echo '<div class="each_checkboxes">';

        echo '<label class="contact" for="checkbox'.$num.'"></label>';
        echo '<input type="checkbox" name="tape[]" id="checkbox'.$num.'" value=""/>';

        echo '</div>';

    endfor;
?>

javascript:

$(document).ready(function () {
    $('#method').change(function () {
        var method = $('option:selected').val(),
            text = "";

        if (method == "CSBSFSR20003") {
            text = "MA";
        } else if (method == "CSBSAPP20029") {
            text = "SAS";
        }

        $('.contact').each(function (i) {
            var num = addZeros(++i);
            var value = text + num;
            $(this).text(value);
            $('#' + $(this).attr('for')).val(value);
        });
    }); //END #method.change
}); //END document.ready

function addZeros(num) {
  if (num<=9999) { num = ("000"+num).slice(-4); }
  return num;
}

References:

https://stackoverflow.com/a/10073761/1447509

http://php.net/manual/en/function.strlen.php

Community
  • 1
  • 1
cssyphus
  • 31,599
  • 16
  • 79
  • 97