Advanced Search

Results 1 to 7 of 7

Thread: Possible to combine lists of words in Javascript?

  1. #1
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Possible to combine lists of words in Javascript?

    Hi everyone, apologies in advance for the newbie question, but I was wondering if its possible with Javascript to do the following?

    Lets say I have 3 textfields, each with a list of words in them (one per line).

    I would like to print a list of every possible combination (one word from each of the three textfields).

    Any help on this would be greatly appreciated.

    Thanks

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    Yes, possible.

    Not sure exactly how.

    fieldname.value will give you the contents of a textfield. But that's about all I'd know for javascript. I could tell you in PHP... but...
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey thanks for the response..

    hmmm what would be involved in PHP to do this do you think? I'm no coder, but I mentioned it in passing to a knowledgeable friend, and he seemed to think it'd be pretty straight forward... any chance I could take you up on that?

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    Well.... I'm not quite sure what you're doing with the lists, but here's some thoughts--

    1. The values of the sent textfields (assuming by method=post) are accessible through:
    $_POST['fieldname'];

    So... for example: $txt = $_POST['txt'];

    To get rid of windows' linebreaks, use $txt = str_replace('\r','',$txt);
    Then, do make into an array, $txt = explode('\n',$txt);

    Then you've got the parts, and using foreach loops, easy enough to mix them up into sentences.


    foreach ($array1 as $a1piece) {
    foreach ($array2 as $a2piece) {
    foreach ($array3 as $a3piece) {
    $output = $a1piece.' '.$a2piece.' '.$a3piece.'\n'.$output;
    }}}


    Anyway, something like that should work.

    (the output would be each line of each textfield mixed up with each combination from each, like:
    a, b, c; 1, 2, 3; i, ii, iii:
    would result in:
    a 1 i
    a 1 ii
    a 1 iii
    a 2 i
    ...etc...
    b 2 i
    ...etc...
    c 3 iii

    Javascript can likely do something similar, though, if you want that.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh you're a legend! Thanks so much for that - I'll see if I can stumble my way through the rest. I think this would be fine, if I can make it work!

    I take it thats not literally all the code needed? (NewbieShame)

    As far as output goes, thats spot on.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    It has no error checking, no output (like... echo), and no input settings different than just grabbing the post variables. You'll also need to repeat the first couple steps for each input list. But that is just about all you'd need, I think.

    As for error checking, the most obvious thing is having a blank list, so I suggest checking if two of the three lists are blank (in which case there's an error), if not, then checking if one list is blank, and then only doing the foreach loop for two (you can use if ($condition) foreach {}....) for the third inner loop.) Also, if they left the middle or first one blank, you'd want to check that and 'move' the data over to the first two and leave the last blank, using if statements. And, if they just had one list input, you could just seperate the words and display them, no complex operations needed there.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Dec 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey thanks again for the help on this djr33 - I still haven't quite got it working, but I hope if I keep stumbling around in the code I'll figure out the missing pieces

    At the moment it keeps throwing up errors about the foreach lines, and I did read something on Php.net about how nesting foreach commands doesn't work? I suspect its just my lack of coding skills however.

Bookmarks

Posting Permissions

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