Log in

View Full Version : [Tutorial] PHP ~ Love Calculator



lolwut
07-10-2007, 03:25 PM
These things are always popular with everyone, so I decided to code one to try to make my hunger pains go away... This uses a the rand(); function and echo's an appropriate message to their number.
It's entriely self contained.


<?php
//LOVE CALCULATOR!!!
echo("
<font size=\"2\" face=\"Verdana\">
<form method=\"POST\">
Your Name: <input type=\"text\" name=\"name1\">
<br />
Crushes Name: <input type=\"text\" name=\"name2\">
<br />
<input type=\"submit\" value=\"Calculate\">
</form>
");
//Woo. So now we choose what to do if the forms sent.
$name1 = $_POST['name1']; // The "Your Name:" variable.
$name2 = $_POST['name2']; // The "Crushes Name:" variable.
if(!$name1){ exit(); }
$rand = rand(1,99); // Uses the rand(); function in PHP to make a number between 1 and 99
echo("<b>$name1</b> and <b>$name2</b>'s love rating is <b>$rand</b>.<br />"); // Result. We've echoed it. Now we'll do a "Cupids Comment" thing.
if($rand <= "20"){
echo("Cupids Comment: Face it, it's not gonna work out between the two of you!");
}
if($rand <= "40" && $rand >= "21"){
echo("Cupids Comment: There could be love between you two, but then again, maybe not.");
}
if($rand <= "60" && $rand >= "41"){
echo("Cupids Comment: Try it and see, there's about a 50/50 chance.");
}
if($rand <= "80" && $rand >= "61"){
echo("It's likely, the chances of the spark going are preety slim.");
}
if($rand <= "99" && $rand >= "81"){
echo("Cupids Comment: There's almost definiatley love there! Go ahead. Your not gonna be heartbroken.");
}
echo("</font>");
?>

Fixed all errors, see it in action at http://tazroger.net/edd/lovecalc.php.

lolwut
07-10-2007, 03:50 PM
Wouldn't let me edit;
Better code, fixed >= errors;


<?php
echo("<font face=\"Verdana\" size=\"2\">");
//LOVE CALCULATOR!!!
if(!$_POST['calc']){
echo("
<form method=\"POST\">
Your Name: <input type=\"text\" name=\"name1\">
<br />
Crushes Name: <input type=\"text\" name=\"name2\">
<br />
<input type=\"submit\" value=\"Calculate\" name=\"calc\">
</form>
");
}else{
//Woo. So now we choose what to do if the forms sent.
$name1 = $_POST['name1']; // The "Your Name:" variable.
$name2 = $_POST['name2']; // The "Crushes Name:" variable.
if(!$name1){ echo("Please enter your name!"); exit(); } // If they didn't post their name, reject the form.
if(!$name2){ echo("Please enter your crushes name!"); exit(); } // If they didn't post their crushes name, reject the form.
$rand = rand(1,99); // Uses the rand(); function in PHP to make a number between 1 and 99
echo("<b>$name1</b> and <b>$name2</b>'s love rating is <b>$rand</b>.<br />"); // Result. We've echoed it. Now we'll do a "Cupids Comment" thing.
if($rand <= "20"){
echo("Cupids Comment: Face it, it's not gonna work out between the two of you!"); // Message if love rating is under 20.
}
if($rand <= "40" && $rand >= "21"){
echo("Cupids Comment: There could be love between you two, but then again, maybe not."); // Message if love rating is under 40.
}
if($rand <= "60" && $rand >= "41"){
echo("Cupids Comment: Try it and see, there's about a 50/50 chance."); // Message if love rating is under 60.
}
if($rand <= "80" && $rand >= "61"){
echo("It's likely, the chances of the spark going are pretty slim."); // Message if love rating is under 80.
}
if($rand <= "99" && $rand >= "81"){
echo("Cupids Comment: There's almost definiatley love there! Go ahead. Your not gonna be heartbroken.");
}
$referer = $_SERVER['HTTP_REFERER'];
echo("<br /><a href=\"$referer\">Try Again?</a>"); // Shows Try Again message.
}
echo("</font>");
?>

RYANNNNN
07-10-2007, 03:55 PM
Learn to indent your code.

Eccentric
07-10-2007, 04:04 PM
Well its different and definetly a good try. :) I may use it on my site if i have random 'generators' as such. :P would rep you if I could.

Net-Margin
07-10-2007, 04:06 PM
Learn to indent your code.

Quoted for truth.

lolwut
07-10-2007, 04:59 PM
Thanks all, indentings hard as hell in a web editor, unless you wanna use spaces. I prefer tabs.

timROGERS
07-10-2007, 05:02 PM
I was gonna make one of these too, I'll indent it for you :P (maybe)

Dentafrice,
07-10-2007, 05:20 PM
<?php
if ($_GET['action'] == "submit")
{

$name1 = $_POST['name1'];
$name2 = $_POST['name2'];

if ($name1 == null)
{
echo "Please enter your name!";
exit;
}
if ($name2 == null)
{
echo "Please enter your crushes name!";
exit;
}

$rand = rand(1, 99);
echo "<strong>$name1</strong> and <strong>$name2</strong>'s love rating is <strong>$rand</strong>.<br />";

if ($rand <= "20")
{
echo ("Cupids Comment: Face it, it's not gonna work out between the two of you!"); // Message if love rating is under 20.
}
if ($rand <= "40" && $rand >= "21")
{
echo ("Cupids Comment: There could be love between you two, but then again, maybe not."); // Message if love rating is under 40.
}
if ($rand <= "60" && $rand >= "41")
{
echo ("Cupids Comment: Try it and see, there's about a 50/50 chance."); // Message if love rating is under 60.
}
if ($rand <= "80" && $rand >= "61")
{
echo ("It's likely, the chances of the spark going are pretty slim."); // Message if love rating is under 80.
}
if ($rand <= "99" && $rand >= "81")
{
echo ("Cupids Comment: There's almost definiatley love there! Go ahead. Your not gonna be heartbroken.");
}
$referer = $_SERVER['HTTP_REFERER'];
echo "<br /><a href=\"$referer\">Try Again?</a>"; // Shows Try Again message.
exit;
}
?>
<font face="Verdana" size="2">
<form method="post" action="?action=submit">
Your Name: <input type="text" name="name1">
<br />
Crushes Name: <input type="text" name="name2">
<br />
<input type="submit" value="Calculate" name="calc">
</form>
</font>


try that.

Eccentric
07-10-2007, 05:34 PM
Sounds stupid but what does indent mean:S

Dentafrice,
07-10-2007, 05:36 PM
Sounds stupid but what does indent mean:S
From this:



<?php
$name = "bob";
if($name == "bob") {
echo "hey";
}else{
echo "bye";
}
?>


to



<?php
$name = "bob";
if ($name == "bob")
{
echo "hey";
}
else
{
echo "bye";
}
?>

Eccentric
07-10-2007, 06:39 PM
so you space it out so its easier to read basically yes?

lolwut
07-10-2007, 08:15 PM
Yeah, and say PHP gets an error like UNEXPECTED T_ELSE ON LINE 23, you can easily tell where the else is. And target the problem easier.

Want to hide these adverts? Register an account for free!