Related
In a php script I have the following
$jsonurlpers = "https://api.openarch.nl/1.1/records/show.json?archive=nha&identifier=8554bfba-9fd9-4ca2-876c-feb7da095d6c";
$jsondatapers = file_get_contents($jsonurlpers);
That string I want to decode with the next command
$jsonpers = json_decode($jsondatapers ,true)[0];
But somewhere it did not work...
When I go to to the #jsonulpers I see :
Event
EventType "Overlijden"
EventDate
Year "1901"
Month "7"
Day "7"
EventPlace
Place "Egmond-Binnen"
RelationEP
Can some one tell me how I can read the value behind EventType in a variable? I tried different options but none works...
Thanks,
Fred
I did try this
e.g.
$itemevent = $jsonpers['Event'];
$itemeventtype = $itemevent['EventType']);
abs I thought
$itemeventtype would have a value of "Overlijden", but it was empty
The (json-)output from the url you gave looks like this:
{
"Event" : {
"EventType" : "Overlijden",
"EventDate" : {
"Month" : "7",
"Day" : "7",
"Year" : "1901"
},
"EventPlace" : {
"Place" : "Egmond-Binnen"
}
},
"RelationEP" : [
{
"RelationType" : "Overledene",
"EventKeyRef" : "Event1",
"PersonKeyRef" : "Person1"
},
{
"PersonKeyRef" : "Person2",
"RelationType" : "Vader",
"EventKeyRef" : "Event1"
},
{
"PersonKeyRef" : "Person3",
"RelationType" : "Moeder",
"EventKeyRef" : "Event1"
}
],
"Source" : {
"SourcePlace" : {
"Place" : "Egmond-Binnen / Egmond-Binnen"
},
"SourceAvailableScans" : {
"Scan" : {
"Uri" : "https://nha.blob.core.windows.net/scans/BS%20Overlijden/Egmond-Binnen/1901/RNH_O_EGB_1901_006-a.jpg",
"OrderSequenceNumber" : "1"
}
},
"SourceLastChangeDate" : "2015-07-31",
"SourceRemark" : {
"Value" : "Datadump ExportBS+Overlijden_20210302_102010.csv van NHA via e-mail"
},
"SourceReference" : {
"InstitutionName" : "Noord-Hollands Archief",
"DocumentNumber" : "14",
"Place" : "Haarlem"
},
"RecordGUID" : "{8554bfba-9fd9-4ca2-876c-feb7da095d6c}",
"SourceIndexDate" : {
"To" : "1901-12-31",
"From" : "1901-01-01"
},
"SourceDate" : {
"Year" : "1901",
"Month" : "7",
"Day" : "8"
},
"SourceType" : "BS Overlijden"
},
"Person" : [
{
"BirthPlace" : {
"Place" : "Egmond-Binnen"
},
"PersonName" : {
"PersonNameLastName" : "Baltus",
"PersonNameFirstName" : "Aafje"
},
"Gender" : "Vrouw",
"Age" : {
"PersonAgeYears" : "8 maanden"
}
},
{
"PersonName" : {
"PersonNameFirstName" : "Jan",
"PersonNameLastName" : "Baltus"
}
},
{
"PersonName" : {
"PersonNameLastName" : "Kuijper",
"PersonNameFirstName" : "Grietje"
}
}
]
}
After "Person":, you see a [, which means there is an array of Persons.
The first person does have a "BirthPlace", for the other persons it seems to be unknown what the "BirthPlace" is, because it is not mentioned in the json.
print($jsonpers["Person"][0]["PersonName"]["PersonFirstName"] should be: "Aafje"
and
print($jsonpers["Person"][2]["PersonName"]["PersonFirstName"] should be: "Grietje"
I am new in mongoDB so I need some help with this.
I have a collection for back up. It contains documents with following data:
{
"_id" : ObjectId("59f18942e7798954dc373b67"),
"category" : "building",
},
{
"_id" : ObjectId("59f18942e7798954dc373b68"),
"category" : "building",
},
{
"_id" : ObjectId("59f18942e7798954dc373b69"),
"category" : "tree",
"type" : 18,
},
{
"_id" : ObjectId("59f18942e7798954dc373b70"),
"category" : "book",
"type" : 18,
}
Then, for example, some of objects were changed and became like that:
{
"_id" : ObjectId("59f18942e7798954dc373b67"),
"category" : "building",
"size" : 100
},
{
"_id" : ObjectId("59f18942e7798954dc373b68"),
"category" : "building",
"size" : 400
},
{
"_id" : ObjectId("59f18942e7798954dc373b70"),
"category" : "book",
"type" : 18,
"title" : "New"
}
So I need to update them in my DB. Just replace, as I understood. I've read the docs about using 'upsert' and 'multi'. But I can't implement it.
How to do that?
Basic syntax for updating document in mongo is
db.collection.update(query,update,options);
For your question,
1.
db.collection.update({_id: ObjectId("59f18942e7798954dc373b67")},{$set:{size:100}});
2.
db.collection.update({_id: ObjectId("59f18942e7798954dc373b68")},{$set:{size:400}});
3.db.collection.update({_id: ObjectId("59f18942e7798954dc373b70")},{$set:{title:"New"}});
EDIT:
You can try out this, If you want to perform Write operation in one command:
> db.characters.bulkWrite(
> [
> { updateOne :
> {
> "filter" : { _id : ObjectId("59f18942e7798954dc373b67") },
> "update" : { $set : { size : 100 } }
> }
> },
> { updateOne :
> {
> "filter" : { _id : ObjectId("59f18942e7798954dc373b68") },
> "update" : { $set : { size : 400 } }
> }
> },
> { updateOne :
> {
> "filter" : { _id : ObjectId("59f18942e7798954dc373b68") },
> "update" : { $set: { title: "New"} }
> }
> },
> ]
>);
I have simple trouble in json
This is my Json code
{ "id" : "1", "name" : "test1" },
{ "id" : "2", "name" : "test2" },
{ "id" : "3", "name" : "test3" },
{ "id" : "4", "name" : "test4" },
{ "id" : "5", "name" : "test5" }
And this it the ajax code I am getting the data
function load_res()
{
$.ajax({
url: 'data.js',
type: 'POST',
dataType: 'json',
success: function(data) {
var div_data='';
$.each(data, function(index, element) {
div_data +="<div ><a href='"+data.name+"'>"+data[index].name+"</a></div>";
});
$('#9lessonsLinks').append(div_data);
}
});
}
HTML part
<a onclick="load_res()">Button</a>
<div id="9lessonsLinks"></div>
Above code it working well and data retrieve and display. But my problem is, when I add a new row to JSON file and I click on load_res() function, it will show old printed data with new data again, multiple printing with multiple clicking. I only want to get the newly added data as last line.
please help me to resolve this. appreciate your great ideas.
Thanks!
Easy fix: change .append to .html
$('#9lessonsLinks').html(div_data);
Empty the list before adding the items to it...
$('#9lessonsLinks').empty().append(div_data);
With javascript like this:
document.getElementById("9lessonsLinks").innerHTML = div_data;
Using ammaps' dataLoader, is there a way to pass post parameters to an external file?
I'm creating a report that contains a map, which when clicked, will provide a list of news articles from a MySQL table. I'm pulling in json data using dataLoader. However, I need to be able to pass a value (reportId) to the script that generates the json file.
here's my code:
var map = AmCharts.makeChart("mapdiv", {
type: "map",
"dataLoader": {
"url": "maparticlesfeed.json.php",
"format":"json",
"showErrors": false//,
// tried this:
//"type" : "post",
//"data" : {"reportIdFromPost" : 1}
},
. . . //the rest of the map config
And this is maparticlesfeed.json.php:
<?php include('database-connect.php');
$con=Database::connect();
$query=$con->prepare("SELECT * from articles Where reportId=$POST['reportIdFromPost']");
$query=>execute();
$articlesList=$query->fetchall(PDO::FETCH_ASSOC);
?>
{
"map" : "MyMapName",
"_comment" : "Here, instead of hard-coding the headlines, we will iterate through $articlesList",
"areas" : [
{ "title" : "Virginia",
"id" : "US-VA",
"selectable" : true,
"numArticles" : 4,
"articles" : [
{ "headline" : "This is the first headline",
"link" : "link url"
},
{ "headline" : "This is the second headline",
"link" : "link url"
},
{ "headline" : "This is the third headline",
"link" : "link url"
},
{ "headline" : "This is the fourth headline",
"link" : "link url"
}
]
},
{ "title" : "Tennessee",
"id" : "US-TN",
"selectable" : false,
"numArticles" : 6
}
]
}
I went the jQuery route to solve this. I'd still like to know if there's a way to do this with dataLoader, but here's a workaround in case anyone else has this issue in the future:
$.ajax({
dataType: "json",
url: "maparticlesfeed.json.php",
data: { reportIdFromPost: 1 },
type: "post",
success: function(data){
console.log(data); // for debugging
makeMap(data);
},
error: function(data) {
console.log('it did not work.');
console.log(data.responseText);
}
});
function makeMap(theData) {
var map = AmCharts.makeChart("mapdiv", {
type: "map",
/* DON'T NEED THIS ANYMORE:
"dataLoader": {
"url": "maparticlesfeed.json.php",
"format":"json",
"showErrors": false
},*/
// replace instead with this:
"dataProvider" : theData,
// ... the rest of the map config ...
}
});
And, in maparticlesfeed.json.php:
<?php
include('database-connect.php');
$con=Database::connect();
if(!empty($_POST) && isset($_POST['reportIdFromPost']) ) {
$postedReportId= $_POST['reportIdFromPost'];
}
include('database-connect.php');
$con=Database::connect();
$query=$con->prepare("SELECT * from articles Where reportId=$postedReportId ");
$query->execute();
$articlesList=$query->fetchall(PDO::FETCH_ASSOC);
?>
{
"map" : "MyMapName",
"areas" : [
// Code to json_encode the results from $articlesList
]
}
It may be that you're posting json data and then not decoding it when you're trying to use the value in your php script.
Try this at the top of maparticlesfeed.json.php:
<?php
include('database-connect.php');
$postedValues = json_decode($POST['reportIdFromPost'], true);
$con=Database::connect();
$query=$con->prepare("SELECT * from articles Where reportId=$postedValues['reportIdFromPost']");
$query=>execute();
$articlesList=$query->fetchall(PDO::FETCH_ASSOC);
?>
You'll also need to uncomment the two lines in the map config:
"type" : "post",
"data" : {"reportIdFromPost" : 1}
Maybe you could add the parameters in the "url" parameter of the dataloader.
"url": "urlA.do?param1=" + $('.selector').val()
and then in some trigger (ie. button click) modify the url and reload
var url = "same URL with different parameters values";
chart.dataLoader.url = url;
chart.dataLoader.loadData()
I try to retrieve data to create a table with tablesorter like here :
http://mottie.github.io/tablesorter/docs/example-widget-build-table.html ( sections : "Setup - Object (javascript variable)" )
The HTML code ( a simple DIV ) :
<div id="list_user"></div>
Here is the JS script :
jQuery(document).ready(function() {
function get_users_list(){
return $.post(
"/mysql/function_users.php",
{
'type_request' : 'list_users'
},
"json"
);
};
get_users_list().done(function(data){
return data;
});
$(document).ajaxComplete(function( event,request, settings ) {
if (settings.url == "/mysql/function_users.php") {
var dataOject = request.responseText;
alert(dataOject);
$('#list_user').tablesorter({
debug:true,
theme : 'blue',
sortList: [[0,0]],
widthFixed: false,
widgets: ['zebra','filter'],
data : dataOject,
widgetOptions : {
build_objectRowKey : 'rows',
build_objectHeaderKey : 'headers',
filter_hideEmpty : false
}
});
};
});
});
When I check what is return from the php script "function_users.php", I get the following structure :
{
"headers":[
[
"Name",
"Surname",
"email",
"region",
"date creation",
"last modification",
{"text":"","class":"not_sort filter-false toclean"},
{"text":"","class":"not_sort filter-false toclean"},
{"text":"","class":"not_sort filter-false toclean"}
]
],
"rows":[
["toto","toto","toto#toto.com","latam","1410142447","1410142537"],
["tata","tata","tata#tata.com","emea","1410142447","1410142537"],
["titi","titi","titi#titi.com","asia","1410142447","1410142537"],
["tutu","tutu","tutu#tuttu.com","latam","1410142447","1410142537"]
]
}
If you have any lead please. Thank you
I was intrigued by your question, and I am eager to make use of this tablesorter tool in the future. I have created a couple of possible approaches to the problem.
See http://jsfiddle.net/terrywbrady/4my61h6d/9/
The first approach (commented out) requests the data from a GIST.
var gisturl = "https://gist.githubusercontent.com/terrywbrady/eb75d9097e633682539e/raw/798bd947404dbcc388fc8954d5d557dec13d0ef7/result.json";
function get_users_list(){
$('#list_user').tablesorter({
theme: 'blue',
widgets: ['zebra'],
widgetOptions: {
build_type : 'json',
build_source : {
url: gisturl,
dataType: 'json'
}
}
});
};
The second approach uses the jsfiddle echo service to return the result.
var DATA = {
"headers":[
[
"Name",
"Surname",
"email",
"region",
"date creation",
"last modification",
{"text":"","class":"not_sort filter-false toclean"},
{"text":"","class":"not_sort filter-false toclean"},
{"text":"","class":"not_sort filter-false toclean"}
]
],
"rows":[
["toto","toto","toto#toto.com","latam","1410142447","1410142537"],
["tata","tata","tata#tata.com","emea","1410142447","1410142537"],
["titi","titi","titi#titi.com","asia","1410142447","1410142537"],
["tutu","tutu","tutu#tuttu.com","latam","1410142447","1410142537"]
]
};
var JDATA = {
json : JSON.stringify(DATA)
}
function get_users_list2(){
$('#list_user').tablesorter({
theme: 'blue',
widgets: ['zebra'],
widgetOptions: {
build_type : 'json',
build_source : {
url: "/echo/json/",
data: JDATA,
contentType: "application/json",
type: "POST",
dataType: 'json'
}
}
});
};
For simplicity, I removed a few of your display options from the table configuration.