SimpleXML multiple data from another array - php

please, I have this code to import data from XML to database:
$a = glob('data/*/*.xml');
echo "import kategorie ...... ";
foreach ($a as $i) {
$xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");
foreach($xml->DocumentElement as $entry) {
foreach ($entry->hotel as $dataHotel) {
addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
}
foreach ($entry->Popisy as $dataPopisy) {
addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
}
}
}
echo "OK\n"
I can not figure out how to do it - I need in function "addHotel" get data from "hotel" array and from "Popisy" array.
So, I need to get from two at once.
Here is XML structure: http://pastebin.com/TNTpBijg and here http://fmnet.cz/HLS240.xml
Is this possilbe? Thank you very much!
Now I tried this:
$a = glob('data/*/*.xml');
echo "import kategorie ...... ";
foreach ($a as $i) {
$xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");
foreach($xml->DocumentElement as $entry) {
foreach ($entry->hotel as $dataHotel) {
//addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
foreach ($entry->Popisy as $dataPopisy) {
//addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
echo "$dataHotel->hotel";
echo "\n";
echo "$dataPopisy->doporuc";
echo "\n";
}
}
}
}
echo "OK\n";
but output is only:
import kategorie ...... OK

Yes, it is possible. You can do it by embedding the popisy loop inside the hotel loop or the other way round. Check the code below:
$a = glob('data/*/*.xml');
echo "import kategorie ...... ";
foreach ($a as $i) {
$xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");
foreach($xml->DocumentElement as $entry) {
foreach ($entry->hotel as $dataHotel) {
addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
foreach ($entry->Popisy as $dataPopisy) {
addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
}
}
}
}
echo "OK\n"

Related

How can I re-organize php array of sports

I'm trying to list sport league offerings for intramurals. My current php foreach spits out all the leagues and offerings. Great.
Now I want to only show 1 instance of the sport (course), like "Basketball" and then list the multiple league offerings (offering) in columns.
I can't do another loop based on
if $nt['course'] == "Basketball" because these will change
Not sure if my mssql query needs to change or if I can use another foreach loop?
query
$sql2 = SELECT category
,course
,offering
,Semester
,spots
,registerLink from dbtable WHERE semester like "%Fall 2016%" and category like "%Leagues%" ORDER BY course
$rs= mssql_query ($sql2, $con)
or die("Error!");
$result = array();
if (mssql_num_rows($rs)) {
while ($row = mssql_fetch_assoc($rs)) {
$result[] = $row;
}
}
return $result;
exit();
my foreach loop in view:
<?php
foreach ($data as $nt) {
echo "<div class='col-md-2'>";
echo "<h2><a href='#'>".$nt['course']."</a></h2>";
echo "<h3>".$nt['offering']."</h3>";
echo "<h4>".$nt['Semester']."</h4>";
echo "<p>".$nt['spots']."</p>";
echo "<button>".$nt['registerLink']."</button>";
echo "</div>";
}
So just to clarify:
category = "Leagues" which is in my query
course = Basketball and Flag Football, etc
It's very simple:-
instead of
$result[] = $row;
do
$result[ $row['course']][] = $row;
Simple solution:
$result = array();
if (mssql_num_rows($rs)) {
while ($row = mssql_fetch_assoc($rs)) {
if(!in_array($row["course"], $result))
{
array_push($result, $row["course"]);
}
array_push($result[$row["course"]], $row);
}
}
return $result;
And loop in view:
foreach($data as $key => $value)
{
echo "<h1>".$key."</h1>";
foreach($value as $nt)
{
echo "<div class='col-md-2'>";
echo "<h3>".$nt['offering']."</h3>";
echo "<h4>".$nt['Semester']."</h4>";
echo "<p>".$nt['spots']."</p>";
echo "<button>".$nt['registerLink']."</button>";
echo "</div>";
}
}

trying to get multiple nested values from jagged json array using json_decode

