Generating a random string then deleting it.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Generating a random string then deleting it.

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't need to call srand().

array_rand() and unset() are all you need.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

akimm wrote: Is this something like it should look?
Change

Code: Select all

array_rand ($option);
to

Code: Select all

$randomquote = array_rand ($option);
and you should be ok
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Thanks!
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

it did work for the first iteration now, but after that the count doesn't decrease.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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 

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

are you expecting it delete a new record each time the page is loaded?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You might be interested in sessions to preserve data between requests.
http://phpnet/sessions
http://www.google.de/search?&q=php%20session%20tutorial
Post Reply