code to change php depending on user input

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
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

code to change php depending on user input

Post by andycharrington »

Hi there,

I am trying to work out how to do the following.

I have an rss feed on my site that is location based (it's a weather feed). However i need to be able to offer multiple rss feeds for different locations.

I have all the rss feed urls for the various locations but need some way of replacing the original rss feed with the one the user selects.

for example. The default rss feed is for Birmingham. However i want to give the user the option to select London for example. When they choose it i need the code that specifies the rss feed url to change to the london rss feed url.

I hope that makes sense.

If any one has any ideas they would be greatly appreciated.

Thanks very much,

Andy
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: code to change php depending on user input

Post by andyhoneycutt »

Are you storing user preferences anywhere? If you are already, add this to their preferences. For a temporary solution you can store their selection in the $_SESSION for the user, or store it in a cookie. You'll need to modify your php that hands them the feed to give them the proper one.

-Andy
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

No i'm not saving the info anywhere. The site is not running from a database. When i said users i meant anyone visiting the site.

How would i enable them to change the feed?

Thanks for your help
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: code to change php depending on user input

Post by Luke »

store it in a cookie. that's what they're for ;)
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

sorry. I'm obviously not aking myself clear.

I am not interested in saving or preserving the users location.

This is what i want to happen.

Someone comes to the site. They choose from a list of predefined locations. Their choice changes the rss feed url.

Saving it is not a problem at the moment.

I just want to know how to implement the changing of the rss feed url.

Thank you very much,
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

any ideas anyone?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: code to change php depending on user input

Post by Jade »

You need to use ajax if you don't want the page to refresh. Setup the drop down box or button (whatever you're using) to trigger the request to load the appropriate url. Otherwise assign the different locations a specific url and load it based on that.
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

i don't mind the page refreshing.

I have been looking in to it and am i right in thinking that i can do it with a drop down box. Each option having a value (which will be the new url).

My problem is then assigning the submitted option to a php variable...........

How would i do it?

Thanks for your help
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: code to change php depending on user input

Post by Jade »

Code: Select all

 
<?php
if ($_POST['location'])
   echo "You selected: " . $_POST['location'];
?>
 
<form action="#" method="post">
Select a Location: 
<select name="location">
  <option value="http://url1.com">Location 1</option>
  <option value="http://url2.com">Location 2</option>
<option value="http://url3.com">Location 3</option>
</select>
<input type="submit" name="submit" value="Change Location" />
</form>
 
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

hey, that looks spot on!

So can i use location as a variable?

ie-

Code: Select all

<? php
$variable=location
?>
And then use $variable in the php code?

Thank you so much for your help.
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

basiacally here's what ive got -

Code: Select all

<?php 
function retrieveYahooWeather($zipCode="UKXX0018") {
    $yahooUrl = "http://weather.yahooapis.com/forecastrss";
    $yahooZip = "?p=$zipCode&u=c";
    $yahooFullUrl = $yahooUrl . $yahooZip; 
    $curlObject = curl_init();
    curl_setopt($curlObject,CURLOPT_URL,$yahooFullUrl);
    curl_setopt($curlObject,CURLOPT_HEADER,false);
    curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
    $returnYahooWeather = curl_exec($curlObject);
    curl_close($curlObject);
    return $returnYahooWeather;
}
$localZipCode = "UKXX0018"; 
$weatherXmlString = retrieveYahooWeather($localZipCode);
$weatherXmlObject = new SimpleXMLElement($weatherXmlString);
$currentCondition = $weatherXmlObject->xpath("//yweather:condition");
$currentTemperature = $currentCondition[0]["temp"];
$currentDescription = $currentCondition[0]["text"];
$currentImage = $currentCondition[0]["code"];
$currentLocation = $weatherXmlObject->xpath("//yweather:location");
$currentCity = $currentLocation[0]["city"];
?>
Where it says

Code: Select all

function retrieveYahooWeather($zipCode="UKXX0018") {
and

Code: Select all

$localZipCode = "UKXX0018";
i need to be able to change that value (the UKXX0018 one) to a different code depending on the users selection.

This way i can give the user the weather in their area rather than just in one place.

Thanks
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: code to change php depending on user input

Post by Jade »

You're welcome.
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

I still cant quite work out how to implement it though....

Did you see my previous post. Does it makes sense?

How would i use the value submitted through the drop down box to the code defining the rss feed.

Cheers.
andycharrington
Forum Newbie
Posts: 9
Joined: Wed Sep 17, 2008 11:59 am

Re: code to change php depending on user input

Post by andycharrington »

any ideas?
Post Reply