writing 3 digits number in php (00x format)

Discussion in 'PHP' started by trmatematik, Mar 13, 2010.

  1. #1
    
    for($i=1;$i<=100;$i++)
    {  
    mysql_query("INSERT INTO jos_sorular (count) VALUES ('count.$i') ");
    }
    ?>
    
    PHP:
    This is
    count1
    count2
    count3
    ..........
    count100


    but i want it as this;
    count001
    count002
    ............
    count099
    count100
    .............

    so how can i do this automatically?
     
    trmatematik, Mar 13, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    for($i=1;$i<=100;$i++)
    { 
    if($i < 10){
    $i = "00".$i;
    } else if($i < 100){
    $i = "0".$i;
    }
    mysql_query("INSERT INTO jos_sorular (count) VALUES ('count.$i') ");
    }
    ?>
    PHP:
     
    danx10, Mar 13, 2010 IP
  3. trmatematik

    trmatematik Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    for($i=1;$i<=100;$i++)
    {


    if( strlen($i)==1 )
    {
    $say='00'.$i;
    }
    elseif( strlen($i)==2 )
    {
    $say='0'.$i;
    }
    elseif( strlen($i)==3 )
    {
    $say=$i;
    }

    mysql_query("INSERT INTO jos_sorular (count) VALUES ('$count$say')");
    }
    ?>
     
    trmatematik, Mar 13, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    COUNT is MySQL aggregate function, so better use other for the column name.

    Regards :)
     
    koko5, Mar 13, 2010 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    Try sprintf("%03d", $i);
     
    Kaizoku, Mar 14, 2010 IP