I have this to select columns from a table and I want to pass this to Jquery ajax fn.
I am using below code but getting invalid json
My table has three column id, name and city but I am not selecting city
This is my json reponse
["{id:1,name\":\"JOHN\",\"city\":\"null\"}"
,"{\"id\":2,\"name\":\"MICHEAL\,\"city\":\"null\"}"]
Sadly the current stable (1.8.1) WanWizard datamapper DMZ_Json class double encode fields when you call all_to_json(). This issue seem to be fixed in the develop branch. You could workaround this multiple ways:
1. Call to_json() on individual model objects and create the json array with string manipulation:
$my_objects = (new Model)->get();
$results = array();
foreach ($my_objects as $o) {
$results[] = $o->to_json();
}
// building a json array from the strings returned by $o->to_json()
print '['.join(',', $results).']';
2. You can use the array extension's all_to_array method and json_encode that result:
$my_object_arrays = (new Model)->get()->all_to_array();
print json_encode($my_object_arrays);
Related
I have in a Codeigniter 4 controller this code:
$competicion =$db->table('competiciones')
->select('*')
->where('competiciones.slug', $blog_slug)
->get()->getRowArray();
How can I get and save in a variable id_competicion value, for example? I try this code but it is wrong:
$competicionid = $competicion->[competicion_id];
getRowArray() returns an array, the correct approach to retrieve an array value would be:
$competicionid = $competicion['competicion_id'];
another possibility would be to use getRow(), which returns an object. To retrieve an object value you could use:
$competicionid = $competicion->competicion_id;
read more: Generating Query Results: Result Rows
I have a serialized String like this
$string = '[{"name":"FOO"},{"name":""},{"name":"BAR"}]';
I am trying to process it via Laravel Collection's filter method and eliminate items without a defined "name" property.
$collection = collect(\json_decode($string));
$collection = $collection->filter(function($v){
return !empty($v->name);
});
$string = \json_encode($collection->toArray());
dd($string);
Normally I am expecting something like this:
[{"name":"FOO"},{"name":"BAR"}]
But I'm getting something like this:
{"0":{"name":"FOO"},"2":{"name":"BAR"}}
Funny thing is, if I skip the filtering process or return true every time, I keep getting the string in the desired format. Removing the toArray() call has the same result. I don't want to keep the numeric indices as associative object keys.
Why this anomaly? And what should I do to get the serialized data in desired format?
In PHP arrays the index key must be unique.
In your case you have the key 'name' and collection automatically assigns the index key to all items in the collection.
To overcome that problem just call
$string = \json_encode($collection->values());
I want to update a totcosty field in the User table but it is throwing this error everytime and it is not updating the field
this is the function for execution:
public static function cost(){
$user = User::find($user_id);
$total = Helper::totcost();
// dd($tot_amt);
$user->totcosty = $total;
$user->save();
}
array to string means you are sending an array to the database but db will not accept it you have to explode() the array before sending it to db...
Hope it will help!
If you really want to store an array in some table field, then better declare it as a JSON field. For this, your DB should have support for JSON type columns.
See here how to do this.
Once this is done, you can save arrays in that column, you can assign an array value to the model property and laravel will convert it to JSON while saving and also it will be converted to array while retrieving.
I created a Power BI Custom Data Connector, the idea is to be able to connect to SSRS Dataset using this Custom Data Connector I was able to do it but the resulting formatted json is different from what i expect.
Here's the result when I open the Custom Connector in Power BI, I expected a properly formatted table but the result is not.
Columns are List of Record contain the Column Names and Type
While the Row is a List of List containing the values for CustomerID and CustomerName.
Here's my code.
section Test.PQ.SSRS_Connector;
[DataSource.Kind="Asia.PQ.SSRS_Connector", Publish="Test.PQ.SSRS_Connector.Publish"]
shared Test.PQ.SSRS_Connector.Feed = Value.ReplaceType(SSRSConImpl, type function (url as Uri.Type) as any);
DefaultRequestHeaders = [
#"Accept" = "application/json;odata.metadata=minimal",
#"OData-MaxVersion" = "4.0"
];
SSRSConImpl = (url as text) =>
let
body= "",
source = Web.Contents(url, [ Headers = DefaultRequestHeaders, Content=Text.ToBinary(body)]),
json = Json.Document(source)
in
json;
Posting some sample JSON would be helpful, but based on the screenshots it seems like continuing your function as below may work:
// ... Your function code
json = Json.Document(source),
toTable = Table.FromRows(json[Rows], {"CustomerID", "CustomerName"}) // If there are more columns, consider extracting names dynamically from json[Columns]
// .... Any remaining code
Code is untested.
My component saves json array in database :
so i want to get distinct dates from all the fields.
database field contains values like this :
a:3:{i:0;s:16:"2013-02-24 00:00";i:1;s:16:"2013-02-23 00:00";i:2;s:16:"2013-02-22 00:00";}
What you have there is serialized data, not json encoded. I don't believe there is a way to grab just the dates out of the database using a mysql query (hence most people recommending that you don't store data in a serialized or even a json form).
You would want to grab the data from the database maybe like so (with the $query variable being the query you need to get data from the database. Since I have no idea what your database looks like, I'm not going to write the query)
$rows = JFactory::getDbo()->setQuery($query)->loadObjectList();
foreach ($rows as $row) {
$dates = unserialize($row->date_column);
// do something with the $dates variable, which is now an array of the three dates.
}