In MySQL, I have a field name postcodes and the type is LONGTEXT. It stores several postcodes being separated by comma. How would I retrieve that and store it as an array for other use ?
you can use the PHP method explode().
one think you can't do, is to do a where x = x on it in the database.
In the model, you can set the mutator methods:
public function getPostcodesAttribute($value) {
return explode(',',$value);
}
public function setPostcodesAttribute($value) {
$this->attributes['postcodes'] = implode(',',$value);
}
Lets say that you have the result stored in a string like this:
$s = "6000,5447"; //$s = $array->postcodes;
you can get the each value on an index in an array using this:
$values= explode(",", $s);
echo $values[0]; // 6000
Or even better.. you can store it as json, and retrieve it as json in array format.
Store it as a JSON field in MySQL, Laravel encode and decode them when you retrieve and save them respectively
in your migration
$table->json('field_name');
then in the model
protected $json = ['field_name'];
then whenever you access the field, laravel will convert it to an array for you, you don't have to call any explicit methods.
Doc - https://laravel.com/docs/5.7/eloquent-mutators#attribute-casting
// the final array all the post codes are collected.
$postCodes = [];
foreach (Model::pluck('postcodes') as $stringCodes)
foreach (explode(',', $stringCodes) as $postCode) $postCodes[] = $postCode;
Related
is there anyway to make a query in MongoDB using a JSON and returning a object if one field of the json matches with some in the database?
for example, I have the this object called keysArray
{ house: 'true', garden: 'false' }
and I would like to make a query in Mongo passing this object as a query field and return if some object in my database matches with at least one of those fields :
keysArray.forEach(function(key){
collection.find({keysArray}, function(err, propertyMatch){
console.log(propertyMatch)
})
})
I got no objects back, even if I have one object in my database that matches these fields.
Thanks in advance
...and I would like to make a query in Mongo passing this object as a
query field and return if some object in my database matches with at
least one of those fields.
It sounds like OR logic - if I understood it well.
On this specific case it's not possible to pass in JSON-like object to query as it would be a implicit AND logic condition.
So you should build first a OR expression and use it in collection.find(), something like this:
var myjson = {'status': 32, 'profile': {$exists: false}};
function build_logic_or(json) {
var orExpr = [];
for (var field in json) {
var expr = {};
expr[field] = json[field];
orExpr.push(expr);
}
return {'$or': orExpr};
}
It would build an expression like this:
{"$or":[{"status":32},{"profile":{"$exists":false}}]}
So:
db.collection.find(build_logic_or(myjson))
I execute a postgresql query to a database using PDO and I get back as repsonse strings in the form of:
POINT(23.7336253085595 38.0002872112492)
How can I get the numbers of these strings and store them into to different variables?
That's my code in order to send the query question:
include 'postgreConnect.php';
$maxGid = 1084;
for ($rowPostGis=1; $rowPostGis<=$maxGid;$rowPostGis++){
$stmt = $dbconn->prepare("SELECT ST_AsText(ST_Transform(geom, 4326)) AS geom FROM part_athens_centroids WHERE gid = :rowPostGis;");
$stmt->execute(array('rowPostGis' => $rowPostGis));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$geom = $row['geom'];
echo($geom);
//echo($geom);
}
}
I would look into http://php.net/manual/en/function.explode.php this will convert your string into an array of strings that you can use http://php.net/manual/en/function.intval.php to convert each string to an int. You may need to crop the query result down to just the numbers for that use http://php.net/manual/en/function.substr.php.
I'm using the Jackson Object Mapper (com.fasterxml.jackson.databind.ObjectMapper) to parse a json string and need to count the number of times the word "path"
occures in the string. The string looks like:
"rows":[{"path":{"uid":"2"},"fields":[]},{"path":{"uid":"4"},"fields":[]},{"path":{"uid":"12"},....
Does anyone know which API option is most efficeint for achieving this?
To count total number of 'childs' inside the 'rows' root, you can use a code like this:
String inputJsonString = "{\"rows\":[{\"path\":{\"uid\":\"2\"},\"fields\":[]},{\"path\":{\"uid\":\"4\"},\"fields\":[]},{\"path\":{\"uid\":\"12\"},\"fields\":[]}]}";
ObjectNode root = (ObjectNode) new ObjectMapper().readTree( inputJsonString );
root.get( "rows" ).size();
If you need to get the exact count of 'path' occurrences, you may use the code like this:
int counter = 0;
for( Iterator<JsonNode> i = root.get( "rows" ).iterator(); i.hasNext(); )
if( i.next().has( "path" ) )
counter++;
System.out.println(counter);
I have this code
$graph_url = "https://graph.facebook.com/100000070848126/statuses?access_token=".$params['access_token'];
$status = json_decode(file_get_contents($graph_url),true);
echo $status->data->message;
and I'm having a problem on how to output data in the array $status. I just don't know how to call the items for this feed
The second parameter of json_decode() is whether to create an associative array or not.
json_decode ( string $json [, bool $assoc = false])
You specified that you do want an array, so the way you would access the values would be like this -
echo $status['data']['message'];
If you leave out the true parameter of the json_decode() function, you'll be able to access the values in a more object oriented way like the syntax in your question.
I am creating a store in CakePHP and have added a text field which stores a json array of all the categories the store falls into.
How do I do a cake find on all stores that fall into the "gardening" category?
json encoded field:
["gardening","books-and-toys"]
I am thinking some sort of in_array() find but not quite sure.
Many Thanks
You can use the PHP method json_decode and return the JSON object as an associative array by setting the second option of that function to true. You can then call array_search for example to return the corresponding key.
<?php
$categories = json_decode($json, true);
$categoryKey = array_search('gardening', $categories);
$categoryName = $categories[$categoryKey];
?>
You can then use $categoryName in a CakePHP find() call.