Multi-dimensional array to JSON - php

I am trying to print my data and encode it into JSON. I'm extracting the data from my MySQL database for a quiz. The data that I am trying to extract 1 question, 1 category and 4 options to make a set of quiz. I think I nearly got it how to make it work but I failed to find out how. This was the result check in this link.
Paste the JSON to this link so that you can easily format it. From that JSON data, I want to output like this for each question:
"What is the approximate number of islands that comprise the Philippines?",
"Philippine Geography",
[
{
"quiz_choice_id":"5",
"choice":"7000",
"is_correct_choice":"1"
},
{
"quiz_choice_id":"6",
"choice":"6000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"7",
"choice":"8000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"8",
"choice":"9000",
"is_correct_choice":"0"
}
],
This is my code for that:
<?php
/**
* Created by PhpStorm.
* User: Muhammad
* Date: 19/04/2016
* Time: 00:46
*/
require_once('Database_Connect.php');
$questions_query = "SELECT question, quiz_choice_id, choice ,is_correct_choice, category FROM category_question cq, question q, question_choices qc WHERE q.quiz_question_id = qc.quiz_question_id AND q.cat_ques_id = cq.cat_ques_id LIMIT 10";
$questions_query_result = mysqli_query($con, $questions_query) or die("error loading questions");
$questions_results = array();
encode_result($questions_query_result);
function encode_result($result)
{
//$response = array();
$questions = array();
//$choices = array();
while ($r = mysqli_fetch_array($result)) {
//$response[] = $r;
//$questions = array('question' => $r[0]);
$questions[] = $r['question'];
$questions[] = $r[4];
$choices[] = array('quiz_choice_id' => $r[1], 'choice' => $r[2], 'is_correct_choice' => $r[3]);
$questions[] = $choices;
//array_push($questions, $questions['question']);
//array_push($questions_results, $questions);
}
echo json_encode(array('questions'=>$questions));
}
mysqli_close($con);
The design of the database is this:
I can't find a way to make it work because from the database quiz_choice_id,choice, is_correct_choice are in a different table but I combined all into one table as you can see in my SQL statement $questions_query. Please let me know how can I fix this. Thanks in advance.

Do you want the json to look like this?
[ {
"question" : "What is the approximate number of islands that comprise the Philippines?",
"category" : "Philippine Geography",
"choices" : [
{
"quiz_choice_id":"5",
"choice":"7000",
"is_correct_choice":"1"
},
{
"quiz_choice_id":"6",
"choice":"6000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"7",
"choice":"8000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"8",
"choice":"9000",
"is_correct_choice":"0"
}
]
},{
..... // next question
}]
You'll have to do something like this, but I have no idea what the results of the query is.
$questions = array();
while ($r = mysqli_fetch_array($result)) {
$key = $r['quiz_question_id']; // you need a key here that is unique to the question, so i think this will do looking at the schema you posted, this will group them in that segment of the array.
if( !isset( $questions[$key] ) ){
//if we never had this question, create the top level in the results
$questions[$key] = array(
'question' => $r['question'],
'category' => $r['category'],
'choices' => array()
);
}
//add in the choices for this question
//on the next iteration, the key is the same so we only do this part
//and append that rows choices to the previous data using $key to match the question
$questions[$key]['choices'][] = array('quiz_choice_id' => $r['quiz_choice_id'], 'choice' => $r['choice'], 'is_correct_choice' => $r['is_correct_choice']);
}
Make sure to add quiz_question_id to your query if this is the questions id, unique identifier. Essentially this will group them together, as this will be the same for each row with that question's choices.
I added string keys to the output, I wouldn't mix using string keys and numbered index. I hope I mapped them out right.
Also I haven't tested this at all, so sorry if there are any syntax errors.

Related

How generate column using for loop

