I have this results from MySQL:
+-----------------+----------+-----------------+
| SUPPLIED_AMOUNT | DATE_ISO | DATE_JAVASCRIPT |
+-----------------+----------+-----------------+
| 5919.00 | 20150716 | 1436994000000 |
| 4389.00 | 20150717 | 1437080400000 |
| 12069.00 | 20150718 | 1437166800000 |
| 10220.00 | 20150720 | 1437339600000 |
| 9444.00 | 20150721 | 1437426000000 |
| 8630.00 | 20150722 | 1437512400000 |
| 9009.00 | 20150723 | 1437598800000 |
| 7324.00 | 20150724 | 1437685200000 |
| 4295.00 | 20150725 | 1437771600000 |
| 4398.00 | 20150726 | 1437858000000 |
| 3385.00 | 20150727 | 1437944400000 |
+-----------------+----------+-----------------+
I need to convert to a similar json:
[1253145600000,26.36],
[1253232000000,26.43],
[1253491200000,26.29],
[1253577600000,26.35],
[1253664000000,26.50]
This is my current PHP code:
$supplied = $service->getSupplied();
if (count($supplied)>0) {
foreach ($supplied as $key=>$value) {
$chart_data['data'][] = $value['DATE_JAVASCRIPT'].','.$value['SUPPLIED_AMOUNT'];
}
}
But it render a similar JSON:
"data": [
"1437004800000,5919.00",
"1437091200000,4389.00",
"1437177600000,12069.00",
"1437350400000,10220.00",
"1437436800000,9444.00"]
Thank you very much
Don't put ['data'] into your array, then:
$chart_data[] = array($value['DATE_JAVASCRIPT'],$value['SUPPLIED_AMOUNT']);
^^^^
And note the change to array(). You're embedding what amounts to CSV strings in your array, which seems weird...
This part of code should work:
foreach ($supplied as $key=>$value) {
$chart_data[] = [$value['DATE_JAVASCRIPT'],$value['SUPPLIED_AMOUNT']];
}
$chart_data['data'][] = [$value['DATE_JAVASCRIPT'].','.$value['SUPPLIED_AMOUNT']];
If you don't need 'data':
$chart_data[] = [$value['DATE_JAVASCRIPT'].','.$value['SUPPLIED_AMOUNT']];
You should take part of json_encode, by formatting your array and using this function to take back a well formatted JSON file. It will avoid some code misconception and errors :
$supplied = $service->getSupplied();
if (count($supplied)>0) {
foreach ($supplied as $key=>$value) {
$date = $value['DATE_JAVASCRIPT'];
$supplied_amount = $value['SUPPLIED_AMOUNT'];
$chart_data[] = [$date, $supplied_amount];
}
$chart_data = json_encode($chart_data);
print_r($chart_data);
# chart_data : example of JSON output
# [
# [12092016, 12.80],
# [12122016, 57.90],
# [12112016, 48.81]
# ]
}
Related
I need help to make query to group by content of field. I have the following Database:
+----+--------------+-----------------+------------------------------+
| id | depart_kode | depart_task | value |
+----+--------------+-----------------+------------------------------+
| 1 | 001 | Create Profil | Some Value |
| 2 | 001.12 | Create Profil | Some Value |
| 3 | 001.452 | Create Profil | Some Value |
| 4 | 002.914 | Create Profil | Some Value |
| 5 | 002 | Create Profil | Some Value |
| 6 | 003 | Create Profil | Some Value |
+----+--------------+-----------------+------------------------------+
I need to make a query where I group by depart_kode and I should print by the first 3 digits like this in format json
"001":[
{
"depart_kode":"001,
and other
},
{
"depart_kode":"001.12"
},
],
"002":[
{
"depart_kode":"002"
}
]
something like that, most importantly they will be grouped in an array based on the first 3 numbers like 001.
and json just an example, which I want to split into an array based on the first 3 digits
Something like this?
$departs = [];
foreach ($sqlRows as $row) {
$departKey = substr($row['depart_kode'], 0, 3);
if (!array_key_exists($departKey, $departs)) {
$departs[$departKey] = [];
}
$departs[$departKey][] = $row['depart_kode'];
}
Working example.
references
substr - get a specific part of a string (first 3 chars in our example)
array_key_exists - checks if the key in an array exists
I'd like to select all period of current term (as now FY80) + 3 period only of last term (FY79) from m_period table
Here's the table m_period looks :
+----+------+-------------+--------+
| id | term | period | status |
+----+------+-------------+--------+
| 1 | FY79 | 79::2020/01 | close |
| 2 | FY79 | 79::2020/02 | close |
| 3 | FY79 | 79::2020/03 | close |
| 4 | FY79 | 79::2020/04 | close |
| 5 | FY79 | 79::2020/05 | close |
| 6 | FY79 | 79::2020/06 | close |
| 7 | FY80 | 80::2020/07 | open |
| 8 | FY80 | 80::2020/08 | open |
| 9 | FY80 | 80::2020/09 | open |
| 10 | FY80 | 80::2020/10 | open |
| 11 | FY80 | 80::2020/11 | open |
| 12 | FY80 | 80::2020/12 | open |
| 13 | FY80 | 80::2021/01 | open |
| 14 | FY80 | 80::2021/02 | open |
| 15 | FY80 | 80::2021/03 | open |
| 16 | FY80 | 80::2021/04 | open |
| 17 | FY80 | 80::2021/05 | open |
| 18 | FY80 | 80::2021/06 | open |
+----+------+-------------+--------+
my php script stuck from here :
<?php
function getPeriod()
{
$prev_term = (int)substr($this->session->userdata('term'),2,2) - 1;
$this->db->select('period');
$this->db->where('term', $this->session->userdata('term'));
$this->db->where('term', $prev_term);
$this->db->order_by('period', 'ASC');
return $this->db->get('m_period')->result_array();
}
I've read CI documentation and couldn't find how to use CASE in codeigniter, or there is another way instead of using CASE ?
can someone help me , how to do this ?
Note : I store current term on session
You can use limit() method.
Then, You have to have two separate functions.
function getPrevPeriod()
{
$prev_term = (int)substr($this->session->userdata('term'),2,2) - 1;
$this->db->select('period');
$this->db->where('term', $prev_term);
$this->db->order_by('period', 'ASC');
$this->db->limit(3);
return $this->db->get('m_period')->result_array();
}
function getCurrentPeriod()
{
$this->db->select('period');
$this->db->where('term', $this->session->userdata('term'));
$this->db->order_by('period', 'ASC');
return $this->db->get('m_period')->result_array();
}
Then you can use array_merge() to combine arrays.
$periods = array_merge($prev, $current);
Edit: With one query
function getPeriod()
{
$prev_term = (int)substr($this->session->userdata('term'),2,2) - 1;
$this->db->select('term','period');
$this->db->or_where_in('term',[$this->session->userdata('term'),$prev_term])
$this->db->order_by('period', 'ASC');
$result = $this->db->get('m_period')->result_array();
$output = [];
$n = 0;
foreach($result as $row){
if($row["term"] == $prev_term){
if($n <= 3){
$output[] = $row;
$n++;
}
}else{
$output[] = $row;
}
}
return $output;
}
So i have database table named usermeta and have table structure like this :
-----------------------------------------------------------
| ummeta_id | user_id | meta_key | meta_value |
-----------------------------------------------------------
| 1 | 1 | fullname | John Doe |
| 2 | 1 | birthplace | New York |
| 3 | 1 | birthdate | 1990/01/01 |
| 4 | 1 | mobile | 0812-3456-7890 |
| 5 | 1 | email | john.doe#mail.com |
| 6 | 2 | fullname | Jon Wick |
| 7 | 2 | birthplace | Washington DC |
| 8 | 2 | birthdate | 1985/10/21 |
| 9 | 2 | mobile | 0890-1234-5678 |
| 10 | 2 | email | wickjohn#mail.com |
And i try to generate json data for all data from this database using Codeigniter (v 3.1.9) using Controller and Model.
This is my Model (model name: db_usermeta)
function userslist()
{
$query = $this->db->select('*')
->from('usermeta')
->get();
return $query->result();
}
This is my Controller
public function userlist()
{
header('Content-Type: application/json; charset=utf-8');
$query = $this->db_usermeta->userslist();
$json_data = array();
foreach ($query as $key)
{
$json_data[$key->meta_key] = $key->meta_value;
}
echo json_encode($json_data);
}
The result when i open using my browser to check the json data using web developer tool is only show last record, in this case only show data from user_id 2, like this:
{
"fullname":"John Wick",
"birthplace":"Washinton DC",
"birthdate":"1985/10/21",
"mobile":"0890-1234-5678",
"email":"wickjohn#mail.com"
}
What I want to achieve is to show all json data nested like this:
"data": [
{
"fullname":"John Doe",
"birthplace":"New York",
"birthdate":"1990/01/01",
"mobile":"0812-3456-7890",
"email":"john.doe#mail.com"
},
{
"fullname":"John Wick",
"birthplace":"Washinton DC",
"birthdate":"1985/10/21",
"mobile":"0890-1234-5678",
"email":"wickjohn#mail.com"
}
]
How can i achieve this? Did I make a mistake on my controller and model. I really appreciate your help.
your $key->meta_key is overwriting for every record. that's why only last record appeared. You don't actually need to loop through to get json data.
public function userlist()
{
header('Content-Type: application/json; charset=utf-8');
$query = $this->db_usermeta->userslist();
$json_data = array(array());
$user_id_map = array();
$index = 0;
foreach ($query as $key)
{
if(!isset($user_id_map[$key->user_id])){
$user_id_map[$key->user_id] = $index++;
}
$currentIndex = $user_id_map[$key->user_id];
$json_data[$currentIndex][$key->meta_key] = $key->meta_value;
}
echo json_encode($json_data);
}
just change your controller code to this and this will return json data.
Since the meta key fullname is same for both records, you need to change the key name to something unique
foreach ($query as $key)
{
$json_data[$key->meta_key] = $key->meta_value;
}
Change $json_data[$key->meta_key] to $json_data[$key->meta_key.$key->user_id]
or simply change it to $json_data[$key->ummeta_id]
I have a MySql table and I want to list things by type and insert headers. What type of query would I use?
From This:
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | f | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Dalli | Alli | canine | m | 2001-12-20 | NULL |
| Tara | David | canine | f | 2002-05-17 | NULL |
| Mimi | Alli | guinea pig | m | 2004-05-17 | NULL |
To this:
<h2>Cat</h2>
<ul>
<li>Fluffy</li>
<li>Claws</li>
</ul>
<h2>Dog</h2>
<li>Buffy</li>
<li>Fang</li>
<li>Bowser</li>
</ul>
etc.
Don't try to do all of this with SQL (just do a standard select query), use PHP to group the result, then present it. The best way to do this would be to have an object that will do the grouping for you, as you'll probably need it more than once. For example, your class could look something like this:
<?php
class Arrays
{
public static function group($array,$key)
{
if(NULL == $array)
return NULL;
$grouped = NULL;
foreach($array as $item)
{
$grouped[$item[$key]][] = $item;
}
return $grouped;
}
}
?>
And your use case could be something like:
<?php
$result = ...; // The result of your database query.
$grouped_by_type = Arrays::group($result,"type");
foreach($grouped_by_type as $type => $group)
{
echo "<h2>".ucwords($type)."</h2>";
echo "<ul>";
foreach($group as $animal)
{
echo "<li>".$animal['first_name']."</li>"; // Assumes the query brought back first_name...
}
echo "</ul>";
}
?>
Your first query would be:
SELECT DISTINCT type FROM table ORDER BY type ASC
With your PHP, I am sure you can get a result from this query and loop through it. Next one is to put another query inside the loop that gives you the list of animals that belong to current type:
SELECT name FROM table WHERE type='$type' ORDER BY name ASC
$type is just variable holding current type. I assumed column and table names so please change it to suit your code.
I have following tables:
+---------+-----------+ +----------+---------+-------------+
| item_id | item_name | | image_id | item_id | image_url |
+---------+-----------+ +----------+---------+-------------+
| 1 | phone | | 1 | 1 | http://url1 |
| 2 | computer | | 2 | 1 | http://url2 |
| 3 | keyboard | | 3 | 2 | http://url2 |
+---------+-----------+ +----------+---------+-------------+
What should I do in php using PDO and json encode to have the following ouput:
[
{
"item_id":"1",
"name":"phone",
"images": [
{
"image_id":"1",
"url":"http://url1"
},
{
"image_id":"2",
"url":"http://url2"
}
]
},
{
"item_id":"2",
"name":"computer",
"images": [
{
"image_id":"3",
"url":"http://url3"
}
]
},
{
"item_id":"3",
"name":"keyboard",
"images": []
}
]
Do I HAVE to use foreach inside foreach loop to get this result?
No, use a single loop, and a single SQL query.
Firstly the SQL query is
SELECT im.image_id, im.image_url, im.item_id
it.item_name
FROM images im
LEFT JOIN items it ON it.item_id=im.item_id
ORDER BY im.item_id, im.image_id
And your PHP (paraphrased) is:
$lastItemId = -1;
$aJson = array();
while ($row = GetNextRow()) { // Depends on your class
$itemID = $row['item_id'];
if (!isset($aJson[$itemID])) {
$aJson[$itemID] = array('item_id'=$itemID, 'item_name'=$row['item_name'], images=>array());
}
$aJson[$itemID]['images'] = array('image_id'=>$row['image_id'], 'image_url'=>$row['image_url']);
}
echo json_encode($aJson);
You should be able to tweak from there?