OpenWeatherMap Weather Details - php

I wanted to display the weather from a user on my website with openweathermap. that works with the temperatures so far i wanted to display whether it is raining or cloudy that doesn't work. weather->main doesn't work can someone help me?
Here is my code
$url="http://api.openweathermap.org/data/2.5/forecast?q=".$city.",".$land."&APPID=MYAPPIDf&units=metric";
$json=file_get_contents($url);
$data=json_decode($json);
echo '<p>Temperature from: ', $data->city->name, ' (', $data->city->country, ')</p>';
// the general information about the weather
echo '<p><strong>Current:</strong> ', $data->list[0]->main->temp, '° C</p>';
echo '<p><strong>Min:</strong> ', $data->list[0]->main->temp_min, '° C</p>';
echo '<p><strong>Max:</strong> ', $data->list[0]->main->temp_max, '° C</p>';
echo '<p><strong>Fells Like:</strong> ', $data->list[0]->main->feels_like, '° C</p>';
echo '<p><strong>actualy Weather:</strong> ', $data->list[0]->weather->main, '</p>';
?>
that's what it shows me on my website
ErrorImage
I've tried a few things but I can't do it myself

weather is an array, you must use an index to access the properties of one element :
echo $data->list[0]->weather[0]->main ;
Also, remove your API key from your question.

Related

PHP: Only show first word of a custom taxonomy

This is my code for displaying a taxonomy (WordPress):
<?php
$locations = get_the_terms($post->ID , 'location');
foreach ($locations as $location) {
echo '<div class="term-location">';
echo $location->name . ' ';
echo '</div>';
}
?>
How can I modify the code to only display the first word of the taxonomy? I've tried it using explode but I can't get it to work (since my php knowledge is limited).
Thanks in advance!
You can indeed use explode so you were on the right path!
Explode returns an array of strings. So you first use explode, and then select the first item.
$locations = get_the_terms($post->ID , 'location');
foreach ($locations as $location) {
echo '<div class="term-location">';
echo explode(' ', $location->name)[0] . ' ';
echo '</div>';
}
You can get more info on explode here, and an example on getting the first word here.

error while using if statement inside image in php

i have a codeigniter php website, in the image tag I have given an if statement to determine the source, the code is like below:
<img class="st_img" <?php if(str_replace(' ', '',$val['unique_tblkey'])=='entertainment'){?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'s/'.$val['image'][0]['btp_image'];?>"<?php} else { ?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'/'.$val['image'][0]['btp_image'];?> " <?php } ?>/>
I am getting
unexpected '}'
this error is coming although there isn't any unwanted brackets, can anyone please tell me what is wrong in my code, thanks in advance
OK, I finished rewriting it. Not completely sure if it will work, because I cannot test it.
<?php
$tableKey = strtolower(str_replace(' ', '', $val['unique_tblkey']));
$url = ADMIN_IMG .
$tableKey .
($tableKey == 'entertainment' ? 's/' : '/') .
$val['image'][0]['btp_image'];
echo '<img class="st_img" src="'.$url.'" />';
There's no repetition, multiple lines, and no mixing of PHP and HTML. This all makes for more readable code.

Exploding an Echo that is separated by commas into new lines

I'm echoing the results a multi-select dropdown, but they appear in one line, with commas and no spaces. Users fill out a form, and select as many items as they want from the dropdown. On their profile, I want to display their selections.
Market Segment:<br>
,Health Systems-Large,Health Systems-Small/Medium,Community Hospitals
I want them to display as:
Market Segment:<Br>
Health Systems-Large<br>
Health Systems-Small/Medium<br>
Community Hospitals
My current code:
<?php if ($user["market_segment"] !="") { ?>
Market Segment:<br><?php echo $user["market_segment"];?>
<?php } ?>
EDIT - Solution:
echo str_replace(",", "<br />", $user["market_segment"]);
Like this:
echo 'Market Segment:<br>';
foreach (explode(',', $user["market_segment"]) as $market_segment) {
echo $market_segment . '<br>';
}
You want to subsitute the , with a new line. So str_replace is your friend here.
echo str_replace(",", "<br />", $user["market_segment"]);
(That is if you are serving this information on a homepage, which it seems you are doing. If not you would wnat a \n instead of br tag)

PHP using Propel

I am having an issue when I try to post the Join Date on my web page:
Fatal error: Class 'getJoinDate' not found in ********
$newDate = getJoinDate::createFromFormat("l dS F Y", $dateFromDB);
$posts = PostQuery::create()->findPk(1);
echo "<p>", "ID:".$posts->getUserID().", ".$posts->getContent().", ".$posts->getJoinDate()." </p>";'
Please let me know what I must do to display the format.
It's this code:
echo "<p>", "ID:".$posts->getUserID().", ".$posts->getContent().", ".$posts->getJoinDate()." </p>";'
I'm not sure what your commas were supposed to be, but I guess not actual commas and so you have commas where stops should be: echo "<p>", <--
And an apostrophe at the end of the line: </p>";'<--
You should grab a decent IDE that will show errors like this immediately, saves a lot of time. And read your error log as it tells you such errors.
Using double quotes variable names will be expanded, try this:
echo "<p>ID: {$posts->getUserID()} {$posts->getContent()} {$posts->getJoinDate()}</p>";
Or this way:
echo '<p>ID: ' . $posts->getUserID() . ' ' . $posts->getContent() . ' ' . $posts->getJoinDate() . '</p>';

How to combine php code as hyperlink

I am trying to combine a code used to pull name of a store for my site with a word, and it is seperating the store name and word and not forming a hyperlink.
For example it will read..............6pm deals, but only hyperlink the store name, here is code that pulls the store name........
<div class="store-name">
<?php echo get_the_term_list($post->ID, APP_TAX_STORE, ' ', ', ', ''); ?>
</div>
I want it to say 6pm deals and hyperlink entire term.
<div class="store-name"><?php echo get_the_term_list($post->ID, APP_TAX_STORE, ' ', ', ', ''); ?>.....6pm deals</div>

Categories