I am managing a project in Kohana 2.3.4 where I need to create an API for my android backend. What I am doing is send a query on my model which returns $result.
$query = "select product.deal_id,product.deal_key,p..."
$result = $this->db->query($query);
I am not sure if $result is an Object or and array This consists of 4 rows and 8 columns. I need to change $result to json format. I am currently doing this by echoing.
echo json_encode($result);
This return an empty json {}.
I am able to use the same query on my view by iterating through the $result
foreach ($result as $h){
echo $h->main_key;
}
Am I doing this right or is it that my $result on this connection has no rows?
I figured out I had use Kohana debug to know if my result was an object or an array. After calling the following
echo Kohana::debug($result);
I figured out that it was an object hence the empty result when converted to a json object. I had also tried getting an associative array with mysql_fetch_assoc which actually expected a mysql query object. This did
not work as the object was created by my ORM object. I then solved this by calling
$result = $this->db->query($query)->as_array();
This returned an array and solved my problem.
Related
I receive the following URL
/articles?difficulty=1,2
Inside the framework I want to receive an array from that - [1,2]. Is there any method in the framework to convert params like that into an array automatically? Can the framework do that? I can do like that explode(',', $params['difficulty']) - but I'm wondering whether this can be handled by the framework.
I don't want to pass params like that:
/articles?difficulty[]=1&difficulty[]=2
There is no helper in framework Request component for converting such values, it can be easily achieved with native PHP explode function. Use:
$array = explode(',', $string);
as you suggested.
But the wrapper of explode exists - \yii\helpers\StringHelper::explode(), it has additional options for trimming and skipping empty elements, you can use it too. But most of the times using regular explode should be enough.
try this
Use the Request class.
http://www.yiiframework.com/doc-2.0/yii-web-request.html
print_r(Yii::$app->request->get()); returns all get variables in an array. It's like doing print_r($_GET); in straight php.
If you want a specific $_GET variable you access it as follows:
Yii::$app->request->get('difficulty');
In your case it would be:
$success = Yii::$app->request->get('success');
$token = Yii::$app->request->get('token');
Then after that explode it with comma, so it will get converted into array.
$array = explode(',', $success );
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.
I am developing in symfony 1.4 using Doctrine ORM. I can't create a NOT IN where clause using an array with ids. This is my code:
$results = Doctrine_Query::create()
->from('Asset a')
->where('a.id NOT IN (?)', implode(',', $ids))
->execute();
The sql for this query that is generated is this one:
WHERE (a.id NOT IN ('1,5,6,7,8,9,10,11,12,13,14,15,17,18,20,24,25,26,29,30,28'))
As you can see is treating the array filled with ids like an string. I tried also without the implode of the array but I get this error:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
The array $ids containing the excluded ids is a plain numeric one.
I can't find out what is the correct syntax for this clause. Thanks
You have to use whereNotIn (old doc but still ok).
$results = Doctrine_Query::create()
->from('Asset a')
->whereNotIn('a.id', $ids)
->execute();
I have a stored procedure that I call upon using 'core_read' and query method. The results are then gathered using fetchAll(PDO::FETCH_ASSOC).
The data comes out perfectly. I can do a foreach on the array, and access data by array keys ($row['name']).
I would like to convert the associative array into a Varien_Object, so I could access the data using $row->getName() notation... Keeping it in Magento style... How would I perform such a conversion, if possible?
Pass your array to the constructor of Varien_Object
$object = new Varien_Object($array);
See the code for the constructor in lib/varien/object
I think you just can use:
foreach($rows as $row) {
$object = new Varien_Object();
$object->setData($row);
}
Thank you for your suggestions, and I think it would have worked if I had one line coming back from the stored procedure. Here's what I ended up doing:
foreach($rows as $row) {
$orders[] = new Varien_Object($row);
}