Page 1 of 1
A few questions
Posted: Wed Aug 03, 2005 10:51 am
by toasty2
I dont know much PHP, but i'm good at picking it up and understanding it most times. You can either tell me how to do this or do it for me, i dont care.
I'm wanting to make something with PHP, that writes 1000 lines, each saying "bob 1" without the quotes. And on the second line the 1 would change to a 2 and so on.......
the word bob is just an example, it will say something else.
My PHP book that i have doesnt tell me how to write 1 thousand lines with a counting number on each one, so thats why i'm asking here.
Posted: Wed Aug 03, 2005 10:56 am
by nielsene
Code: Select all
for($i=0;$i<1000;$i++) {
echo "Bob $i <br />";
}
Posted: Wed Aug 03, 2005 10:59 am
by toasty2
sweet thanks.
Now how can i make it randomly choose between sf, at4, ak, ak74su, rpk, svd, mos, spr, rpg, s, s24, gp, g, d, and bdm and randomly display one of those after each line?
BTW, this whole thing togethor will make a big long page showing a thousand commands that do something in a program i have.
Thanks.
Posted: Wed Aug 03, 2005 11:11 am
by timvw
http://www.php.net/count to count the number of avaible thingies
http://www.php.net/rand to get a random number
combine the two, and you can get a random number, which acts as the index, and thus gives you a random element of $thingies.
Posted: Wed Aug 03, 2005 12:57 pm
by toasty2
I dont want random numbers, i want it to randomly choose between these: sf, at4, ak, gp, rpk, svd, s, spr, and display a random one of those at the end of each of the 1 thousand lines.
Posted: Wed Aug 03, 2005 1:21 pm
by trdesigns
Code: Select all
$suffixes=array('sf','at4','ak','gp','rpk','svd','s','spr');
for($i=1;$i<=1000;$i++)
{
$randValue=rand(0,count($suffixes)-1);
echo "Bob {$i} {$suffixes[$randValue]} <br />";
}
I believe that should do the trick. Just as a note, that will make the first line "Bob 1" and one of the suffixes. If you need the first line to be "Bob 0" and some suffix change the
to
Posted: Wed Aug 03, 2005 1:32 pm
by toasty2
Thanks alot! I'm trying it right now.
Thanks again,
GWsux
Posted: Wed Aug 03, 2005 1:44 pm
by toasty2
when i execute the php file, all it shows is:
"; } ?>
Whats the problem?
I used the code trdesigns showed. I even messed with it, but couldnt find out the problem.
Posted: Wed Aug 03, 2005 2:25 pm
by trdesigns
Code: Select all
<?php
$suffixes=array('sf','at4','ak','gp','rpk','svd','s','spr');
for($i=1;$i<=1000;$i++)
{
$randValue=rand(0,count($suffixes)-1);
echo "Bob {$i} {$suffixes[$randValue]} <br />";
}
?>
That code on my machine with PHP 4.4.0 is outputting exactly what you asked for.
Posted: Wed Aug 03, 2005 2:41 pm
by toasty2
lol,
it works fine now, i dont know what the problem was. TI must've been how i accessed my webhost to view the file.
Thanks Everyone.
Posted: Wed Aug 03, 2005 4:06 pm
by pickle
Here's a version that uses array_rand() and is, in my opinion, simpler and easier to understand.
Code: Select all
<?php
$suffixes=array('sf','at4','ak','gp','rpk','svd','s','spr');
for($i=1;$i<=1000;$i++)
{
$rand_suffix = $suffixes[array_rand($suffixes)];
echo "Bob $i $rand_suffix <br />";
}
?>
I'm not sure what the curly braces do inside double quotes - I've never used them so I took them out. array_rand(), obviously, chooses a random element from the array.
Remember that complete randomness doesn't eliminate the possibility of having the same value line after line.
Edit: I updated line 7 of my code to be correct.
Posted: Fri Aug 05, 2005 2:35 pm
by toasty2
I know sometimes it might be the same on two lines next to eachother, and thats ok. It would still be random if every line had the same thing, wouldnt that be something?
Thanks everyone,
GWsux