How to make this function produce column occLvl_ loop 3 time and using explode to fetch each value from sql CONCAT. So the result will become like this.
[{
"accommodationID": "LA56",
"occLvl_0": "40.00",
"occLvl_1": "70.00",
"occLvl_2": "90.00"
}]
function getOccLevel(){
global $ehorsObj;
$occArray = array();
$sql = "SELECT accommodationID, GROUP_CONCAT(occLevelDesc) AS occLevels
FROM tblSamAccOccLevels
WHERE ACTIVE = 'y'
GROUP BY accommodationID
ORDER BY accommodationID ASC, occLevelDesc ASC ";
$GetResult = $ehorsObj->FetchData($sql, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
while ($row = $GetResult->fetch()){
$occArray[] = array(
'accommodationID' => $row['accommodationID'],
);
//seem the method below is not working
for ($j = 0; $j < 3; $j++) {
$occArray["occLvl_".$j] = explode(",", $row['occLevels'])
}
}
header("Content-type: application/json");
$result = json_encode($occArray);
echo $result;
}
Result of the query
accommodationID occLevels
LA56 40.00, 70.00, 90.00
Making numerically named object properties/variables (occLvl_0 etc.) is generally a bad idea as it makes it difficult to work with them in any regular manner (e.g. using a loop). It is better practice to put the values into an array:
while ($row = $GetResult->fetch()){
$occArray[] = array(
'accommodationID' => $row['accommodationID'],
'occLvl' => explode(",", $row['occLevels'])
);
}
This will give you an output JSON that looks something like:
[
{
"accommodationID": "LA56",
"occLvl": [
40,
70,
90
]
},
{
"accommodationID": "PQ45",
"occLvl": [
30,
60,
100
]
},
...
]
And in your JS you can then iterate over the occLvl array to get the values.
If you need the data in the form you describe, then you need to iterate over the exploded occLevels value to generate the individual values, pushing them with the accommodationID into a new array and then pushing that array to $occArray:
while ($row = $GetResult->fetch()){
$this_occ = array(
'accommodationID' => $row['accommodationID'],
);
foreach (explode(",", $row['occLevels']) as $key => $occLvl) {
$this_occ["occLvl_$key"] = $occLvl;
}
$occArray[] = $this_occ;
}
Explode returns an array. You should explode before the loop and iterate through the result of that explode in the loop

query inside while loop only shows 1 result

i'm making a while loop in php and it all goes well but the problem is that
I don't only want to get the id of the user but also some other stuff that is inside another table, so when I go ahead and make a query inside this while loop and select everything from that second table (where the id is equal to the id of the result from the first query), it only returns 1 result...
So this is the code that I currently have:
public function getFriends($id)
{
global $params;
$get = $this->db->select("{$this->DB['data']['friends']['tbl']}", "*",
array(
"{$this->DB['data']['friends']['one']}" => $id
)
);
if($get)
{
while($key = $get->fetch())
{
$query = $this->db->query("SELECT * FROM {$this->DB['data']['users']['tbl']}
WHERE {$this->DB['data']['users']['id']} = :id",
array(
"id" => $key->{$this->DB['data']['friends']['two']}
)
);
while($row = $query->fetch())
{
$params["user_friends"][] = [
"id" => $key->{$this->DB['data']['friends']['two']},
"name" => $row->{$this->DB['data']['users']['username']},
"look" => $row->{$this->DB['data']['users']['figure']}
];
}
}
}
else
{
$params["update_error"] = $params["lang_no_friends"];
}
}
Thanks in advance!
Please help me out!
In the absence of answers, I don't know what db framework you are using behind the scenese...PDO, mysqli_, or (hopefully not) mysql_. But, in any case, the problem might be that your second query stops the first from continuing. I would use PDO->fetchAll() to get them all...but you say you can't do that...so, looping the first and loading those results into an array is the first thing I would do to see if this is the problem:
public function getFriends($id)
{
global $params;
$get = $this->db->select("{$this->DB['data']['friends']['tbl']}", "*",
array(
"{$this->DB['data']['friends']['one']}" => $id
)
);
$firstResults = array();
if( $get ) {
while( $key = $get->fetch() ) {
$firstResults[] = $key;
}
}
else
{
$params["update_error"] = $params["lang_no_friends"];
}
foreach( $firstResults AS $key )
{
$query = $this->db->query("SELECT * FROM {$this->DB['data']['users']['tbl']}
WHERE {$this->DB['data']['users']['id']} = :id",
array(
"id" => $key->{$this->DB['data']['friends']['two']}
)
);
while($row = $query->fetch())
{
$params["user_friends"][] = [
"id" => $key->{$this->DB['data']['friends']['two']},
"name" => $row->{$this->DB['data']['users']['username']},
"look" => $row->{$this->DB['data']['users']['figure']}
];
}
}
}
If this doesn't work, then we need more data...e.g. what is the query generated? When you run it manually does it return more than one result? If you get rid of the inner-query, does this fix it? etc.
The first step when diagnosing PHP and Mysql issues is to add lines to your code that tell you what each line is doing (declare each time a loop is entered; when each mysql query is run, spit out the query string) so you can narrow down where the problem is. Often this makes you feel stupid in retrospect: "Duh, this query didn't return anything because I formatted the record ID wrong" and so forth.
The code snippet you've provided above isn't super helpful to me. I'm a troubleshooter (not a parser) so I need diagnostic data (not straight code) to be of any more help than this.

json DataTable in googlecharts

I am having a bit of trouble getting the proper format of json string.
I have a database table that looks something like this:
Table Columns: emp month sales
Table rows: Bob 1 100
Bob 2 150
Jane 1 125
Jane 2 130
Mary 1 110
Mary 2 130
Within drawChart(), I can create something like this statically:
var data = google.visualization.arrayToDataTable([
['Month', 'Bob', 'Jane', 'Mary],
['Jan', 100, 125, 110],
['Feb', 150, 130, 130]
]);
In the end, the json string needs to look like this:
{"cols":[{"label":"Month","type":"string"},
{"label":"Bob","type":"number"},
{"label":"Jane","type":"number"},
{"label":"Mary","type":"number"}],
"rows":[{"c":[{"v":100},{"v":125},{"v":110}]},
{"c":[{"v":150},{"v":130},{"v":130}]}]}
But I am having trouble pulling from the table to come up with proper json formatting that is equivalent to the above. I am following the steps from here... PHP MySQL Google Chart JSON - Complete Example
But that example is only for a single data set. if you were to add multiple weeks instead of having just one data set, how do run the query?
To get your data in the format you want, you have to pivot your data. Some databases support pivotting, but others like MySQL don't. If you are stuck without pivot support, then you have to resort to trickery to make it happen. Here's one way you could do it:
SELECT
month,
SUM(if(employee = "Bob", sales, 0)) AS Bob,
SUM(if(employee = "Jane", sales, 0)) AS Jane,
SUM(if(employee = "Mary", sales, 0)) AS Mary
FROM myTable
GROUP BY month
This requires that you know ahead of time what the employee names are so that you can write the SQL statement (either when you write the code, or you could pull them from another SQL query and write a dynamic SQL query).
Asgallent, thank you. Your response gave me the direction I needed. I was able to do it all dynamically via SQL. I made two queries: 1 to the "saleperson" table to get the names, and then another to pivot the data as you suggested. For anyone else that might find this helpful, here is the code I have.
The queries (Note: I am using codeigniter):
$sp_qry = $this->db->query('select * from salespeople');
$qryString="";
foreach ($sp_qry->result_array() as $row)
{
$qryString.= ",SUM( IF( `salespeople_id` =" . $row['salespeople_id'] . ", `num_sold` , 0 ) ) AS " . $row['name'];
}
$qry= "SELECT `month` " . $qryString . " FROM `product_sales`
GROUP BY `month`";
$query = $this->db->query($qry);
return $query->result_array();
and in my viewing page
$rows = array();
$table = array();
$cols = array();
$cols[] = array('label' => 'Month', 'type' => 'string');
foreach ($salespeople as $sp)
{
$cols[] = array('label' => $sp['name'], 'type' => 'number');
}
$table['cols'] = $cols;
foreach ($sales as $chart_item)
{
$tmp=array();
$tmp[] = array('v' => (string) $chart_item['month']);
foreach ($salespeople as $sp)
{
$name=$sp['name'];
$tmp[] = array('v' => (int) $chart_item[$name]);
}
$rows[] = array('c' => $tmp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);

Document exact matching

Let's say we have a following MongoDB-collection:
{id:1, data:"some_data"}
{id:2, data:"some_data"}
{id:3, data:"some_data"}
{id:4, data:"some_data"}
I also have an php-array with element "6" that is not in the collection
$q = [1,3,6];
If I query the collection like this:
$cursor = $db->col->find(['id' => ['$in' => $q]]);
I'm getting documents with id's 1 and 3
But I'd like to have an empty result because the id 6 do not exist in the collection,
how can I exact match the collection?
Can I do this with just one query?
If id is unique:
var result = db.col.find({id:{$in:q}});
if(result.count() == q.length) {
//handle result
} else {
//handle empty result
}
You can not do this just with one mongodb query, because it does not have such functionality.
Also I don't understand why exactly do you need this, you can achieve it doing something like this:
$q = [1,3,6];
$res = array();
$cursor = $db->col->find(['id' => ['$in' => $q]]);
foreach($cursor as $val){
$res[$val['id']] = $val['data'];
}
foreach($q as $val){
if (!isset($res[$val])) $res[$val] = ''; // to show that there is nothing in the results.
}
P.S. I have not tried this code, so there might be errors.

Create multidimensional array from database query

I have a database that stores accounts and subscriptions. An account can contain accounts and subscriptions and have more than three parent levels.
A row from the database query result looks something like this:
$array = (0 => array("level" => "2", "accountno" => "123124234", "accountname" => "Hansel", "parentaccountno" => "000213123", "subscription" => "5423213213", "username" => "Gretchen");
(Level 1 means that it is top level and it will have PARENTACCOUNTNO = null).
From this I am trying to create a multidimensional array which looks something like this:
accounts:
000213123
accounts:
123124234
name: Hansel
subscriptions:
5423213213
username: Gretchen
This is the code I have so far, which works really well on the first two levels. When there are are three levels or more things become more complicated, since each account only knows about its own parent:
$hierarchy = array();
foreach ($decode as $row) {
$accountno = $row["ACCOUNTNO"];
$msisdn = $row["MSISDN"];
if ($row["PARENTACCOUNTNO"] == null)
$base_array_key = $hierarchy["accounts"];
$base_array_key[$accountno] = array("name" => $row["ACCOUNTNAME"], "accounts" => array(), "subscriptions" => array());
$hierarchy["accounts"][$accountno]["accounts"]["321323123213"] = "blaha";
if ($row["MSISDN"] != null)
$hierarchy["accounts"][$accountno]["subscriptions"][$msisdn] = array("enduser" => $row["ENDUSER"]);
}
The solution I have in mind right now is to pass the base key from each iteration to the next iteration, and then if for example the previous level was two levels above this one just use some sort of string replace to remove the last two parts of the key.
Any better ideas? I am quite sure this is a shitty way of doing what I am trying to accomplish.
Do all your results look like that ? So then, where's are the parent accounts ? Or am i missing something ? I would simple rewrite the query to get a more "linear" result, like:
parentaccountno | accountno | accountname | bla
-----------------------------------------------
000213123 | 123124234 | Hansel | abc
When every result row looks like this, you can simple create your final array like that:
foreach ($decode as $row) {
$my_array = [ $row["parentaccountno"] ][ $row["accountno"] ]["accountname"] = $row["accountname"];
$my_array = [ $row["parentaccountno"] ][ $row["accountno"] ]["bla"] = $row["bla"];
}
Create a temporary associative array:
$all_records = array();
Populate it with the results you find in the database:
while($result = get_results_from_db()) {
$accountno = $result[accountno];
$all_records[$accountno] = $result;
}
Now you have an array indexed by account numbers so it's easy to find any record if you know it's account number. So you iterate through the array to associate children to parents:
$top_level_records = array();
foreach($all_records AS $record) {
$parentaccountno = $record[parentaccountno];
if(!empty($parentaccountno)) {
$parent = &$all_records[$record[parentaccountno]];
if(empty($parent['accounts']))
$parent['accounts'] = array()
$parent['accounts'][] = $record;
}
else {
$top_level_records[] = $record;
}
}
I appreciate the answers, but I think the problem is my database query which should contain records of all the parents of an account and not just the parent, using something like SYS_CONNECT_BY_PATH in Oracle SQL.

Categories