I'm trying to print the url and filename of the files array inside each instance that is shown. Each for instance
http://xbins.nyc/mvscu
mvscu.zip
http://xbins.nyc/mvsc
mvsc.zip
http://xbins.nyc/mvscjp
mvscjp.zip
The above is how the output should be once the code below runs. it throws errors since it's checking the whole code for the further nested arrays.
{
"accountId":"Ja4Rulez",
"finished":"true",
"createdAt":"2016-05-20",
"releaseDate":"1998",
"updatedAt":"2016-05-20",
"expires":"1468905126822",
"requestTableUS":{
"tableCAPCOM":{
"tableName":"CAPCOM",
"partial":"true",
"files":[
{
"url":"http://xbins.nyc/mvscu",
"filename":"mvscu.zip"
}
]
}
},
"requestTableMain":{
"tableCAPCOM":{
"tableName":"CAPCOM",
"partial":"true",
"files":[
{
"url":"http://xbins.nyc/mvsc",
"filename":"mvsc.zip"
}
]
}
},
"requestTableJp":{
"tableCAPCOM":{
"tableName":"CAPCOM",
"partial":"true",
"files":[
{
"url":"http://xbins.nyc/mvscjp",
"filename":"mvscjp.zip"
}
]
}
}
}
The above query is sent to the $data variable below and then json_decoded into $obj.
<?php
$obj = json_decode($data, TRUE);
foreach($obj as $row){
foreach($row as $k){
foreach($k as $j){
foreach($j as $l){
echo $l['url'];
echo "<br />";
echo $l['filename'];
echo "<br />";
}
}
}
}
?>
Also the number of returned "requestTable"s returned vary but here it's simply 3, but it can change from 3 to 100+. I need to make sure the loop can accommodate additional "requestTables" if available.
Most of the elements of $obj are not arrays, so you can't use foreach on them. You only want to drill down into the elements whose keys begin with requestTable, and then you only want to loop through the files, not everything in that element.
foreach ($obj as $key => $row) {
if (substr($key, 0, 12) == 'requestTable')) {
foreach ($row['tableCAPCOM']['files'] as $l) {
echo $l['url'];
echo "<br />";
echo $l['filename'];
echo "<br />";
}
}
}
This snippet should do exactly what you want.
<?php
$array = json_decode($json, true);
foreach($array as $key=>$data) {
if (strpos($key, 'requestTable') !== false) {
foreach($data['tableCAPCOM']['files'] as $file) {
echo $file['url'];
echo "<br />";
echo $file['filename'];
echo "<br />";
}
}
}
?>

How to get more than 20 results for nearby places using google api

