Page 1 of 1
code to change php depending on user input
Posted: Wed Sep 17, 2008 12:03 pm
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
Re: code to change php depending on user input
Posted: Wed Sep 17, 2008 2:55 pm
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
Re: code to change php depending on user input
Posted: Wed Sep 17, 2008 3:47 pm
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
Re: code to change php depending on user input
Posted: Wed Sep 17, 2008 4:08 pm
by Luke
store it in a cookie. that's what they're for

Re: code to change php depending on user input
Posted: Wed Sep 17, 2008 4:15 pm
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,
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 9:31 am
by andycharrington
any ideas anyone?
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 10:09 am
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.
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 11:10 am
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
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 11:15 am
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>
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 11:27 am
by andycharrington
hey, that looks spot on!
So can i use location as a variable?
ie-
And then use $variable in the php code?
Thank you so much for your help.
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 11:35 am
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
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
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 12:04 pm
by Jade
You're welcome.
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 12:06 pm
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.
Re: code to change php depending on user input
Posted: Thu Sep 18, 2008 2:02 pm
by andycharrington
any ideas?