PDA

View Full Version : Possible to combine lists of words in Javascript?



someprogress
12-18-2006, 10:23 AM
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

djr33
12-18-2006, 11:39 AM
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... :p

someprogress
12-18-2006, 12:23 PM
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?

djr33
12-18-2006, 01:19 PM
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.

someprogress
12-18-2006, 01:37 PM
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.

djr33
12-18-2006, 09:58 PM
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.

someprogress
12-19-2006, 08:23 AM
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.