This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

three field phone number insert


  1. kevtrout
    Member

    I'm creating a web app (php+mysql) that searches my "contacts" database and returns db fields back into the machform layout. I've gotten all that to work but the phone numbers are giving me trouble.

    The "phone" form has three text fields, xxx-xxx-xxxx. But when this is submitted to the database, it becomes xxxxxxxxxx in one field.

    Any idea how to :
    A) submit phone numbers as three fields (element_x_1, element_x_2, element_x_3)
    OR
    B) what the mysql method is for splitting the 10 digit field into three text fields on the form?

    Posted 16 years ago #
  2. arnaud404
    Member

    A> use number field
    B> have a look here
    http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substr

    Posted 16 years ago #
  3. yuniar

    Option B would be the best choice.
    And yes, arnaud404 is correct, you can use substring() function on your SQL to split the field.

    Alternatively, you can use PHP substr() function to split the phone field. MachForm use this method actually. If you checked line 130 of includes/entry-functions.php you will see the code.

    Here is a sample:

    $phone_value = '1234567890';
    $phone_1 = substr($phone_value,0,3);
    $phone_2 = substr($phone_value,3,3);
    $phone_3 = substr($phone_value,-4);


    MachForm Founder

    Posted 16 years ago #
  4. kevtrout
    Member

    Thanks guys.
    As part of my database search results, I define $phone = $row['element_4'](which is the 10 digit phone number). Then instead of <?echo $phone?> in my html text field display layouts, I use these statements:

    <?echo substr($phone, 0,-6);?>
    <?echo substr($phone, 3,-3);?>
    <?echo substr($phone, 6, 4);?>

    Works like a charm, and seems so simple. Thanks for the pointers.

    Yuniar, your code example does the same thing as my code, any consequence to using the negative values in mine over your simpler, more direct values? (Maybe I just pointed out the benefit myself...human-wise, but how about computer-wise?)

    Posted 16 years ago #
  5. yuniar

    Kevin, I don't think there is significant performance difference between those code (at least in this case).
    I suppose you should be fine using whatever method easier for you.


    MachForm Founder

    Posted 16 years ago #

RSS feed for this topic

Reply