3 quick PHP questions

Discussion in 'PHP' started by Yazari, Jul 18, 2007.

  1. #1
    Hello everyone,

    I'm starting a new PHP project and want to know the answer of the following questions :

    • How do I convert int to float (ex : 1 become 1,00)
    • Best solution to generate Bill Numbers ?? (I'm currenty working on it using the MySQL AUTO_INCREMENT Proprety ) any other ways ?
    • Is it possible to store a php array in one MySQL field ?


    Thank You
     
    Yazari, Jul 18, 2007 IP
  2. chris-T33

    chris-T33 Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. $v=$number.",00"; // I guess it will work

    as far as I know - php has no variable definition and you can use a variable now as a String, later as a Int


    3. yes - comma separated for example, and when you import them you do an explode


    exemple

    in mysql: 1,2,3,4,5

    when you will import in php the array ($m is the string taken from mysql query):

    $array=explode(",",$m);


    now you will have:
    $array[1]=1
    $array[2]=2
    $array[3]=3
    $array[4]=4
    $array[5]=5
     
    chris-T33, Jul 18, 2007 IP
  3. Bryce

    Bryce Peon

    Messages:
    1,235
    Likes Received:
    93
    Best Answers:
    0
    Trophy Points:
    0
    #3
    1. you can use the sprintf function.
    ex: $the_float = sprintf("%.02f", $the_int);

    2. the AUTO_INCREMENT is best solution if you want ordered bill #'s

    3. chris answered it...
     
    Bryce, Jul 18, 2007 IP
  4. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    1. $myFloat = $myInt * 1.0; // would be a "float" but if it's for output purposes PHP will not print out characters after decimal if they are all zeros. 11.000 will print as 11, you may want to look up number_format() for better control of numeric output.

    2. what?

    3. You can serialize() your array which will convert it to a string, save it in the db, then when you read it $myArray = unserialize($dbValue); and you have your array back.
     
    mrmonster, Jul 18, 2007 IP
  5. Yazari

    Yazari Peon

    Messages:
    180
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    chris thank you for the 3th answer .. the first one will not work for because i already have in db some float numbers, appliying this will result i guess with x,00,00 numbers


    this works ?? This is exactly what i'm looking for

    Good solutions thank You..
    For bill numbers, I'll explain :

    i want to generate bill numbers like those:

    FG5000001

    or

    2007071700001

    or simply

    #000001

    using the autoincrement i only get 1 2 3 4 single numbers.
     
    Yazari, Jul 18, 2007 IP