7 Replies - 236 Views - Last Post: 05 February 2012 - 01:13 PM Rate Topic: -----

Topic Sponsor:

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 02:24 AM

I'm trying to make my function more versatile by removing hard coded data.

function get_currency_rate($currencySource) {
    
if (isset($currencySource)) {
        $feed = $currencySource;
    } else {
        echo 'Feed not found.  Check URL';
    }

    if (!$feed) {
        echo('Feed not found');
    }


    $xml = new SimpleXmlElement($feed);

    $rate = get_rate($xml, 15); //EUR
    if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml') {
        $rate = get_rate($xml, 16); //GBP
    } else {
        $rate = get_rate($xml, 56);  //USD
    }
}


I would prefer to pass in the values 15, 16, 56 and the URL 'http://themoneyconverter.com/rss-feed/USD/rss.xml' rather than hard coding them into the function.

Below is the get_rate function.

// Get and return currency rate
// Perform regular expression to extrat numeric data
// Split title string to extract currency title 
function get_rate(SimpleXMLElement $xml, $xmlTagPosition) {

    $currency['rate'] = $xml->channel->item[$xmlTagPosition]->description;
    preg_match('/([0-9]+\.[0-9]+)/', $currency['rate'], $matches);
    $rate = $matches[0];

    $title['rate'] = $xml->channel->item[$xmlTagPosition]->title;
    $title = explode('/', $title['rate']);
    $title = $title[0];

    echo $rate . ' ' . $title . '<br />';
}


The feed URL's are set in my cityConfig.php script as below:

// Feed URL's //
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';

// Define arrays // 
$cities = array('London', 'New York', 'Paris');
$currencyHeadings = array('London - 1 British Pound Sterling', 'New York - 1 US Dollar', 'Paris - 1 Euro');
$currencySource = array($theMoneyConverter . 'GBP/rss.xml', $theMoneyConverter . 'USD/rss.xml', $theMoneyConverter . 'EUR/rss.xml');
$currencyId = array(15, 16, 56); 
?>


Notice the $currencyId array values are the same as those hard coded into the get_currency_rate function. I tried passing in a second parameter for the $currencyId to no avail. Any ideas where I'm going wrong?

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to remove hard coded data from a function in PHP

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 10:10 AM

So then you want to parse the url:
<?php
$url = "http://www.somesite.com?x=15&y=15&z=56";
$vars = parse_url($url,PHP_URL_QUERY);
parse_str($vars);
echo "x = ".$x."<br>y = ".$y."<br>z = ".$z;
?>


Was This Post Helpful? 0
  • +
  • -

#3 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Re: Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 11:48 AM

View PostCTphpnwb, on 05 February 2012 - 10:10 AM, said:

So then you want to parse the url:
<?php
$url = "http://www.somesite.com?x=15&y=15&z=56";
$vars = parse_url($url,PHP_URL_QUERY);
parse_str($vars);
echo "x = ".$x."<br>y = ".$y."<br>z = ".$z;
?>



It's currently displaying nothing but I can echo the value out the variable $x

