Jump to content

[SOLVED] name generator script, isn't working :( help please


interpim

Recommended Posts

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.