I am getting the data from DB as array and then outputting them as a JSON.
Some parts of my code:
private function getUsers( ){
$users = $this->db->resource(dbMapper::USER);
return $this->usersToArray( $users );}
private function usersToArray( $users ){
$result = array( );
foreach ($users as $user){
$result[] = array(
'id' => intval( $user->get('id') ),
'name' => $user->get('name')
);
}
return $result;
}
public function getAllData( ){
$result = array( );
$result['users'] = $this->getUsers( );
$results = print_r($result['users'], true); echo $results;
return $result;
}
and then I get the JSON from this result:
$data = $model->getAllData( );
$this->_helper->json( $data );
The JSON output looks like this (I removed the data for debugging), but even if there are no data I have a syntax error in first letter of Array:
In my browser I got this: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
and if I put that JSOn to JSON validator, I got this:
Parse error on line 1:
Array( [0] => A
^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
Array
(
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
Not sure what is wrong with the Array in JSON...
The issue was with incorrect characters in the username field in DB, that caused the JSON error.
Related
I'm sending an serialize ajax post request.
$.ajax({
type: 'post',
url: 'js/json.php',
data:{
'a': 'JRNLHDR-SAVE',
'formdata':$(_tr).find('[name^="formdata_jrnlhdr"]').serialize(),
},
success: function (data){
console.log(data);
},
error: function (er){
console.log(er);
}
});
Json data is correct
Array
(
[formdata_jrnlhdr] => Array
(
[153] => Array
(
[txid] => 153
[reference] => PH/21/JB0161
[description] => Adjustment Entry
)
)
)
I'm trying to read the data in php but it giving an error
Undefined index: txid, reference and description
foreach($journal_entry as $keys => $val){
print_r( $val['txid'] . $val['reference'] . $val['description'] );
}
That fault, because you miss your child array [153] .. and you call $keys in foreach, but still not using it.
Try this:
foreach($journal_entry as $val){
print_r( $val[153]['txid'] . $val[153]['reference'] . $val[153]['description'] );
}
Edit...
For dynamic array index, use this:
foreach($journal_entry as $key => $val){
print_r( $val[$key]['txid'] . $val[$key]['reference'] . $val[$key]['description'] );
}
Receiving an error It is not yet possible to assign complex types to properties when trying to put a new array into an object.
$file = '/path/file.xml';
$terms = array(
'Title' => [
'name' => 'name'
]
);
$term = json_decode( json_encode($terms), true);
$data = simplexml_load_file($file);
list($result) = $data->xpath('(//Key1/Key2)');
$number = count($result);
$data->Key1[$number] = $term;
$data->asXML($file);
When doing
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
$result = json_decode($response);
The response is
)]}'
["uvresp","03AJpayVHarkP_b40i5...stuff...VOrIy6m3ws",1,120]
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
The )]}' breaks things. I have no idea where that is coming from.
$g_recaptcha_response looks fine to me (what I am sending to google to verify).
The function to verify recaptcha
function verify_recaptcha ($g_recaptcha_response, $remote_address) {
# Verify captcha
$post_data = http_build_query(
array(
'secret' => 'secret',
'response' => $g_recaptcha_response,
'remoteip' => $remote_address
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($opts);
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
$result = json_decode($response);
return $result->success;
}
Thanks.
Ok my bad. The above code works, I had a problem further downstream (query was trying to use a column that did not exist).
hi guys i'm making an api in laravel 4 !! so im getting data that i store and return a json response everyting is good but just one thing :)
this is part of th code that return a response
if( $user->save() )
{
$id = DB::table('users')
->where('pseudo','LIKE',$pseudo)
->select('id')
->get();
return Response::json(array(
'status' => 'ok',
'message' => 'success',
'userId' => $id
));
}
the reponse is
{"status":"ok","message":"success","userId":[{"id":11}]}
but i whant to get this response
{"status":"ok","message":"success","userId":11}
how can i do it im trying but nothing chaged ! thx
You are setting 'userId' equal to the entire $id object returned from the query. You really just want
return Response::json(array(
'status' => 'ok',
'message' => 'success',
'userId' => $id['id']
));
Just something a little bit neater:
$id = DB::table('users')
->where('pseudo','LIKE',$pseudo)
->pluck('id'); // Retrieve first result, column 'id'
I have a stupid little question.
As I already know a select query in Laravel will always return an array of results,
I have this query:
$id = DB::select(
'select id from users where username = ?', array(Session::get('theuser')));
by inserting this id into my table
DB::table('characters')->insert(array(
'id' => $id,
'char_name' => $charname,
'char_dynasty' => $dynastyname,
'picture' => 'Male1.png'
));
I will get the error: ksort() expects parameter 1 to be array, string given.
How can I get rid of this? Thanks in advance!
At least one of $id, $charname or $dynastyname is an array and should not be.
You are using it wrong.
Below is a POC proving this.
The output is "Warning: ksort() expects parameter 1 to be array, integer given on line 13"
It runs as expected when providing 'id' => 'a'.
function insert(array $values)
{
if ( ! is_array(reset($values)))
{
$values = array($values);
}
else
{
foreach ($values as $key => $value)
{
ksort($value); $values[$key] = $value;
}
}
var_dump($values);
}
insert(array(
'id' => array('a'),
'char_name' => 2,
'char_dynasty' => 3,
'picture' => 'Male1.png'
));