<?php
{
$references = array();
$names = array();
$lat="26.177194999999998";
$long="91.77591333333334";
$count=0;
$placeSearchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10000&types=mosque&sensor=true&key=AIzaSyASP02W3Qwb75Ruep3isiGstqA7Y2HXjGw";
$placeSearchJSON = file_get_contents($placeSearchURL);
$dataArray = json_decode($placeSearchJSON);
if(isset($dataArray->status) &&$dataArray->status == "OK") {
foreach( $dataArray->results as $details) {
array_push($references, $details->reference);
array_push($names, $details->name);
}
}
foreach ($names as $name) {
echo $name."<br>";
}
echo "next token".$dataArray->next_page_token."<br>";
if(!empty($dataArray->next_page_token)) {
echo "in the if statement"."<br>";
$placeSearchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10000&types=mosque&sensor=true&key=AIzaSyASP02W3Qwb75Ruep3isiGstqA7Y2HXjGw&nextpage=".$dataArray->next_page_token;
$placeSearchJSON = file_get_contents($placeSearchURL);
//echo "hello";
$dataArray = json_decode($placeSearchJSON);
if(isset($dataArray->status) &&$dataArray->status == "OK") {
foreach( $dataArray->results as $details) {
array_push($references, $details->reference);
array_push($names, $details->name);
}
}
echo "hello<br>";
}
foreach ($names as $name) {
echo $name."<br>";
}
echo "next token".$dataArray->next_page_token."<br>";
if(!empty($dataArray->next_page_token)) {
echo "in the if statement"."<br>";
$placeSearchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10000&types=mosque&sensor=true&key=AIzaSyASP02W3Qwb75Ruep3isiGstqA7Y2HXjGw&nextpage=".$dataArray->next_page_token;
$placeSearchJSON = file_get_contents($placeSearchURL);
//echo "hello";
$dataArray = json_decode($placeSearchJSON);
if(isset($dataArray->status) &&$dataArray->status == "OK") {
foreach( $dataArray->results as $details) {
array_push($references, $details->reference);
array_push($names, $details->name);
}
}
echo "hello<br>";
}
foreach ($names as $name) {
echo $name."<br>";
}
?>
I get the same 20 results every time, and I have used pagination as you can see in my code. Every time I print the next page token I get a different one.
I would really appreciate if someone could point out the error in my code because I am stuck with this for quite some time. You are also most welcome to give any kind of relevant suggestion.
use pagetoken instead of nextpage in your url string and in the last two calls give only api key and page token as parameter and after each api call use sleep(2) i tried your code and it works perfectly when the following changes are made.happy coding!
<?php
//echo"hello";
$references = array();
$names = array();
$lat="26.177194999999998";
$long="91.77591333333334";
$count=0;
$placeSearchURL ="https://maps.googleapis.com/maps/api/place/radarsearch/json?location=26.177194999999998,91.77591333333334&radius=3000&types=mosque&key=your APIkey";
$placeSearchJSON = file_get_contents($placeSearchURL);
$dataArray = json_decode($placeSearchJSON);
foreach( $dataArray->results as $details) {
//echo $details->place_id;
array_push($references, $details->place_id);
}
foreach($references as $reference)
{
$count=$count+1;
//echo $count." ";
$placeSearchURL="https://maps.googleapis.com/maps/api/place/details/json?placeid=".$reference."&key=your apikey";
$placeSearchJSON = file_get_contents($placeSearchURL);
$dataArray = json_decode($placeSearchJSON);
echo $dataArray->result->name."<br>";
echo "lat ".$dataArray->result->geometry->location->lat."<br>";
echo "lon ".$dataArray->result->geometry->location->lng."<br>";
echo "address ".$dataArray->result->formatted_address."<br>";
echo "<br>";
}
?>
basically i wanted to find a way to get more than 20 places of type masjid so i first used radar search to get placeids and then for each placed_id got the information and best part is you can get arount 200 results of nearby places

PHP Multidimensional object menu

I have made function to get multidemnsional menu from database.
public function get_menu($parent=0,$vis=1) {
$categories = array();
$this->db->from('ci_categories');
$this->db->where('cat_child',$parent);
$this->db->where('cat_vis',1);
$this->db->order_by('cat_order');
$q = $this->db->get();
$result = $q->result();
$i=0;
foreach($result as $mainCategory) {
$mainCategory->cat_subcategories = $this->get_menu($mainCategory->cat_id);
$categories[$i] = $mainCategory;
$i++;
}
return $categories;
}
It is working but I would like to print out the menus. For now I can print main menu and one child with this code:
if (count($menu) > 0) {
foreach($menu as $menu_item) {
$link = anchor("category/".$menu_item->cat_url, $menu_item->cat_name);
echo "<li>$link";
if (!empty($menu_item->cat_subcategories)) {
echo "<ul>";
foreach($menu_item->cat_subcategories as $subcat) {
$sub_link = anchor("category/".$subcat->cat_url, $subcat->cat_name);
echo "<li>$sub_link</li>";
}
echo "</ul>";
}
echo "</li>";
}
}
It is working too but how could I print out even 3rd or 4th submenu without manualy writing those foreach and ifs ? Thanks
You can try a recursive function...
Something like:
function showMenu($menu) {
echo "<ul>";
foreach ($menu as $menu_item) {
$link = anchor("category/".$menu_item->cat_url, $menu_item->cat_name);
echo "<li>$link";
if (!empty($menu_item->cat_subcategories)) showMenu($menu_item->cat_subcategories);
echo "</li>";
}
echo "</ul>";
}
That's an idea ;)

Search multidimensional arrays for specific keys and output their data

I have following array construction: $array[$certain_key][some_text_value]
And in a while loop, I want to print the data from the array, where $certain_key is a specific value.
I know how to loop through multidimensional arrays, which is not the complete solution to this problem:
foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2\n";
}
}
I do not want to loop the whole array each time, but only when $certain_key is matched.
EDIT: to be more exact, this is what I'm trying to do:
$array[$array_key][some_text];
while reading from db {
//print array where a value returned from the db = $array_key
}
while ($row = fetch()) {
if (isset($array[$row['db_id']])) {
foreach ($array[$row['db_id']] as $some_text_value => $some_text_values_value) {
echo ...
}
}
}
foreach ($array as $certain_key => $value) {
if($certain_key == $row['db_id']) {
foreach ($value as $some_text_value) {
echo "$v2\n";
}
}
}
You mean like
foreach($array[$certain_key] as $k => $v)
{
do_stuff();
}
?
Maybe you're looking for array_key_exists? It works like this:
if(array_key_exists($certain_key, $array)) {
// do something
}
<?php
foreach ($a as $idx => $value) {
// replace [search_value] with whatever key you are looking for
if ('[search_value]' == $idx) {
// the key you are looking for is stored as $idx
// the row you are looking for is stored as $value
}
}

Categories