1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Convert Small PHP code

Discussion in 'PHP' started by sonu21, Aug 8, 2012.

  1. #1
    <?php
    $xml = simplexml_load_file('http://www.google.com/ig/api?weather=Jaipur-Rajasthan');
    $information = $xml->xpath("/xml_api_reply/weather/forecast_information");
    $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
    $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
    ?>
    <html>
    <head>
    <title>Google Weather API</title>
    </head>
    <body>
    <h1><?php echo $information[0]->city['data']; ?></h1>
    <h2>Today's weather</h2>
    <div>
    <img src="<?php echo 'http://www.google.com' . $current[0]->icon['data']?>" />
    <div>
    <?php echo $current[0]->temp_c['data'] ?>&deg; C,
    <?php echo $current[0]->condition['data']; ?>
    </div>
    </div>
    </body>
    </html>

    Help Appreciated!

    Thanks!
     
    sonu21, Aug 8, 2012 IP
  2. setyp

    setyp Member

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    38
    #2
    What's the matter? Your code works: 8087acfffa80a9673389f25d473532db.png
     
    setyp, Aug 8, 2012 IP
  3. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    Yes,I know but I need to convert it to smarty template.Can you help me?

    Thanks!
     
    sonu21, Aug 8, 2012 IP
  4. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Since you're talking about Smarty, I presume you're loading the template from a regular PHP file somewhere. In this PHP-file, you should do all your "logic", and then assign the variables for output in the template file.
    In your original PHP-file, you should do something like this (assuming Smarty is initialized to the variable named $smarty):
    
    $xml = simplexml_load_file('http://www.google.com/ig/api?weather=Jaipur-Rajasthan'); 
    $information = $xml->xpath("/xml_api_reply/weather/forecast_information"); 
    $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); 
    $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); 
    
    $smarty->assign(array(
        'city' => $information[0]->city['data'],
        'temp' => $current[0]->temp_c['data'],
        'condition' => $current[0]->condition['data']
    ));
    
    PHP:
    In your template file, you would then do like this:
    
    <html> 
        <head> 
            <title>Google Weather API</title> 
        </head> 
        <body> 
            <h1>{$city}</h1> 
            <h2>Today's weather</h2> 
            <div> 
                <img src="http://www.google.com{$icon}" /> 
                <div> 
                    {$temp}&deg; C, 
                    {$condition}
                </div> 
            </div> 
        </body> 
    </html>
    
    HTML:
    Makes sense? :)
     
    Last edited: Aug 9, 2012
    Soulstone, Aug 9, 2012 IP
  5. sonu21

    sonu21 Member

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    Hi SoulStone,Thanks for your reply.But,this is not executing the PHP function.I had make 1 .tpl file name as weather.tpl & include it in another .tpl file.Do you have any solution for this.

    Thanks!
     
    sonu21, Aug 9, 2012 IP
  6. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Ah okay. I guess you could do like this:
    
    {php}
    $xml=simplexml_load_file('http://www.google.com/ig/api?weather=Jaipur-Rajasthan');
    $information=$xml->xpath("/xml_api_reply/weather/forecast_information");
    $current=$xml->xpath("/xml_api_reply/weather/current_conditions");
    $forecast_list=$xml->xpath("/xml_api_reply/weather/forecast_conditions");
    
    $this->assign(array(
    'city'=>$information[0]->city['data'],
    'temp'=>$current[0]->temp_c['data'],
    'condition'=>$current[0]->condition['data']
    ));
    {/php}
    <html> 
        <head> 
            <title>Google Weather API</title> 
        </head> 
        <body> 
            <h1>{$city}</h1> 
            <h2>Today's weather</h2> 
            <div> 
                <img src="http://www.google.com{$icon}" /> 
                <div> 
                    {$temp}&deg; C, 
                    {$condition}
                </div> 
            </div> 
        </body> 
    </html>
    
    HTML:
     
    Soulstone, Aug 9, 2012 IP
  7. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Using those {php}{/php}-tags, you can "embed" PHP directly into the template file. This is not very good practice though, but might be the only solution here.
    You still have to remember to "assign" the variables though.
     
    Soulstone, Aug 9, 2012 IP