$currencySource = array($theMoneyConverter . 'GBP/rss.xml?x=15',


function get_currency_rate($feed) {

    $xml = new SimpleXmlElement($feed);

    $rate = get_date($xml);
    echo $currency['date'] . '<br />';

    $vars = parse_url($feed, PHP_URL_QUERY);
    parse_str($vars);
//    echo "x = " . $x;
    
    $rate = get_rate($xml, $x); //EUR

This post has been edited by NewToJava2011: 05 February 2012 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 876
  • View blog
  • Posts: 2,250
  • Joined: 15-February 11

Re: Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 12:07 PM

Your variable $x is NULL. If parse_str doesn't get a second parameter is places the results of the parsed string within $arr.

Either pass $x as a second parameter to parse_str or pass $arr as the second parameter for get_rate.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 12:09 PM

I don't know what you're talking about. Show complete, functioning code that demonstrates the problem. Is $currencySource being passed to get_currency_rate? If so then $feed is an array and your function makes no sense.
Was This Post Helpful? 0
  • +
  • -

#6 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Re: Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 12:41 PM

View PostCTphpnwb, on 05 February 2012 - 12:09 PM, said:

I don't know what you're talking about. Show complete, functioning code that demonstrates the problem. Is $currencySource being passed to get_currency_rate? If so then $feed is an array and your function makes no sense.


Complete code as requested.

CityConfig.php script stores the URL's of each city

// Feed URL's //
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';

// Define arrays // 
$cities = array('London', 'New York', 'Paris');
$currencySource = array($theMoneyConverter . 'GBP/rss.xml?x=15', $theMoneyConverter . 'USD/rss.xml?x=16', $theMoneyConverter . 'EUR/rss.xml?x=56');
?>


Notice I have added the argument ?x=15 , ?x=16 and ?x=56 to the corresponding URL's e.g. ?x=15 corresponds to GBP.

The currency URL's are then passed into a separate script, getCurrencyRate.php. The feed URL's are passed into the get_currency_xml function.

function get_currency_xml($currencySource) {

    if (isset($currencySource)) {
        $feed = $currencySource;
    } else {
        echo 'Feed not found.  Check URL';
    }

    if (!$feed) {
        echo('Feed not found');
    }

    return $feed;
}


The $feed variable created from this function is passed into the same getCurrencyRate.php script but to another function as below.

function get_currency_rate($feed) {

    $xml = new SimpleXmlElement(uwe_get_file($feed));
    
    $vars = parse_url($feed, PHP_URL_QUERY);
    parse_str($vars);
    //echo "x = " . $x;

    $rate = get_rate($xml, $x); //$x has replaced number 15 here
    if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml') {
        $rate = get_rate($xml, $x); //$x has replaced number 16 here
    } else {
        $rate = get_rate($xml, $x);  //$x has replaced number 15 here
    }
}


Notice that I have included the $x as a parameter into each of the $rate = get_rate calls. Previously these $x variables where replaced as numbers which I have commented out above. These numbers correspond to the position of the <item> XML tag once placed into an array from the original feed URL's as set in cityConfig.php. As mentioned, the code worked fine with the hard coded values 15,16, and 56 in but I wanted to remove the hard coded variables

The call to get_rate function is as follows and also located in the getCurrencyRate.php script:

// Get and return currency rate
// Perform regular expression to extrat numeric data
// Split title string to extract currency title 
function get_rate(SimpleXMLElement $xml, $x) {

    $currency['rate'] = $xml->channel->item[$x]->description;
    preg_match('/([0-9]+\.[0-9]+)/', $currency['rate'], $matches);
    $rate = $matches[0];

    $title['rate'] = $xml->channel->item[$x]->title;
    $title = explode('/', $title['rate']);
    $title = $title[0];

    echo $rate . ' ' . $title . '<br />';
}


Hope this is clearer. Thanks

This post has been edited by NewToJava2011: 05 February 2012 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 12:57 PM

That's as clear as mud. Which block of code can I copy/paste into a file and use without modification to see your issue?
Was This Post Helpful? 0
  • +
  • -

#8 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Re: Trying to remove hard coded data from a function in PHP

Posted 05 February 2012 - 01:13 PM

View PostCTphpnwb, on 05 February 2012 - 12:57 PM, said:

That's as clear as mud. Which block of code can I copy/paste into a file and use without modification to see your issue?



Well, I use this function to call the contents of get_currency_rate

function displayCurrencyRateContent($currencySource) {

    if (isset($currencySource)) {
        echo get_currency_rate($currencySource) . '<br />';
    } else {
        echo 'Sorry.  This content is currently unavailable';
    }
}


and this to display the above function on index.php:

<ul class="columns">
            <li class="col1">
                <?php
                displayColumnContent($currencySource);
                ?>
            </li>
        </ul>

This post has been edited by NewToJava2011: 05 February 2012 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1