You are here:  » Trying to display data for a particular time


Trying to display data for a particular time

Submitted by mrw2020 on Fri, 2009-05-29 09:31 in

Hi everyone

I wonder if anyone could point me in the right direction, I have the following code that you kindly helped me together and it works a treat:

<?php
  
require("magicParser/MagicParser.php");
  function 
myRecordHandler($item)
  {
    global 
$description;
    print 
"<option value='".htmlentities($item["TITLE"])."'>".$item["TITLE"]."</option>";
    if (
$_GET["sign"]==$item["TITLE"])
    {
      
$description = ("<h2>".($item["TITLE"])."</h2>".$item["DESCRIPTION"]);
    }
  }
  print 
"<br />";
  
$url "http://horoscopeservices.co.uk/daily_delivery/xmlaccess.asp?uid=898723781";
  print 
"<form method='GET'>";
  print 
"<select name='sign'>";
  
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  print 
"</select>";
  print 
"&nbsp;&nbsp;<input type='submit' value='Go' />";
  print 
"</form>";
  if (
$_GET["sign"])
  {
    print 
"<p>".$description."</p>";
  }
  else {
        echo 
'<p>&nbsp;</p><h2>Today\'s Forecast</h2><p>See what the stars have in store for you today, just pick your sign from the drop down list.</p>';
    }
?>

And again with the help of this forum I have been able to take that information and put it into a database with the following code:

<?php
  
require("MagicParser.php");
  
$conn =  mysql_connect("****","****","****"); // change for your server!
  
$link =  mysql_select_db("****",$conn); // change for your server
  
function myRecordHandler($record)
  {
    global 
$link;
    
//  add any more fields as required
    
$sql "INSERT INTO signs SET
              TITLE = '"
.mysql_escape_string($record["title"])."',
              DESCRIPTION = '"
.mysql_escape_string($record["description"])."'";
                             
// ^^ don't forget the last one doesn't have a comma after ^^
    
mysql_query($sql,$link);
    
// and for debugging:
    
print mysql_error();
    print 
"<br />";
  }
  
// we might want to empty the table before we start
  
$sql "TRUNCATE signs";
  
mysql_query($sql,$link);
  
// now parse the XML to load the new records
  
MagicParser_parse("http://horoscopeservices.co.uk/daily_delivery/xmlaccess.asp?uid=898723781","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
print_r($record);
    
// The following code will print out each field in your sample data:
    
print $record["TITLE"];
    print 
$record["DESCRIPTION"];
  }
  
MagicParser_parse("http://horoscopeservices.co.uk/daily_delivery/xmlaccess.asp?uid=898723781","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

What I now need to do is display the horoscope for the current star sign, for example Aries is from March 21st to April 19th so I would like to display today's horoscope for that sign - does anyone have any Ideas how I could achieve this or if it's even possible.

Any help would be greatly appreciated.

Best wishes
Madeleine

Submitted by support on Fri, 2009-05-29 10:42

Hello Madeleine,

It's certainly possible; the main requirement is a function to tell you the star sign for today's date, so I've written a neat function to do that - getStarSign() - so all that's needed is to combine that with the parse; so to print the reading for the current star sign:

<?php
  
require("MagicParser.php");
  function 
getStarSign()
  {
    
$zodiac[356] = "Capricorn";
    
$zodiac[326] = "Sagittarius";
    
$zodiac[296] = "Scorpio";
    
$zodiac[266] = "Libra";
    
$zodiac[235] = "Virgo";
    
$zodiac[203] = "Leo";
    
$zodiac[172] = "Cancer";
    
$zodiac[140] = "Gemini";
    
$zodiac[111] = "Taurus";
    
$zodiac[78]  = "Aries";
    
$zodiac[51]  = "Pisces";
    
$zodiac[20]  = "Aquarius";
    
$zodiac[0]   = "Capricorn";
    
$dayOfTheYear date("z");
    
$isLeapYear date("L");
    if (
$isLeapYear && ($dayOfTheYear 59)) $dayOfTheYear $dayOfTheYear 1;
    foreach(
$zodiac as $day => $sign) if ($dayOfTheYear $day) break;
    return 
$sign;
  }
  function 
myRecordHandler($record)
  {
    global 
$currentStarSign;
    if (
$record["TITLE"] == $currentStarSign)
    {
      print 
"<p>".$record["DESCRIPTION"]."</p>";
      return 
TRUE// stop parsing
    
}
  }
  
$currentStarSign getStarSign();
  print 
"<p>The current star sign is: ".$currentStarSign." - Here is today's reading:</p>";
  
MagicParser_parse("http://horoscopeservices.co.uk/daily_delivery/xmlaccess.asp?uid=898723781","myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

Just to describe my getStarSign() function. The $zodiac array contains the day of the year on which each star sign begins - in reverse order. Notice how Capricorn appears twice. The current day of the year is obtained using date("z"), and adjusted in the case of a leap year. A foreach loop then compares the current day with each day in the array; and when it reaches a value that is greater than the current day it exits the loop, leaving the current star sign in $sign; which is then returned by the function.

Hope this helps!
Cheers,
David.

Submitted by mrw2020 on Fri, 2009-05-29 11:02

Many, many thanks David it works a treat, now i've seen it written it makes perfect sense. My logical skills just aren't up to it.

Best wishes
Madeleine