Results 1 to 13 of 13

Thread: [RESOLVED] Seperated by commas, insert into db...

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Resolved [RESOLVED] Seperated by commas, insert into db...

    i have a textbox named "meta_tags". The user puts tags in the box separated by commas (eg: "dogs,pets,cats")

    How to i separate them so i can insert them into a database?
    My usual boring signature: Something

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Seperated by commas, insert into db...

    use the Explode function

  3. #3

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Seperated by commas, insert into db...

    Quote Originally Posted by lintz
    use the Explode function
    Ok i understand it but

    how to i loop them through a database?
    Last edited by dclamp; Feb 25th, 2007 at 06:58 PM.
    My usual boring signature: Something

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Seperated by commas, insert into db...

    I don't know what you mean. if it's what I think, then I don't see how you're confused with this.

    explode creates an array. if your delimiter is a comma, then you just explode by the commas, and then you could build your query by doing a for() loop through the contents of that array. you could either do an insert query for every loop through, or create a large query that inserts all of the items using the same criteria. example query structure of the latter:
    Code:
    INSERT INTO table_name (field1, field2, field3) VALUES
    ('valueA1', 'valueA2', 'valueA3'),
    ('valueB1', 'valueB2', 'valueB3');

  5. #5

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Seperated by commas, insert into db...

    Quote Originally Posted by kows
    I don't know what you mean. if it's what I think, then I don't see how you're confused with this.

    explode creates an array. if your delimiter is a comma, then you just explode by the commas, and then you could build your query by doing a for() loop through the contents of that array. you could either do an insert query for every loop through, or create a large query that inserts all of the items using the same criteria. example query structure of the latter:
    Code:
    INSERT INTO table_name (field1, field2, field3) VALUES
    ('valueA1', 'valueA2', 'valueA3'),
    ('valueB1', 'valueB2', 'valueB3');
    i understand how to put info into a db.

    I mean, say i have this string:
    PHP Code:
    $string "1,2,3,4,5";
    $string explode(","$string);
    echo 
    $string['0']; //1
    //... 
    $string could be 5 number, or it can be 100 number. How do i put that into a db, since i dont know how many arrays there are?
    My usual boring signature: Something

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Seperated by commas, insert into db...

    if you want to know how many entities are in an array, use count().

    edit: also, it's easier to reference your array using $string[0] (instead of $string['0']) because your array key is an integer rather than a string.

  7. #7
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Seperated by commas, insert into db...

    Try this (untested)
    PHP Code:
     $string "1,2,3,4,5"
    $string explode(","$string); 

    for(
    $i=0;$i<=Count($string);$i++) {

    INSERT INTO table_name (field1VALUES
    ('$string[$i]')


    Last edited by lintz; Feb 25th, 2007 at 08:14 PM. Reason: Fixed typo

  8. #8

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Seperated by commas, insert into db...

    sorry if i sound stupid, but i dont under stand the loop part of this whole thing
    My usual boring signature: Something

  9. #9

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Seperated by commas, insert into db...

    didnt see lintz post. testing now
    My usual boring signature: Something

  10. #10
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Seperated by commas, insert into db...

    Quote Originally Posted by dclamp
    sorry if i sound stupid, but i dont under stand the loop part of this whole thing

    do you not understand how "loops" work or how looping works with inserting into a db?

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: [RESOLVED] Seperated by commas, insert into db...

    Why not a foreach loop?
    Code:
    $nums = explode(',', $string);
    foreach($nums as $num) {
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: [RESOLVED] Seperated by commas, insert into db...

    Quote Originally Posted by CornedBee
    Why not a foreach loop?
    Code:
    $nums = explode(',', $string);
    foreach($nums as $num) {
    }
    little late for that
    My usual boring signature: Something

  13. #13
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: [RESOLVED] Seperated by commas, insert into db...

    At least he's offering a solution

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width