code to change php depending on user input
Moderator: General Moderators
-
andycharrington
- Forum Newbie
- Posts: 9
- Joined: Wed Sep 17, 2008 11:59 am
code to change php depending on user input
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
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
- 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
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
-Andy
-
andycharrington
- Forum Newbie
- Posts: 9
- Joined: Wed Sep 17, 2008 11:59 am
Re: code to change php depending on user input
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
How would i enable them to change the feed?
Thanks for your help
Re: code to change php depending on user input
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
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,
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
any ideas anyone?
Re: code to change php depending on user input
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
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
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
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
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.
So can i use location as a variable?
ie-
Code: Select all
<? php
$variable=location
?>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
basiacally here's what ive got -
Where it says
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
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"];
?>Code: Select all
function retrieveYahooWeather($zipCode="UKXX0018") {Code: Select all
$localZipCode = "UKXX0018";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
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
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.
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