PHP - Beginner Needing Help With Json Script
So im trying to use a script to fetch the weather forecast from weatherunderground and im using their weather API functionality.
Basically im trying to get the output to be something like (Continuing down in a list) (Day of week): (Current conditions) (High temp)-(Low temp) this is what I have: Code: [Select] #!/usr/bin/php <?php $json_string = file_get_contents("http://api.wunderground.com/api/MY_APIKEY/forecast/q/19104.json"); $parsed_json = json_decode($json_string); $day = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'date'}->{'weekday_short'}; $high = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'high'}->{'fahrenheit'}; $low = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'low'}->{'fahrenheit'}; $conditions = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'conditions'}; echo "Forecast:\n"; echo "${day}: ${conditions} ${high}-${low}\n"; ?> the output of the get_file_contents is: Code: [Select] "response": { "version": "0.1", "termsofService": "http://www.wunderground.com/weather/api/d/terms.html", "features": { "forecast": 1 } }, "forecast": { "txt_forecast": {}, "simpleforecast": { "forecastday": [ { "date": { "epoch": "1330398000", "pretty": "10:00 PM EST on February 27, 2012", "day": 27, "month": 2, "year": 2012, "yday": 57, "hour": 22, "min": "00", "sec": 0, "isdst": "0", "monthname": "February", "weekday_short": "Mon", "weekday": "Monday", "ampm": "PM", "tz_short": "EST", "tz_long": "America/New_York" }, "period": 1, "high": { "fahrenheit": "59", "celsius": "15" }, "low": { "fahrenheit": "36", "celsius": "2" }, "conditions": "Partly Cloudy", "icon": "partlycloudy", "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "skyicon": "mostlysunny", "pop": 0, "qpf_allday": { "in": 0, "mm": 0 }, "qpf_day": { "in": 0, "mm": 0 }, "qpf_night": { "in": 0, "mm": 0 }, "snow_allday": { "in": 0, "cm": 0 }, "snow_day": { "in": 0, "cm": 0 }, "snow_night": { "in": 0, "cm": 0 }, "maxwind": { "mph": 9, "kph": 14, "dir": "South", "degrees": 180 }, "avewind": { "mph": 6, "kph": 10, "dir": "South", "degrees": 171 }, "avehumidity": 65, "maxhumidity": 78, "minhumidity": 48 }, { "date": { "epoch": "1330484400", "pretty": "10:00 PM EST on February 28, 2012", "day": 28, "month": 2, "year": 2012, "yday": 58, "hour": 22, "min": "00", "sec": 0, "isdst": "0", "monthname": "February", "weekday_short": "Tue", "weekday": "Tuesday", "ampm": "PM", "tz_short": "EST", "tz_long": "America/New_York" }, "period": 2, "high": { "fahrenheit": "50", "celsius": "10" }, "low": { "fahrenheit": "37", "celsius": "3" }, "conditions": "Clear", "icon": "clear", "icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif", "skyicon": "mostlysunny", "pop": 0, "qpf_allday": { "in": 0, "mm": 0 }, "qpf_day": { "in": 0, "mm": 0 }, "qpf_night": { "in": 0, "mm": 0 }, "snow_allday": { "in": 0, "cm": 0 }, "snow_day": { "in": 0, "cm": 0 }, "snow_night": { "in": 0, "cm": 0 }, "maxwind": { "mph": 8, "kph": 13, "dir": "NW", "degrees": 318 }, "avewind": { "mph": 5, "kph": 8, "dir": "ESE", "degrees": 123 }, "avehumidity": 53, "maxhumidity": 69, "minhumidity": 39 }, { "date": { "epoch": "1330570800", "pretty": "10:00 PM EST on February 29, 2012", "day": 29, "month": 2, "year": 2012, "yday": 59, "hour": 22, "min": "00", "sec": 0, "isdst": "0", "monthname": "February", "weekday_short": "Wed", "weekday": "Wednesday", "ampm": "PM", "tz_short": "EST", "tz_long": "America/New_York" }, "period": 3, "high": { "fahrenheit": "43", "celsius": "6" }, "low": { "fahrenheit": "41", "celsius": "5" }, "conditions": "Rain", "icon": "rain", "icon_url": "http://icons-ak.wxug.com/i/c/k/rain.gif", "skyicon": "cloudy", "pop": 80, "qpf_allday": { "in": 0.71, "mm": 18 }, "qpf_day": { "in": 0.5, "mm": 12.7 }, "qpf_night": { "in": 0.21, "mm": 5.3 }, "snow_allday": { "in": 0, "cm": 0 }, "snow_day": { "in": 0, "cm": 0 }, "snow_night": { "in": 0, "cm": 0 }, "maxwind": { "mph": 16, "kph": 26, "dir": "ESE", "degrees": 106 }, "avewind": { "mph": 9, "kph": 14, "dir": "ESE", "degrees": 115 }, "avehumidity": 91, "maxhumidity": 95, "minhumidity": 64 }, { "date": { "epoch": "1330657200", "pretty": "10:00 PM EST on March 01, 2012", "day": 1, "month": 3, "year": 2012, "yday": 60, "hour": 22, "min": "00", "sec": 0, "isdst": "0", "monthname": "March", "weekday_short": "Thu", "weekday": "Thursday", "ampm": "PM", "tz_short": "EST", "tz_long": "America/New_York" }, "period": 4, "high": { "fahrenheit": "54", "celsius": "12" }, "low": { "fahrenheit": "32", "celsius": "0" }, "conditions": "Overcast", "icon": "cloudy", "icon_url": "http://icons-ak.wxug.com/i/c/k/cloudy.gif", "skyicon": "cloudy", "pop": 0, "qpf_allday": { "in": 0, "mm": 0 }, "qpf_day": { "in": 0, "mm": 0 }, "qpf_night": { "in": 0, "mm": 0 }, "snow_allday": { "in": 0, "cm": 0 }, "snow_day": { "in": 0, "cm": 0 }, "snow_night": { "in": 0, "cm": 0 }, "maxwind": { "mph": 10, "kph": 16, "dir": "WSW", "degrees": 253 }, "avewind": { "mph": 4, "kph": 6, "dir": "West", "degrees": 280 }, "avehumidity": 95, "maxhumidity": 100, "minhumidity": 87 } ] } } } I feel like i need to define different variables for each day like dayx, highx, lowx, conditionsx, etc where x=period and it increases the period every time you run through. I remember learning this in my intro to java class but in all honesty i forgot, even if i did know if i was on the right path. Any help? I'd appreciate it. Thanks -Koz Similar Tutorialsi am brand new to php and thought a project looking at the weather undergound api would be a great place to "cut my teeth" and i am following up on a recent post i saw here http://www.phpfreaks.com/forums/index.php?topic=354658.msg1675207#msg1675207 the responder excellently described how to use the foreach function however i am not sure this is right for my application the project i am trying to accomplish is defining the freeze thaw cycle for the next ten days which i can accomplish in excel but thought this is a good way to learn to code. so my question is how should i do this, i have tried and failed with trying to pick out individual highs and lows data and define them as above or below freezing but i can't figure out how to pick out the right number my code for this part of the project is $json_line = file_get_contents("http://api.wunderground.com/api/{key}/geolookup/forecast10day/q/$zipcode.json"); $parsed_line = json_decode($json_line); echo $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'date'}->{'weekday'}; $temp_h = $parsed_line->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'high'}->{'farenheit'}; $temp_l = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}->{'low'}->{'farenheit'}; echo "Forecast for ${day} is:\n"; echo "High Temp is ${temp_h}<br />"; echo "Low Temp is ${temp_l}\n"; another road i was looking at going down was trying to do an array shift any help would be greatly appreciated thanks jeremy So ive created a script sometime back for entering movies into my database, i simply fill in the movie title, URL for it and then from a drop down menu i select what category from the catagories table that it fits in. eventually after time some of the movies need to be placed in a different category. currently the only way to do this is actually go into myphpadmin and edit within the tables. obviously way too time consuming. so im seeking to create a script that would allow me to select any given movie from my movies table (via dropdown maybe) and then change what category its assigned too, maybe also in a dropdown. heres the table structure. Movies Table ID - Title - Category - URL ---------------------------------------------------- 1 - Movie 1 - 18 www..com Categories Table ID - Category Name -------------------------------- 18 - Category 1 Note that the FK in movies table is Category. in my previous script from entering the movies i have it made so that when selecting the category it shows the category name instead of the ID. i hope that the same could be possible for this new script. If its not too much trouble i hope that someone could help me out on this. im totally clueless where too start. thanks! Hi! I have a mysql database with a lot of emails there and I want to send an email to all the email addresses that are on my database.. There is like 7000 email addresses so could it be possible to set a interval time between each email sending? If it helps i'm using Linux / Debian 5 32bits Apache2 - php5 and php5-mysql Well I have made a form in html and i need the information which is entered into the form to be shown to the user in a different page so I have a Ticket request page and i have made a Confirmation_Page.php Ticket request page: has this <form id="form1" name="form1" method="get" action="Confirmation_page.php"> The ticket request page has (Just one example so eg. the forename: </p> <p> <label>Forename <input type="text" name="Forname" id="Forname" /> </label> </p> <p> And corresponding to this in the Confirmation_Page.php it is : Your forename is : <?php echo $_GET["Forename"];?><br /> Now the thing is when I enter info in the form inside the ticket request page and click submit It takes me to the Confirmation_Page.php HOWEVER It doesn't show me the information I've inputed it into the form Eg.It only shows Your forename is : please help me I need to hand this in by monday :( thanks to everyone who tries to help me :l Hi guys! I'm new to PHP and I need help from you to create a simple form with checkboxes. Experts here will be able to solve it easily. I'm posting a part of the code below. form.html Code: [Select] Interests<br /> Sports<input type="checkbox" name="interests[]" value="Sports" /> <br /> Reading<input type="checkbox" name="interests[]" value="Reading" /> <br /> Surfing<input type="checkbox" name="interests[]" value="Surfing" /> <br /> output.php Code: [Select] <?php $inter = $_POST['interests']; ?> I read somewhere that it is now submitted as an array to to the output.php file but I don't know how to play with it.I want php to out put the values of $inter in the following manner. "You are interested in {$inter}." Please help me. Is this a very complicated code to write? Hello, I have checked out many of the scripts and tried implementing them to help me scrape 1 single image from a url. Example www.123.com/333.png Getting a script to scrape that image isnt the problem. Im not sure on how to implement the simple curl to save the image every 30mins and name it in successive order so it appears as , 1.jpg, 2.jpg, 3.jpg I am working with a debian 6 server and php would be the easiest way to do this that i can work with. I have searched the web endlessly and still cant produce such thing. Any help is appreciated. hi i am calling some text from a database with php and echoing it to flash. i have no idea the charset anything is in. all i can see is that calling the text from the database and placing into flash yields in some missing chars, but if echo the called text to the browser, copy it and echo that to flash, all characters appear. now, i kow this is not a flash forum, and im not going to ask for a flash solution because there's just too many different charsets and code i cannot control/ nor see so i was thinking of doing something along the line of having php creating a clone of the copied text that appears in the browser, and having it echo that text to flash. it there something php can do, similar to what i described? regards Hi. I have our company website running on an older Joomla and so running PHP v5.6. We were encountering some issues so I have been rebuilding on latest Joomla version and will run the latest PHP now but I have encountered an issue (and I'm no php expert). We have a brochure page on the site where a user can select one or more brochures from a list, and then have to complete their details in a form and an email is sent to them with the brochures as a pdf attachment. We also receive an email notification with the user details and what brochure was selected so we can follow up on the lead. So I have the html coding for the web page, which links back to a php file with the brochures listed in an array and using phpmailer to send. Would someone be able to assist me by looking at the current files I have and showing me how to update the deprecated php commands - and also to ensure that the fields info is being properly verified etc. Thank you! Hello, helpful readers: You all have been great for me in the past.... How do I handled index.php file unable to print the header.php or footer.php, for instance because of the version conflict. Have transferred all files from Yahoo, to Apache 2.2.17 web server. Everything else is taking shape nicely, thanks largely to the advice received here. Thanks for any info, Yshua When using cURL, how would I be able to include a call inside my get_all that basically will loop through all the next_page, get and store the data and then output it to $response->data when the "next_page" parameter becomes null? **Here is the method**: public function get_all() { return $response->data; } **This is what $response->data is returning as of now** (The cURL code wasn't included here): "paginator": { "total": 3092, "per_page": 500, "current_page": 2, "last_page": 7, "prev_page": "https://oc.com/api/v1/employees/?page=1", "next_page": "https://oc.com/api/v1/employees/?page=3" }, "data": [ { "id": 1592, etc.... Here are all of my unsuccessful attempts: public function get_all() { // $next_url = $response->paginator->next_page; // // foreach ($response as $next => $next_page) { // print_r2($next); // // if ($next_url !== null) { // $next_page = $response->data; // } // } // foreach ($response as $paginator => $next_page) { // if ($next_url !== null) { // $return[] = $response->data; // } // } // var_dump($response->paginator); // if ($next_url !== null) { // $this->get_all($path, $args, $next_url); // } return $response->data; } Edited October 30, 2019 by Sema314 hi there i am a beginner in PHP and i really would like some help with this.....
i need to make use of the date() function to retrieve the current date. Use the split() function to retrieve the day month and the year from the current date. and the calculations to display the age.
if anyone could help me with this it would be amazing.
thank you!!
Attached Files
newagecalc.php 1.56KB
0 downloads Hi, I'm trying to set up a mysql database on my laptop, XP, and connect to a test database using php. I've created the database called 'testdb' with user account 'test' and password 'password. The code I'm using to try and connect to the test datase is the following; $dbconnect=mssql_connect("localhost", "test", "password"); But when I open the page in a web browser, I am getting the following error; Fatal error: Call to undefined function mssql_connect() in <file_location> I'm hoping I'm doing something simple here, but, I've looked up forums and this seems to be the code to use to connect. Anyone who could help would be greatly appreciated. Thanks. Hi all messing about with php , trying to get my head round the basics : ok I have a file called test.php with this in it <? $ourFileName = "testFile.txt"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); ?> when I run this instead of creating a blank text file called testfile.txt all it does is repeat the code in the command box window as below C:\php>php -f c:\php\test\test.php <? $ourFileName = "testFile.txt"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); ?> C:\php> Anyone got a clue as to why it wouldnt create a text file please? Hi there, I'm new to PHP about 5month (previously i don't have any programming background), i study the basic PHP through online. i found a tutorial from : http://www.phpwebcommerce.com/ , and there is some error in this tutorial + i need customize this tutorial for my site. I'm here to ask , is the tutorial suitable beginner like me ? is the tutorial consider for advanced used ? i able to solve some error in this tutorial but it take too long. Can give a solution ? should i give up this tutorial on my site ? or just continue find solution ? but i'm already spent almost 2month in this tutorial. so far, i left shipping cost cant find solution...(but i have try do it for 2weeks) Thank you. First of all, sorry for my bad english. I have this: PHP Code: [Select] $link = file_get_contents("http://www.imdbapi.com/?i=tt1285016"); $json = json_decode($link,true); echo $json["Title"]; and I want to replace tt1285016 with $info["id"]; (this is something from mysql, first time when i use ). If I put echo $info["id"]; it return exactly what i need: tt1421545. How can I do that? thank u very much and sorry again for bad enlighs, not native language. EDIT: Sorry for bad section, first time when i come here. hey guys, I am very new to PHP and wanted to create a simple form script. Somehow it doesnt work... can you help me? Help is highly appreciated!! here is the code: Code: [Select] <?php $admin= 'name@email.com'; // 1. Message to the admin $subject1= 'You have one new subscription'; $message1.= 'Email: '.$email."\n\n"; $message1.= 'Name: '.$name."\n\n"; // Sending mail mail($admin, $subject1, $message1, "From: $email"); header('Location: http://www.youtube.com'); ?> Hello, For all purposes, I am a complete beginner. I just know the basics of passing form data through to an email, and displaying the text of a variable on the next page via ECHO, POST, etc.. My situation is that I do not know what sort of code to use to accomplish the job of what I want done. I have created a form to be used for displaying insurance information. This form allows users to select the following: To Get Started: State You Live In: Car Information: Model Year: Original Listing Price: Your Car Currently Is: Your Information: Do You Rent Or Own? Are You Married? Do You Have Children Under 18 Years Of Age? Gross Annual Household Salary Do You Have Health Insurance? How Much Are You Worth In Total Assets (Savings, Equity, Stocks, 401K, Car, Home, etc...) ------------------------ Next to each question, I have a box from which they can select their options (standard html form code). The form has an action and the method is post. Some code is displayed below.. Now so far I am able to display the text of whatever item they selected, by using Echo $_Post etc and the answer page... Here is my problem... I want to display to the user, "recommended insurance limits" based on the data they select from the drop down boxes. So... If a user lives in Alabama, and has an income of $150K+, I want to display a different answer than someone who only earns $30K and lives in Alabama, and the list goes on, (range of options for each question). I have no idea what sort of code to use to display the appropriate answer. I thought I could use the "if, elseif, else" statement, and simply do hundreds of variations on it for each state, but surely there is a more refined and less bloated code for doing such a thing? I would need to display a different answer for the user, for each separate option they choose. Example) Alabama, Model Year of 1995, Salary of $50K would be DIFFERENT then Alabama, Model Year of 2000, Salary of $50K, etc... Thank you for your time. Here is my php code. Code: [Select] <?php $state = $_POST["state"]; $modelyear = $_POST["modelyear"]; $carprice = $_POST["carprice"]; $carownership = $_POST["carownership"]; $homeownership = $_POST["homeownership"]; $marriage = $_POST["marriage"]; $childrenunder18 = $_POST["childrenunder18"]; $salary = $_POST["salary"]; $healthinsurance = $_POST["healthinsurance"]; $assets = $_POST["assets"]; ?> <?php if ($state=="Alabama") { echo "Alabama requires the following minimum insurance limits:<br /> $25,000 - Liability Per Person<br /> $50,000 - Liability For Total Persons<br /> $25,000 - Property Damage Total"; } elseif ($state=="Alaska") { echo "Alaska requires etc..."; } else { echo "You did not select a state."; } ?> Hi guys, sorry for the beginner issues here. after following a tutorial i come across a little problem! Everything works just fine, no errors but my page source only shows the xml tags and not the actual products <?php $link = mysql_connect("localhost","Joao","password"); mysql_select_db("brimelow_store"); $query = 'SELECT * FROM products'; $results = mysql_query($query); echo "<?xml version=\"1.0\"?>\n"; echo "<products>\n"; while ($line = mysql_fetch_assoc($results)); { echo "<item>" . $line["product"] . "</item>\n"; } echo "</products>\n"; mysql_close($link); ?> source = <?xml version="1.0"?> <products> <item></item> </products> I have checked the database names and they all match... im confused. can i get some help pls? I have some PHP which sends some user provided data to the client:
<?php header('Content-type: application/json'); $data=array( array('id'=>10,'firstname'=>'John','lastname'=>'Doe'), array('id'=>14,'firstname'=>'Jane','lastname'=>'Doe'), array('id'=>19,'firstname'=>'XSS!','lastname'=>'XSS!'), ); echo(json_encode($data)); ?>The client then displays the data: $.getJSON('getJSON.php', { something: 123 }, function(list) { var string = ''; for (var i in list) { string += '<li>< a href = "index.php?id=' + list[i]['id'] + '">' + list[i]['firstname'] + '</a></li>'; } $("#MyElem").html(string); });Does this represent any XSS risk? If so, how do I prevent it? Okay so i know this is going to sound really stupid i just got out of school and now im in the real world here is the problem I dont know very much about how to work with JSON via PHP all i am trying to do is have the Script read data into an array from some text boxes then from the array into a JSON file the reading and the listing is all fine and dandy but only if i want the entire file how do i read data from a JSON file and only have it display data according to what i want so for example if i wanted to return data that matched an ID like you would do in MySQL like this ("SELECT * FROM catagories WHERE ID = '$id'") so how do i do that without a Database using a JSON File as a source for the data |