How to decode json in yii 1 - php

I have following codes in my controller:
public function actionCabinet($id){
$this->render('cabinet', array('model'=>$this->loadJson($id)) );
}
public function loadJson($id)
{
$jsonfile=ChForms::model()->findByPk($id, array("select"=>"json"));
$decodedJson=json_decode($jsonfile, true);
return $decodedJson;
}
Data is saved in json field in ChForm in json format. I am going to convert it to array. When I run this application, it displays following error message:
json_decode() expects parameter 1 to be string, object given
How can I fix this error?

Please try this,
public function loadJson($id)
{
$jsonfile=ChForms::model()->findByPk($id);
$decodedJson=json_decode($jsonfile->json, true);
return $decodedJson;
}

You can use json_encode like below,
public function loadJson($id)
{
$jsonfile=ChForms::model()->findByPk($id, array("select"=>"json"));
$decodedJson=json_encode($jsonfile, true);
return $decodedJson;
}

Related

Parse json array to find Id

So I'm new in laravel and I'm trying to create a rest api to return a specific item of my data database
http://127.0.0.1:8000/api/items/1010100203
And I'm getting []
So I have a array of json objects like
[{"No_":"1010100203","Price:"23","Description":"Item1"},{"No_":"1010100204","Price":"15","Description":"Item2"},{"No_":"1010100205","Price":"12","Description":"Item3"}]
in my database and I want to get my item with "No_" 1010100203.
In controller I have this function
public function find($id){
return Item::where('No_', 'like', $id)->get();
}
I've also tried to create a find function to only return $item and also returned []
I think I need to parse my array but I don't know how to do this..
And in my route api file,
Route::get('items/{id}', 'ItemController#find');
Also tried with Manager,
ItemController,
public function find($id){
$im= new ItemManager();
if (!empty($id))
$i=$im->GetItemIfExist($id);
return $i;
}
ItemManager
public function GetItemIfExist($id){
$result=\Illuminate\Support\Facades\DB::table('MR$Item')
->where('No_',$id)
->get();
return $result;
}
Why you use 'like'? Use '=' if you need exact match

Query in php on Codeigniter

I can't use my query in php when i use codeigniter.
I think that it's all ok but my code doesn't work.
What is it wrong ?
I need to cofront two parametres.
I have a table and i need colonn3
Models:
function get_colonn3($data){
$this->db->select("table.colonn3");
$this->db->where("table.colonn3",$data);
return $this->db->get("table.colonn3")->result_array();
Controller:
$colonn3_exsist = $this->my_model->get_colonn3($data)
if($colonn3_exsist):
echo "ok";
endif;
Remove table.colon3 from get(), maybe it will work
return $this->db->get()->result_array();
I believe is because you're passing the table name to the where statement and it expects a column.
CodeIgniter where documentation
function get_colonn3($data) {
$this->db->select("table.colonn3");
$this->db->where("colonn3", $data);
return $this->db->get("table")->result_array();
}
They also allow you to pass an array to the where parameters too:
function get_colonn3($data) {
$this->db->select("table.colonn3");
$this->db->where([
"colonn3" => $data
]);
return $this->db->get("table")->result_array();
}
CI also allows you to chain DB functions like so:
function get_colonn3($data) {
$this->db->select("table.colonn3")
->where([
"colonn3" => $data
]);
return $this->db->get("table")->result_array();
}
try this:
//model
function get_colonn3($data){
$this->db->select("colonn3");
$this->db->where("colonn3",$data);
return $this->db->get("table")->result_array();
//controller
$colonn3_exsist = $this->my_model->get_colonn3($data)
if($colonn3_exsist>0){
echo"ok";
}
You are passing the value to WHERE in a wrong way. You have to pass the values in an array without passing the table name.
You have to pass the array in WHERE clause like this if you have more than one parameter to compare from Database:
function get_colonn3($para1,$para2){
$this->db->select("table.colonn3");
$this->db->where(['columnname1' => $para1, 'columnname2' => $para2]);
return $this->db->get("table.colonn3")->result_array();
}
If you have only one parameter to compare then you can use this:
function get_colonn3($para1){
$this->db->select("table.colonn3");
$this->db->where('columnname1',$para1);
return $this->db->get("table.colonn3")->result_array();
}
Model
function get_colonn3($data){
return $this->db->select("table.colonn3 as col3")->from('table')->where(['table.colonn3' => $data])->get()->row()->col3;
}
Controller
$colonn3 = $this->my_model->get_colonn3($data)
if(!empty($colonn3)):
echo "ok";
endif;

PHP Trying to get property of non-object error in function

Need some help please. I am getting a 'Trying to get property of non-object' error in a function call that looks at an array.
The array I am calling is
$var = array(
"variableA" => "abc123",
"variableB" => "123456789"
);
The function I am using is
public function getJson($var)
{
$resource = sprintf("/info/%s/%s/json", $var->variableA, $var->variableB);
return $this->_restCall('GET', $resource);
}
I cant understand why the array values are not being passed through?
Could someone please help?
$var is an array not an object. So you need to use array syntax, not object syntax:
public function getJson($var)
{
$resource = sprintf("/info/%s/%s/json", $var['variableA'], $var['variableB']);
return $this->_restCall('GET', $resource);
}

PHP Class Diagram function returns 2 possible data types

I'm confused on how to draw a class diagram where a function returns either a boolean value, or an array:
private function isSuccessful(&$errors) {
if (sizeof($errors) > 0) {
return false; // failed
} else {
// $data is an array of key and value pairs
$data = $this->getData();
return $data;
}
}
Would I write something like this inside of the function section of my class diagram?:
- isSuccessful(errors : & array) : boolean | array
And also, if any other part of that line looks wrong, I'd love to see what it should look like. Thank you!

PHP: Return the last json object

Edit
Thanks for all the input on this, I did find error in my question so modifying now. Sorry for that.
I am trying to figure out how to return the last object in the JSON string I have rendered. The two functions I am working with:
public function revision($return = false)
{
$id = $this->input->post('galleryID');
$data = array('revision_count' => $this->revision->count_revision($id) );
if($return){
return json_encode($data);
}
else {
echo json_encode($data);
}
}
public function last_revision()
{
$allRevisions = json_decode($this->revision(),true);
return end($allRevisions);
}
The issue is that end() returns error stating that 1st parameter should be array.
Thanks for any help on this.
It is important to note here that json_decode returns an instance of stdClass by default. Try using json_decode($jsonstring, true) to return the JSON as a PHP associative array.
However, You haven't included what the $this->revision() method does. Could you possibly show that portion of the code, since that is the function you are getting a return value from?
Edit:
Alright, after we saw the right function in your code, here are a couple of things I would like to say:
You have added a $return parameter to your revision method, but you aren't using it when you need to. You should change $this->revision() to $this->revision(true) in your last_revision method.
If you're going to return data from the revision() method, there's not much of a point in json_encodeing it, just to json_decode the result. Just pass back the raw data array.
Once you have changed both of these things, this should work:
$allRevisions = $this->revision(true); return end($allRevisions['revision_count']);
You can change the edit_function() to:
public function edit_revision($return = false)
{
$galleryID = $this->input->post('galleryID');
$revisionID = $this->input->post('revisionID');
$data = array('revision_images' => $this->revision->get($galleryID, $revisionID) );
if($return)
return json_encode($data);
else
echo json_encode($data);
}
and then:
public function last_revision(true)
{
$allRevisions = json_decode($this->revision());
return end($allRevisions);
}
Maybe you need convert that json output to an php array (json_decode() function), then you could get the last item with array_pop() function:
https://php.net/array_pop

Categories