interpim Posted March 4, 2007 Share Posted March 4, 2007 Im creating a name generator script and as far as I can tell the code looks ok, then again, I may be thinking things work differently than they actually do... so basically I have 3 text files with strings on each line. I want to select a random string from each file and concatenate the 3 into one string... I am getting nothing in the output for some reason here is the link to the directory w/ the files http://interpim.com/ambassade and here is the code <?php echo "test"; $f_first=fopen('name_first.txt'); srand ((double) microtime( )*1000000); $random_first = rand(0,161); $i=-1; while(&i<$random_first) { $first=fgets($f_first); $i++; } fclose($f_first); $f_middle=fopen('name_middle.txt'); srand ((double) microtime( )*1000000); $random_middle = rand(0,135); $j=-1; while($j<$random_middle) { $middle=fgets($f_middle); $j++; } fclose($f_middle); $f_last=fopen('name_last.txt'); srand ((double) microtime( )*1000000); $random_last = rand(0,173); $k=-1; while($k<$random_last) { $last=fgets($f_last); $k++; } fclose($f_last); $name=$first . $middle . $last; echo "$name<br>"; ?> what am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/ Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 There's a much simpler way doing that: <?php $names = array("name_first.txt", "name_middle.txt", "name_last.txt"); $result = ""; foreach($names as $file) { $lines = file($file); shuffle($lines); $result .= $lines[0]; } echo $result; ?> I hope it helps Orio. Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199206 Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 Another thing I've noticed- Your first loop: while(&i<$random_first) Should be: while($i<$random_first) But I still think you should use the script I've attached above. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199207 Share on other sites More sharing options...
interpim Posted March 4, 2007 Author Share Posted March 4, 2007 OK... but i ran it and it worked... but how do i remove the blank space between the strings? Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199209 Share on other sites More sharing options...
interpim Posted March 4, 2007 Author Share Posted March 4, 2007 or is that a problem with my text files? Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199212 Share on other sites More sharing options...
interpim Posted March 4, 2007 Author Share Posted March 4, 2007 anymore help on this? need to have the strings together with no spaces. Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199233 Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 You want to remove all of the spaces from the strings, or only the spaces between the three? Orio. Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199236 Share on other sites More sharing options...
interpim Posted March 4, 2007 Author Share Posted March 4, 2007 between them... right now i get.... string1 string2 string3 in the output... I need it string1string2string3. i look at source and it is string1 string2 string3 Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199240 Share on other sites More sharing options...
Orio Posted March 4, 2007 Share Posted March 4, 2007 <?php $names = array("name_first.txt", "name_middle.txt", "name_last.txt"); $result = ""; foreach($names as $file) { $lines = file($file); shuffle($lines); $result .= trim($lines[0]); } echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199244 Share on other sites More sharing options...
Snooble Posted March 4, 2007 Share Posted March 4, 2007 kill the lines between the echos... It happened to me a couple of nights ago. Snooble $name=$first.$middle.$last; Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199247 Share on other sites More sharing options...
interpim Posted March 4, 2007 Author Share Posted March 4, 2007 Thank you so much Orio... works great now guess I will start learning more about foreach() hehe just learning now Quote Link to comment https://forums.phpfreaks.com/topic/41128-solved-name-generator-script-isnt-working-help-please/#findComment-199249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.