Jump to content

sandeep529

Members
  • Posts

    80
  • Joined

  • Last visited

    Never

Posts posted by sandeep529

  1. Will this work...?

     

    This uses the class I mentioned in previous message....

     

    $blog = array(  array('name'=>'entry 1','title'=>'title 1','type'=>'misc')  , array('name'=>'entry 2','title'=>'title 2','type'=>'misc2','anotherEntry'=>'entry content')          );
    
    include 'crXml.php';
    $crxml = new crxml;
    
    foreach($blog as $c => $entry) {
    foreach($entry as $k=>$v) {
    $crxml->item[$c]->$k = $v;
    }
    }
    echo $crxml->xml();

     

    Outputs 

     

    <?xml version="1.0" encoding="UTF-8"?>
    <item>
      <name>entry 1</name>
      <title>title 1</title>
      <type>misc</type>
    </item>
    <item>
      <name>entry 2</name>
      <title>title 2</title>
      <type>misc2</type>
      <anotherEntry>entry content</anotherEntry>
    </item>

     

    Just replace the blog array with any content from your blog.....

     

  2. Do you want to provide an rss feed to the user, or you want to use data from external RSS feeds to be displayed in your site?

     

    RSS is basically xml so what you will need to do will be XML parsing or XML generation...you can try SimpleXML or DOM XML functions...

     

    There is also a class I have created for xml parsing/generation/manipulation called crXml, the usage of which I have outlined in the thread

     

    http://www.phpfreaks.com/forums/index.php?topic=351131.msg1657792#msg1657792

     

     

  3. hi,

     

    Your XML contains a bit of namespaces, So if you find simpleXML difficult, you can try using a class I created. Which makes  accessing CDATA sections and namespaces easy/

     

    http://crxml.pagodabox.com/

     

    You can download the archive at 

    http://crxml.pagodabox.com/crxml.tar

     

    The advantage of this class is that you have a search function. You can call it with a name 'visibility' and it will return the php statement using this class that you can use to access the value of that node.

     

    for example after loading your xml into the class , the statemment

     

    var_dump($crxml->search('visibility'));

     

    will output  array with 1 element.

     

    array(1) {
      [0]=>
      array(7) {
        ["nodeName"]=>
        string(10) "visibility"
        ["accessStatement"]=>
        string(85) "...->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility"
        ["nodeChildrenCount"]=>
        int(1)
        ["nodeType"]=>
        int(1)
        ["namespaceURI"]=>
        NULL
        ["nodeContent"]=>
        string(52) "<visibility units="statute miles">10.00</visibility>"
        ["htmlEncodedNodeContent"]=>
        string(74) "<visibility units="statute miles">10.00</visibility>"
      }
    }

     

    In the above section there is a "accessStatement" element....this contains value "->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility"

     

    so to access the value of that node from php you can use

     

    echo $crxml->->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility;

     

    will echo 10.

     

    to get the 'Units' attribute for visibility  you can give as

     

    echo $crxml->dwml->data[1]->parameters->weather->{'weather-conditions'}[1]->value->visibility['units']

     

    will echo 'statute miles'...

     

    This way you dont have to manually find your way through the XML.  What ever you can do with simpleXML like iterating, You can do with this class also.

     

    I have to warn you that even though I have somewhat tested and is activity developing this class, I am the only one working on this.

     

    So you might want to try simplexml first. Even though it seems a bit hard to me, people have been using it for long, and there will be solutions in the net for what ever issue you come up with.

     

    Or try using DOM XML functions. Its What I have used to make the above class. I Have found it much more consistent and powerful.

     

    If you use DOM function, you better take a look at XPath also http://www.tizag.com/xmlTutorial/xpathtutorial.php.

     

    If you choose to use simpleXML pls see the link

     

    http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html

     

    That is not meant to scare you. But it shows how to access xml nodes when namespaces are involved

     

    Good Luck.....

  4. I still don't understand why you don't like to use strtotime()? It works fine and it doesn't matter if you have preceding zeros or not :)

     

    Check it, for example

    var_dump( strtotime( '2012-01-05' ) < strtotime( '2012-1-4' ) );

     

    I love strtotime()..its a great function. I was just curious  if string comparision will work under any circumstances. I nevertheless always use date functions for all date/time comparisons

  5. I have created this short function for formatting xml strings using recursion and anonymous function. Can you guys check and tell me if it is really working...?

     

    Also if this can be done in a shorter way?

     

    Please avoid the xml version tags and the style sheet declarations for now, as it would break the function. I have included a sample xml in the code posted below...

     

    <?php 
    $xmlstr = <<<EOB
    <Item xmlns="http://uriofthenamespace.com" Type="Book"><Name>Freedom at Midnight</Name><Authors><Author><FirstName>Collins</FirstName><LastName>Larry</LastName></Author><Author><FirstName>Lapierre</FirstName><LastName>Dominique</LastName></Author></Authors><Price><![CDATA[150.00]]></Price></Item>
    EOB;
    function indent($str,$il=0,&$count=null)
    {
    $indentChar = "  ";
    $str = preg_replace("#>\s*(\w|<)#",">\\1",$str);
    $replacer = function($str) use($il,$indentChar)
    {
    $str[3] = trim($str[3]);
    $count = 0;
    if(substr($str[3],0,9)!='<![CDATA[') $str[3] = indent($str[3],$il+1,$count);
    $n=$count?"":"\n$indentChar";
    $str[3] = str_replace("\n","\n".str_repeat($indentChar,$il),$str[3]);
    return "\n{$str[1]}$n{$str[3]}\n</{$str[2]}>";
    };
    $return = preg_replace_callback("#(<(\w*?).*>)(.*)</\\2>#Uism",$replacer,$str,-1,$count);
    if($il==0) 
    return preg_replace("#(<\w*/>)([^<]+)\n(\s*)<#Uism","\\1\n\\3$indentChar\\2\\3\n\\3<",preg_replace("#(<\w*/>)\n(\s*)(\S)#Uism","\n$indentChar\\2\\1\n\\2\\3",$return));
    else 
    return $return;
    }
    
    echo indent($xmlstr);

  6. Hi,

     

    The procedure I am outlining here is Just my Idea. I need someone to confirm its reliability...

     

    The best way to handle the situation would be to use Unix Timestamps. This is because unix timestamps are timezone independent.

     

    This means when some thing is to happen at a timestamp of X, then it has to happen when X seconds have passed since 1970-01-01 00:00:00 at greenwich.

     

    In otherwords servers running in different parts of the world running in different timezones always return the same unix timestamp.

     

     

    echo date_create('2011-12-30 00:00:00 GMT')->format('U');echo "<br/>";
    echo date_create('2011-12-30 05:30:00 GMT+5:30')->format('U');echo "<br/>";

     

    So if you want a user in timezone GMT+5:30 (India) to recieve a notification at  say Jan 15 2012 00:00:00, you have to find what is the unix timestamp when a clock in india shows time Jan 15 2012  00:00:00;  can be done as...

     

    $timestamp_one = date_create('2012-01-15 00:00:00 GMT+5:30')->format('U');

     

    Insert that value in the database.

     

    You can also insert the value of unix timestamp when a clock in GMT timezone shows  Jan 15 2012  00:00:00; can be done as

     

    $timestamp_two =gmmktime(0,0,0,1,15,2012);

     

    This is for handling the second one in the following situtations...

     

    For querying.

     

    Situtation 1:

     

    1.Your server clock shows 2012-01-15 6:24:34

    2.You have a user in database with notification timestamp_one, $user_notification_timestamp = 1326607200.Assume that this timestamp is a hour boundary like 5:00:00 ...and not like    5:34:34...

    3. You have to check if the user has to be notified

     

    You have to do the following 

     

    $my_last_hour_timestamp = mktime(6,0,0,1,15,2012);   //returns  the unix timestamp when your server clock was at 2012-01-15 6:00:00
    if($my_last_hour_timestamp == $user_notification_timestamp) send_user_notification();

     

     

    Situtation 2:

     

    1.Your want to send a notification to all users with a notification time of  2012-01-25  9:00:00/their time  right now....

    2.You have a user in database with notification timestamp_two ,$user_notification_timestamp_two = 1326607200.Assume that this timestamp is a hour boundary like 5:00:00 ...and not like  5:34:34...

     

    You have to do the following 

     

    $timestamp = gmmktime(9,0,0,1,25,2012);   //returns  the unix timestamp when a C LOCK AT GMT is at 2012-01-25 9:00:00
    if($timestamp == $user_notification_timestamp_two)   send_user_notification();

     

     

    I dont know if this is the best method..So You may want to  wait till someone else reply....

     

    Good Luck....

     

     

     

     

×
×
  • 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.