Jump to content

Basic PHP variable problem


aidoDel

Recommended Posts

I have a html form where user enters birthday (day + month) and it is sent to be processed by php to calculate their star sign. Everything works fine but I'm unsure how to treat the data once sent.

 

PHP

<?php
$Capricorn = array("Capricorn", "The sign Capricorn is one of the most stable and (mostly) serious of the zodiacal types.");
$Aquarius = array("Aquarius");
$Pisces = array("Pisces");
$Aries = array("Aries");
$Taurus = array("Taurus");
$Gemini = array("Gemini");
$Cancer = array("Cancer");
$Leo = array("Leo");
$Virgo = array("Virgo");
$Libra = array("Libra");
$Scorpio = array("Scorpio");
$Sagittarius = array("Sagittarius");
?>
    
<?php
    $user_month = $_POST['user_month'];
    $user_day = $_POST['user_day'];
    echo "Your birthday is {$user_day} of {$user_month} that makes your star sign ";
?>
<?php
// .........JAN = CAPRICORN or AQUARIUS........
if ( $user_month  == "January" ) {
    if ($user_day <= "19"){
    echo "{$Capricorn;[0]}";
    }
    else{
        echo "{$Aquarius[0]}";
    }
}

 

Rather than echo $Capricorn, I want to set this value to be $Capricorn so I can call it later.

 

I have no clue how to go about doing this.

Link to comment
Share on other sites

<?php

$month = 12;
$year = 1985;

function star_sign($month, $year) {
$time = mktime(0, 0, 0, $month, 0, $year);
$day_of_year = date("z", $time);

if (date("L", $time) && ($day_of_year > 59))
	$day_of_year -= 1;

switch ($day_of_year) {
	case $day_of_year > 356:
		return "Capricorn";
	case $day_of_year > 326:
		return "Sagittarius";
	case $day_of_year > 296:
		return "Scorpio";
	case $day_of_year > 266:
		return "Libra";
	case $day_of_year > 235:
		return "Virgo";
	case $day_of_year > 203:
		return "Leo";
	case $day_of_year > 172:
		return "Cancer";
	case $day_of_year > 140:
		return "Gemini";
	case $day_of_year > 111:
		return "Taurus";
	case $day_of_year > 78:
		return "Aries";
	case $day_of_year > 51:
		return "Pisces";
	case $day_of_year > 20:
		return "Aquarius";
	default:
		return "Capricorn";
}
}

die(star_sign(12, 1985));

Link to comment
Share on other sites

i see you are giving all variables an array which is pointless you need to have something like this if you wants to store only one value;

$star = arrary('Capricorn','Aquarius','Pisces','Aries','Taurus','Gemini','Cancer','Leo','Virgo','Libra','Scorpio','Sagittarius');

and then you can call like this:

$capricorn = $star[0];
$Aquarius = $star[1];
and so on

Link to comment
Share on other sites

i see you are giving all variables an array which is pointless you need to have something like this if you wants to store only one value;

$star = arrary('Capricorn','Aquarius','Pisces','Aries','Taurus','Gemini','Cancer','Leo','Virgo','Libra','Scorpio','Sagittarius');

and then you can call like this:

$capricorn = $star[0];
$Aquarius = $star[1];
and so on

 

 

Each star sign will hold several pockets of information when I have completed it.

 

