Basic PHP variable problem

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
aidoDel
Forum Newbie
Posts: 7
Joined: Mon Nov 23, 2009 11:32 am

Basic PHP variable problem

Post by aidoDel »

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.

Code: Select all

<?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 the user_month to be $Capricorn so I can call it later.

I have no clue how to go about doing this.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Basic PHP variable problem

Post by cpetercarter »

It might be a good idea to have a single array for your star signs.

Code: Select all

$starsign[0] = array('name'=>'Capricorn', 'description' =>'description of capricorn people');
$starsign[1] = array (....etc....
 
Then, see if you can devise a way of representing the birthday as a single number (number of days since the beginning of the year). You can then calculate the right star sign like this:

Code: Select all

 
if ($days < 20) $group = 0;
elseif ($days >= 20 AND $days > .....) $group = 1;
//..etc to the end of the astrological year 
$user_starsign = $starsign[$group]['name'];
$description = $starsign[$group]['description'];
 
Does this help?
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Basic PHP variable problem

Post by iankent »

cpetercarter wrote:It might be a good idea to have a single array for your star signs.
To make things easier on yourself you could precalculate the position of each starsign. E.g., as part of your starsign array include the day of the year (0-365) that the starsign starts and ends with. You can then calculate the number of days from beginning of the year and compare with the values in the array to find the matching starsign. Much easier than lots of nested if{} statements
aidoDel
Forum Newbie
Posts: 7
Joined: Mon Nov 23, 2009 11:32 am

Re: Basic PHP variable problem

Post by aidoDel »

I have changed my code to:

Code: Select all

<?php
$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'];
 
 
$user_starsign = $starsign[$group]['name'];
$description = $starsign[$group]['description'];
 
 
 
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 get the idea of having a multi-dimensional array.

I have also removed the "if" statements to a "swtich" but it is not working correctly for me. I have two problems:
1) no matter what I enter into my form page it will only ever return "capricorn"
2) In my function I have got it to return a string rather than relate this back to the starsign array. How do I go about changing this?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Basic PHP variable problem

Post by cpetercarter »

Have a look at this.

You need to write each case statement like this

Code: Select all

 
case ($day_of_year > 356) :
     return 0;
     break;
//etc
 
If you return the appropriate integer instead of the name of the starsign, then you can use the integer to access the array of starsign names and descriptions.

Code: Select all

 
$group = star_sign ($month, $day, $year);
echo "Your star sign is ".$starsign[$group]['name']."\n";
echo $starsign[$group]['description'];
 
Post Reply