I'm working with google places API , and i showed content from the api but one by one like this
<?php
$str = file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.075971700000004,-0.7651981999999999&radius=5000&type=restaurant&keyword=cruise&key=AIzaSyDxXV4Ka8yiDq1-UKzlzX-MUC8csfdN8y4');
$maps_url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.075971700000004,-0.7651981999999999&radius=5000&type=restaurant&keyword=cruise&key=AIzaSyDxXV4Ka8yiDq1-UKzlzX-MUC8csfdN8y4';
$maps_json = file_get_contents($maps_url);
$maps_array = json_decode($maps_json, true);
$lat = $maps_array['results'][1]['name'];
$lat2 = $maps_array['results'][2]['name'];
echo $lat;
echo "<br>";
echo $lat2;
?>
but i want to show all the result one time with a loop
Try
foreach ($maps_array['results'] as $map) {
echo $map['name'];
echo "<br>";
}
Put the results array in a foreach and make a loop from that. in that way u will get all the data instead of taking it 1 by 1. And for the numbers array u can add a ++ statement(not needed).
hope this helps. Good luck
Related
I apologize in advance if there was already a similar question. I tried to find a solution but unfortunately I still can't.
I have a JSON response from influxdb
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
and I need to print out the name of columns time and count and values to be able generate a new JSON for Google chart.
What am I able to do now is only get a statement_id=0.
$decode = json_decode($output2,true);
foreach($decode['results'] as $val){
//Actual statement_id is 0
echo "statement_id is ";
echo $val['statement_id'];
echo "<br/>";
}
But I need to print:
time, count<br/>
2020-07-02T00:00:00Z,1<br/>
2020-07-03T00:00:00Z,0<br/>
2020-07-04T00:00:00Z,0<br/>
2020-07-05T00:00:00Z,0<br/>
2020-07-06T00:00:00Z,1<br/>
or put it into the variable to be able to work with it. I already tried without converting into array just with
$decode = json_decode($output2); //without ",true"
Can you please help me how to solve it.
I've spent almost the whole day looking for a solution and I can't find a way to solve the problem.
If you need to print it out on html, just change the \n to <br>
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
echo $decode["results"][0]["series"][0]["columns"][0].",".$decode["results"][0]["series"][0]["columns"][1]."\n";
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
echo $el[0].",".$el[1];
echo "\n";
}
?>
If i understand you write this would be the code to output the JSON for the Google Chart:
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
$googlechart = array();
$googlechart[] = array($decode["results"][0]["series"][0]["columns"][0],$decode["results"][0]["series"][0]["columns"][1]);
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
$googlechart[] = array($el[0],$el[1]);
}
echo json_encode($googlechart);
?>
I'm trying to request more than 100 live channels from the Twitch API. The current limit of results are 100. I need to do a new request every 100 while adding +1 to the offset url. I've tried to search to find help but I haven't been able to find anything.
<?php
$get2 = json_decode
(#file_get_contents_curl('https://api.twitch.tv/kraken/streams/?
offset=0&limit=5'), true);
foreach ($get2['streams'] as $test) {
echo "<pre>";
print_r ($test['channel']['name'] . ' | ' . $test['viewers']);
echo "</pre>";
}
?>
This will echo 100 results perfectly. I just need to loop that code x however many times I need to get to the total. So say there are 30,000 live streams. That will be 300 queries.
Figured out what I had to do after staying up all night working on it.
This will take the request and loop it for as many times as I need to get the full results.
for ($offset = 0; $offset < $requests; $offset++) {
$testz =
json_decode(#file_get_contents_curl('https://api.twitch.tv/kraken/streams/?
offset=' . $offset . '&limit=100'), true);
foreach ($testz['streams'] as $streamz) {
echo "<pre>";
print_r ($streamz['channel']['name']);
echo "</pre>";
}
}
I've read the Q&A regarding this topic, but it unfortunately doesn't answer my question, because I'm a Beginner in PHP.
I'm using a function to display a polygon on a google Map. That works all fine. The coords are stored in the following variable:
$polygon = array(
"43.231297 -79.813721",
"43.238438 -79.810768",
"43.230335 -79.809395",
"43.230312 -79.809296",
"43.240208 -79.808983",
"43.230225 -79.808884",
"43.240116 -79.808617",
"43.229823 -79.807388",
"43.231235 -79.802649",
"43.237137 -79.800774",
"43.231297 -79.813721"
);
I now want to get the latitude and longitude dynamically out of a MySQL database. My code below runs great and returns the desired coordinates:
<?
foreach ($BusinessAreaMunich as $item) {
echo "new google.maps.LatLng(" .$item['AreaCoordLatitude'] . "," .$item['AreaCoordLongitude'] . "), \n";
}
?>
However, I've tried to do the following:
$polygon = array(
foreach ($BusinessAreaMunich as $item) {
echo $item['AreaCoordLatitude'], $item['AreaCoordLongitude'];
}
);
Now I know that doesn't work, but I don't know how to solve my issue. Could you please give me an idea how to solve this?
Proper code is:
$polygon = array(); // define `$polygon` as array
foreach ($BusinessAreaMunich as $item) {
// create `string` value as a result of concatenating coords and a space
$coords = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];
// append a `string` to `$polygon`
$polygon[] = $coords;
// or simply:
// $polygon[] = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];
}
// output to see what you have
print_r($polygon);
I'm working on a WordPress site and am using a plugin called 'Simple Fields', to add data to repeatable fields. I've entered data into three fields: (audioinfo, audiofile, audiocomposer).
I would now like to create a post that displays that information in the following way:
Audio Info 1
Audio File 1
Audio Composer 1
~~~
Audio Info 2
Audio File 2
Audio Composer 2
I've been trying to figure this out with foreach. Here's the closest I can arrive at (thought I know it's invalid). Can anyone make any suggestions of how to handle this?
<?php
$audioinfo = simple_fields_values("audioinfo");
$audiofile = simple_fields_values("audiofile");
$audiocomposer = simple_fields_values("audiocomposer")
foreach ($audioinfo as $audioinfos, $audiofile as $audiofiles, $audiocomposer as $audiocomposers){
echo $audioinfos;
echo $audiofile;
echo $audiocomposer;
}
?>
(Just as a side note, in case it's important, the first three lines appear to all be valid - that is, if I do a "foreach" on $audiocomposer alone, I successfully get: Bach Handel Beethoven.)
Is there something I can do to apply "foreach" to all three at the same time?
Thanks!
Assuming all the arrays of same length, making use of a normal for loop.
<?php
$audioinfo = simple_fields_values("audioinfo");
$audiofile = simple_fields_values("audiofile");
$audiocomposer = simple_fields_values("audiocomposer");
for($i=0;$i<count($audioinfo);$i++)
{
echo $audioinfo[$i];
echo $audiofile[$i];
echo $audiocomposer[$i];
}
?>
One more pretty solution:
<?php
$audioinfo = simple_fields_values("audioinfo");
$audiofile = simple_fields_values("audiofile");
$audiocomposer = simple_fields_values("audiocomposer");
foreach ($audioinfo as $k => $val){
echo $val;
echo $audiofile[$k];
echo $audiocomposer[$k];
}
?>
Assuming there are always going to be the same number of items in all 3 arrays, you could do something like this:
$items = count($audioinfo);
for ($i = 0; $i < $items; $i++)
{
echo $audioinfo[$i];
echo $audiofile[$i];
echo $audiocomposer[$i];
}
here i gave one example how to handle the 3 array value in single foreach , hope it will much help to you
$audioinfo =array('popsong','rocksong','molodysong');
$audiofile=array('jackson','rahman','example');
$audiocomposer =array('first','second','third');
foreach($audioinfo as $key => $value)
{
echo $value."</br>";
echo $audiofile[$key]."</br>";
echo $audiocomposer[$key]."</br>";
}
Im learning JQuery and php.
Is it possible to store multiple video links within variables and get php to echo one at random??
My goal is to control my own video ads on my site and I thought this would be a good idea but I dont have a clue where I should look online.
Here is what I was thinking
<?php
$advert1 = 'MyVIDEO1.mp4';
$advert2 = 'MyVideo2.mp4';
$advert3 = 'MyVideo3.mp4';
I want a code that would go here and say: randomly select one of these vars.
echo "At random one of the vars";
?>
I hope Im making sense. help?
You could make an array, like this:
$advert[] = array();
$advert[1] = 'MyVIDEO1.mp4';
$advert[2] = 'MyVIDEO2.mp4';
$advert[3] = 'MyVIDEO3.mp4';
$chosen_one = rand(1,count($advert));
echo $advert[$chosen_one];
You can also use array_rand() instead of shuffle():
<?php
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
echo $videos[array_rand($videos)];
?>
For embedding the video in the right way, have a look at http://www.w3schools.com/html/html_videos.asp
For a start, PHP itself won't exactly play a video. You could use it to echo out one of the URL's to a video. So, bear in mind there is a lot more to do than just select a video.
To answer your question in the code I'd recommend the following:
Try looking up PHP arrays. You want to keep all the videos in an array.
Next up, you'll want to shuffle that array. Then select the first element.
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
shuffle($videos);
echo $videos[0];
This is a simple solution
Place your adverts in an array
<?php
$adverts = array("advert1" => "MyVIDEO1.mp4",
"advert2" => "MyVIDEO2.mp4",
"advert3" => "MyVIDEO3.mp4");
$count = count($adverts);
$rand_advert = rand(1, $count);
echo $adverts['advert'.$rand_advert];
?>
Or alternatively if you don't want to have keys and just put the values in the array straight away
<?php
$adverts = array("MyVIDEO1.mp4",
"MyVIDEO2.mp4",
"MyVIDEO3.mp4");
shuffle($adverts);
echo $adverts[0];
?>
You have need all value in a array then find index randomly and show it.
<?php
$advert = array(
'MyVIDEO1.mp4',
'MyVideo2.mp4',
'MyVideo3.mp4'
);
$total_video = count($advert);
$total_video--; //array index starting from 0 so decrease 1
$random_index = rand(0, $total_video); //array index 0 to 2
$video_to_play = $advert[$random_index];
echo $video_to_play;
?>