$Capricorn = array("<h1>Star NAME", "<p>Star Description " <img>Releated picture")

 

I need to generate a new page on the user's star sign so if someone is Capricorn will have a heading, description and image all relating to Capricorn.

 

That is why I have created an array for each star sign. Is there a better way of doing this?

 

 

 

Link to comment
Share on other sites

<?php

$month = 12;
$year = 1985;

function star_sign($month, $year) {
$time = mktime(0, 0, 0, $month, 0, $year);
$day_of_year = date("z", $time);

if (date("L", $time) && ($day_of_year > 59))
	$day_of_year -= 1;

switch ($day_of_year) {
	case $day_of_year > 356:
		return "Capricorn";
	case $day_of_year > 326:
		return "Sagittarius";
	case $day_of_year > 296:
		return "Scorpio";
	case $day_of_year > 266:
		return "Libra";
	case $day_of_year > 235:
		return "Virgo";
	case $day_of_year > 203:
		return "Leo";
	case $day_of_year > 172:
		return "Cancer";
	case $day_of_year > 140:
		return "Gemini";
	case $day_of_year > 111:
		return "Taurus";
	case $day_of_year > 78:
		return "Aries";
	case $day_of_year > 51:
		return "Pisces";
	case $day_of_year > 20:
		return "Aquarius";
	default:
		return "Capricorn";
}
}

die(star_sign(12, 1985));

 

 

This is so much more logically than my approach flyhoney but I don't understand how to related it back to my form, and WHY I need the year 1985? Is it possible to explain what is happening? I'm a complete PHP novice..... this is the first thing I have ever tried.

 

 

Link to comment
Share on other sites

Oops! Sorry, I just wasn't paying attention, you really need the month, day AND year.

 

So, a comprehensive example:

<?php

function star_sign($month, $day, $year) {
   $time = mktime(0, 0, 0, $month, $day, $year);
   $day_of_year = date("z", $time);

   if (date("L", $time) && ($day_of_year > 59))
      $day_of_year -= 1;

   switch ($day_of_year) {
      case $day_of_year > 356:
         return "Capricorn";
      case $day_of_year > 326:
         return "Sagittarius";
      case $day_of_year > 296:
         return "Scorpio";
      case $day_of_year > 266:
         return "Libra";
      case $day_of_year > 235:
         return "Virgo";
      case $day_of_year > 203:
         return "Leo";
      case $day_of_year > 172:
         return "Cancer";
      case $day_of_year > 140:
         return "Gemini";
      case $day_of_year > 111:
         return "Taurus";
      case $day_of_year > 78:
         return "Aries";
      case $day_of_year > 51:
         return "Pisces";
      case $day_of_year > 20:
         return "Aquarius";
      default:
         return "Capricorn";
   }
}

$month = $_POST['user_month'];
$day = $_POST['user_day'];
$year = $_POST['user_year'];

echo "Your star sign is " . star_sign($month, $day, $year);

Link to comment
Share on other sites

I have tried this code and also tried to change somethings but it only ever returns "Capricorn" no matter what date I enter.

 

My HTML is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    <title>Smart Forms JS</title>
    <script type="text/javascript" src="js/smartform.js"</script>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
<h2 align="center">What your future holds</h2>
<div id="form-area">
<!--<form action="users_details.php" method="post">-->
    <form action="new.php" method="post">
    <fieldset>
    <legend>Your Personal Details</legend>
                
        <p>
            Please enter your date of birth: <br />
                <label for="user_day">Day:<input name="user_day" id="user_day" type="text" size="2" class="reqd" maxlength="2" onkeypress="return isNumberKey(event)"></label>
                <label for="user_month">Month:
                    <select name="user_month"  id="user_month" class="reqd">
                            <option value="" selected="selected">Choose a month</option>
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                            <option value="April">April</option>
                            <option value="May">May</option>
                            <option value="June">June</option>
                            <option value="July">July</option>
                            <option value="August">August</option>
                            <option value="September">September</option>
                            <option value="October">October</option>
                            <option value="November">November</option>
                            <option value="December">December</option>
                    </select>
                </label>
                <label for="user_year">Year:<input name="user_year" id="user_year" type="text" size="4" class="reqd" maxlength="4" onkeypress="return isNumberKey(event)" ></label>
        </p>
                
<p>

            <input name="submit" type="submit" />
            <input name="reset" type="reset" />
        </p>
    
    </fieldset>
</form>
</div>
</body>
</html>

 

and my php is now:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    <title>Processed Form</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>

<div id="wrapper">
    <h2>Processed Form from Birthday Page</h2>

<?php
//$Capricorn = array("Capricorn", "And so say all of us");
//$Aquarius = array("Aquarius");
//$Pisces = array("Pisces");
//$Aries = array("Aries");
//$Taurus = array("Taurus");
//$Gemini = array("Gemini");
//$Cancer = array("Cancer");
//$Leo = array("Leo");
//$Virgo = array("Virgo");
//$Libra = array("Libra");
//$Scorpio = array("Scorpio");
//$Sagittarius = array("Sagittarius");

//Change variables to multidimentional array:

$starsign[0] = array('name'=>'Capricorn', 'description' =>'description of capricorn people');
$starsign[1] = array('name'=>'Aquarius', 'description' =>'description of aquarius people');
$starsign[2] = array('name'=>'Pisces', 'description' =>'description of Pisces people');
$starsign[3] = array('name'=>'Aries', 'description' =>'description of aries people');
$starsign[4] = array('name'=>'Taurus', 'description' =>'description of Taurus people');
$starsign[5] = array('name'=>'Gemini', 'description' =>'description of Gemini people');
$starsign[6] = array('name'=>'Cancer', 'description' =>'description of Cancer people');
$starsign[7] = array('name'=>'Leo', 'description' =>'description of Leo people');
$starsign[8] = array('name'=>'Virgo', 'description' =>'description of Virgo people');
$starsign[9] = array('name'=>'Libra', 'description' =>'description of Libra people');
$starsign[10] = array('name'=>'Scorpio', 'description' =>'description of Scorpio people');
$starsign[11] = array('name'=>'Sagittarius', 'description' =>'description of Sagittarius people');

$month = $_POST['user_month'];
$day = $_POST['user_day'];
$year = $_POST['user_year'];

?>



<?php

function star_sign($month, $day, $year) {
   $time = mktime(0, 0, 0, $month, $day, $year); //return the Unix timestamp
   $day_of_year = date("z", $time);  // "z" is equal to  the day of the year 0 to 365

   if (date("L", $time) && ($day_of_year > 59)) // for leap years "L" is LEAP YEAR
      $day_of_year -= 1; // if it is FEB 29 (59) Subtract 1 from the day of year


   switch ($day_of_year) {
      case $day_of_year > 356: // above 22nd Dec = Capricorn
         return "Capricorn";
      case $day_of_year > 326:
         return "Sagittarius";
      case $day_of_year > 296:
         return "Scorpio";
      case $day_of_year > 266:
         return "Libra";
      case $day_of_year > 235:
         return "Virgo";
      case $day_of_year > 203:
         return "Leo";
      case $day_of_year > 172:
         return "Cancer";
      case $day_of_year > 140:
         return "Gemini";
      case $day_of_year > 111:
         return "Taurus";
      case $day_of_year > 78:
         return "Aries";
      case $day_of_year > 51:
         return "Pisces";
      case $day_of_year > 20:
         return "Aquarius";
      default:
         return "Capricorn";
   }

}

         echo "Your star sign is " . star_sign($month, $day, $year);
?>

 

 

I assume that it is only returning the default value in the switch statement? How do I rectify this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.