Click to See Complete Forum and Search --> : Random Greetings


ryanbutler
04-18-2008, 02:46 PM
I have two PHP documents, one titled 11.9 and the other one random.php. In 11.9 I'm trying to call the random.php document and generate random greetings from a randomization script. However, I can't quite figure out how to call the function from 11.9 and make it execute in random.php.

Here's 11.9:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>11.9</title>
</head>

<body>
<a href="random.php?function=greet">My called link</a>
</body>
</html>


Here's random.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>11.9</title>
</head>

<body>
<?php
//store random greetings

switch($greet){

case 1:
$greet = 'Hello!';
break;
case 2:
$greet = 'Welcome!';
break;
case 3:
$greet = 'Greetings!';
break;
case 4:
$greet = 'Salutations!';
break;
case 5:
$greet = 'Good day!';
break;
case 6:
$greet = 'Yo!';
break;
}
echo $greet;

//set the seed for mtrand with the number of microseconds
//since the last full second of the clock

mt_srand((double)microtime() * 1000000);
//computes a random integer 0-4

$number=mt_rand(0,5);
echo $number;
?>
</body>
</html>

I don't think you can call a link directly from PHP per se. However, I thought perhaps you could call a link that passes a parameter to a function from there, is that correct? I'm not even sure I have the randomization part correct.

Any help or assistance would be greatly appreciated.

TJ111
04-18-2008, 03:09 PM
Why not something like this:

<?php
//array of greetings
$greetings = array("Hello", "Greetings", "etc");

//randomize the order of the array
shuffle($greetings);
?>

<html>
<!--All your html stuff-->
<body>
<?php print array_pop($greetings) /*prints the last array item*/?>
</body>
</html>


Go through the PHP Array documentation, theres all kinds of useful functions in there. What you have is wayyyy too complicated for such a simple problem.

hastx
04-18-2008, 03:19 PM
I would leave everything in the same page, and stay away from the switch statement since it can get quite extensive as you add new greetings. a way to keep it simple would be something like this:

11.9:

<?PHP
$greetings = array('Hello!','Welcome!','Greetings!','Salutations!','Good day!','Yo!','Howdy!','Whats your thang?',"'Sup!" );

$qty = count($greetings);
$new = (rand() % $qty);
$greeting = $greetings[$new]);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>11.9</title>
</head>

<body>
<a href="wheretogo.html"><?PHP echo $greeting?></a>
</body>
</html>


I don't know if I am looking at this right though. I am not sure if this is what you are looking for. You just want a random greeting right?

ryanbutler
04-19-2008, 10:48 AM
Yeah....I'm supposed to stick to the original requirements. I was sitting here going "this is kind of back ass wards and stupid." The original requirements was just using the following:

<?php
//set the seed for mtrand with the number of microseconds
//since the last full second of the clock

mt_srand((double)microtime() * 1000000);
//computes a random integer 0-4

$number=mt_rand(0,4);
echo $number;
?>

As well as trying to make the link call the separate PHP document to show random greetings. Though I could just try one of these solutions to get the thing working first.