JSON_encode not working with GROUP_CONCAT database selection - php

I'm breaking my brain on a array from a query in MYSQL that i want to pass to a javascript array.
In the query i select the array with a GROUP CONCAT and the outcome looks like:
1358121600,1,1,0,0,0,0,0,0,1358380800,2,2,0,0,0,0,0,0,1358640000,1,1,0,0,0,0,0,0,1360454400,3,3,0,0,0,0,0,0,1360972800,1,1,0,0,0,0,0,0
But if i use JSON_Encode like this:
<?php echo 'var prijzen = new Array('.json_encode($array_prijzen).');'; ?>
I looks like the array is filled and i can also alert the array, but if i alert prijzen[0] it gives "undefined".

The following code should fix your problem:
<?php echo 'var prijzen = ['.$array_prijzen.'];'; ?>

Building on #datasages 's answer. If $array_prijzen is a genuine php array, then the following will work. I think datasages's answer is based on the fact that your variable named $array_prijzen is actually a string (which seems to be the case). But if it's an array, then do the following (i created a five element array as an example):
<?php $array_prijzen = array(1358121600,1,1,0,0); echo 'var prijzen = ['.implode(",",$array_prijzen).'];'; ?>

Related

Get value from Array inside of an Array

I have the following string output if I run print_r($val):
{"next_offset":-1,"records":[{"id":"e3266222-5389-11ed-ab30-0210c01ad3d2","name":"That is a nice name"}]}
Now, I need the value of attribute "id". Sounds simple but I'm not able getting there.
Does something like this work?
I'm assuming $val is a json string.
<?php
$val = "{\"next_offset\":-1,\"records\":[{\"id\":\"e3266222-5389-11ed-ab30-0210c01ad3d2\",\"name\":\"That is a nice name\"}]}";
$val = json_decode($val);
print_r($val->records[0]->id);
?>

Show only data in array in var_dump

I want to show only 1 data in array.
My code right now:
var_dump($ranked);
In others cases, I have used echo $ranked->tier but in array this won't work.
You can do it like this:
echo $ranked['tier'];
var_dump($ranked['tier']);
or:
echo $ranked['tier'];

PHP Database Results > Array > JSON (without deprecated mysql_fetch_assoc)

I've been searching everywhere for a definitive answer to what seems to be a really simple task - unfortunately all the solutions I can find (and there are a lot) use mysql_fetch_assoc which is deprecated.
All I'm trying to do is to take a two column set of results from a database and echo in JSON format - I've managed everything fine except for one bit - I can't work how to get the values in to a two dimensional array (two column array) using array_push. I just end up with my two column values merged in to one array. Here's a stripped version of what I've done:
header('Content-Type: application/json');
$mostPopularStm = $sparklyGenericPdoObj->prepare("SELECT views, thing FROM X");
$mostPopularStm->execute();
$mostPopularRS = $mostPopularStm->fetchAll();
echo '{data:';
$mostPopularJson = array();
foreach ($mostPopularRS as $mostPopularItem)
{
array_push($mostPopularJson, $mostPopularItem["views"], $mostPopularItem["thing"]);
}
echo json_encode($mostPopularJson);
echo '}';
This is producing output like this:
{data:["Monkeyface","43","Giblets","25","Svelte","22","Biriani","21","Mandibles","20"]}
But what I need is this:
{data:["Monkeyface":"43","Giblets":"25","Svelte":"22","Biriani":"21","Mandibles":"20"]}
I know I can create something manually to do this, using json_encode to format the string on each loop but it seems inefficient.
Any pointers would be hugely appreciated!
Your current array is like
array(0 => 'Monkeyface', 1 => 43, /* ... */);
but you need like
array('Monkeyface' => 43, /* ... */);
Replace
array_push($mostPopularJson, $mostPopularItem["views"], $mostPopularItem["thing"])
By
$mostPopularJson[$mostPopularItem["thing"]] = $mostPopularItem["views"];
And
echo '{data:';
echo json_encode($mostPopularJson)
echo '}';
Better to use:
echo json_encode(array('data' => $mostPopularJson));
As kingkero said, you will never get your expected result because it is invalid:
{data:["Monkeyface":"43" ...
Correct:
{ "data": { "Monkeyface": "43" ...
Compose your array like so:
$mostPopularJson [$mostPopularItem["thing"]] = $mostPopularItem["views"];

How to use array variable in PostgreSQL array_append()?

I would like to use array variable in PostgreSQL array_append(). If I use array directly then its works but not when using variable.
$name= {1,2,3};
$Name_key_array={4};
I would like to find result by using following way-
$name='array_append(name, $Name_key_array)';
or
"SELECT array_append($Books->name, $Name_key_array) as b";
Waiting to see some great ideas.
Thanks to all
personally, I use json.
<?php $json_var = json_encode($array)
$sql="insert into blah (arr) values (select array_agg(a::text) from json_array_elements(:json_var)";

Passing multiple variable for php page

Hey guys I need help on passing multiple values for my PHP
http://s596.beta.photobucket.com/user/kingbookal/media/Capture.png.html?sort=3&o=0
That's the code from the 1st page and for the next page it is
$year = $_GET['yearlevel'];
I tried to alert the value but its null.
Please guide me well..
https://www.dropbox.com/sh/fokl2hnrjtpsfbn/Rcm8EfApm1
This is the link for my scripts
Do you want to echo the value from a key inside an array? Then you do like this:
echo $rowProfName['professor_name'];
If it doesn't return anything, you could check if a key exists in the array by using the following code:
if(!isset($rowProfName['professor_name']))
{
echo "Array key 'professor_name' is not set.";
}
If you want to check what your array contains, simply run this code:
$yourArray=array("value1","value2");
print_r($yourArray);
//If you want clean output as HTML, use this:
echo "<pre>";
print_r($yourArray);
echo "</pre>";
I hope I understood your question.

Categories