Page 1 of 1
Generating a random string then deleting it.
Posted: Sat Jul 21, 2007 12:23 am
by akimm
Code: Select all
<?php
$option[] =" a shoe";
$option[] =" sidewalk chalk";
$option[] =" eraser";
$option[] =" monocle";
$option[] =" scratching post";
$option[] =" litter box";
srand ((double) microtime() * 1000000);$randomquote = rand(0,count($option)-1);echo "<p>" . $option[$randomquote] . "</p>";?>
I know it seems silly, by the way, there are actually 50 options, I just have a few listed. What command would be best for deleting an option after its used one time?
Posted: Sat Jul 21, 2007 12:29 am
by feyd
You don't need to call
srand().
array_rand() and
unset() are all you need.
Posted: Sat Jul 21, 2007 12:36 am
by akimm
Code: Select all
<?php
$option[] =" a shoe";
$option[] =" sidewalk chalk";
$option[] =" eraser";
$option[] =" monocle";
$option[] =" scratching post";
$option[] =" litter box";
array_rand ($option);
echo "<p>" . $option[$randomquote] . "</p>";
unset($option);
?>
Thanks Feyd as always you're much help!
Is this something like it should look?
Posted: Sat Jul 21, 2007 12:45 am
by John Cartwright
akimm wrote:
Is this something like it should look?
Change
to
Code: Select all
$randomquote = array_rand ($option);
and you should be ok
Posted: Sat Jul 21, 2007 12:47 am
by akimm
Thanks!
Posted: Sat Jul 21, 2007 1:05 am
by akimm
Code: Select all
<?php
$option[] =" monocle";
$option[] =" scratching post";
$option[] =" litter box";
$randomquote = array_rand ($option);
echo "<p>" . $option[$randomquote] . "</p>";
echo "<p>" . $option[$randomquote] . " was deleted" . "</p>";
unset ($option[$randomquote]);
?>
This code does all except unset the given option, am I using unset wrong? I checked the documents for it over and over, I cannot see what I'm doing wrong. Thanks for any additional help.
Posted: Sat Jul 21, 2007 1:14 am
by John Cartwright
Code: Select all
$option[] =" monocle";
$option[] =" scratching post";
$option[] =" litter box";
$randomquote = array_rand ($option);
echo "<p>" . $option[$randomquote] . "</p>";
echo "<p>" . $option[$randomquote] . " was deleted" . "</p>";
unset ($option[$randomquote]);
echo count($option);
//returns 2
Works fine for me
Posted: Sat Jul 21, 2007 1:16 am
by akimm
it did work for the first iteration now, but after that the count doesn't decrease.
Posted: Sat Jul 21, 2007 1:20 am
by akimm
http://www.akimm.com/allpoetry_quote.php
Here is where its located.
and here is the entire code
Code: Select all
<?php
$option[] =" pencil";
$option[] =" hourglass";
$option[] =" petri dish";
$option[] =" street lamp";
$option[] =" hula hoop";
$option[] =" ring";
$option[] =" hyperdermic needle";
$option[] =" thread";
$option[] =" aluminum tinfoil";
$option[] =" battery";
$option[] =" cd";
$option[] =" elmers glue";
$option[] =" stapler";
$option[] =" fishbowl";
$option[] =" xerox machine";
$option[] =" fork";
$option[] =" cementary stone";
$option[] =" guitar";
$option[] =" phone booth";
$option[] =" satalite";
$option[] =" yo-yo";
$option[] =" nail clipper";
$option[] =" tooth brush";
$option[] =" shovel";
$option[] =" a sieve";
$option[] =" canvas";
$option[] =" comb";
$option[] =" an animals water bowl";
$option[] =" a fence";
$option[] =" seashell";
$option[] =" lanyard";
$option[] =" kalidascope";
$option[] =" microphone";
$option[] =" window drapes";
$option[] =" clothes line";
$option[] =" a fan";
$option[] =" a saw";
$option[] =" pliers";
$option[] =" wrench";
$option[] =" tire";
$option[] =" bug spray";
$option[] =" window";
$option[] =" hammer";
$option[] =" ruler";
$option[] =" a shoe";
$option[] =" sidewalk chalk";
$option[] =" eraser";
$option[] =" monocle";
$option[] =" scratching post";
$option[] =" litter box";
$randomquote = array_rand ($option);
echo "<p>" . $option[$randomquote] . "</p>";
echo "<p>" . $option[$randomquote] . " was deleted" . "</p>";
unset ($option[$randomquote]);
echo count($option);
//returns 2
?>
Posted: Sat Jul 21, 2007 1:42 am
by John Cartwright
are you expecting it delete a new record each time the page is loaded?
Posted: Sat Jul 21, 2007 1:50 am
by akimm
Yes, its because I am hosting a poetry contest on a website I use, everytime I generate a random option I give it to the person, I wish for it to be deleted after its used naturaully.
Posted: Sat Jul 21, 2007 3:45 am
by volka