In my controller I create the array to be charted
function chart() {
$results = $this->Visit->query(
"SELECT date(visits.created) as visit_date,
Count(visits.id) AS count_visits
FROM visits
GROUP BY date(visits.created)"
);
foreach($results AS $result) {
$row = array(
$result[0]['visit_date'],
$result[0]['count_visits']
);
$chartData[] = json_encode($row);
}
pr($chartData);
}
pr($chartData) gives following array
Array
(
[0] => ["2012-07-11","5"]
[1] => ["2012-07-13","1"]
[2] => ["2012-07-14","1"]
)
in chart view i have
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create the data table.
var data = google.visualization.arrayToDataTable($chartData);
var options = {
title: 'Visits by Date',
hAxis: {title: 'Date', titleTextStyle: {color: 'black'}}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
but this doesn't get me a chart .. no errors ... just no chart ..
if I manually type in an array to test my view, it works fine ... eg if i replace ..
var data = google.visualization.arrayToDataTable($chartData);
with the following format from Google Chart example it generates a nice column chart
var data = google.visualization.arrayToDataTable([
['Date', 'Count'],
['2012-07-11', 5],
['2012-07-13', 1],
['2012-07-14', 1]
]);
couple of questions:
- how do i get the header titles in the var data array?
- do double quotes or single quotes in array make a difference?
- am i passing the array $chartData from php to Google chart javascript correctly?
- the Google example has '[]' around the array, how do i get those around my array too?
(bwt, i know all about cakephp's Find(all) but I just got lost in documentation about how to get what i wanted and sql was much easier )
you are using json_encode in your controller, but in your view you are using -
google.visualization.arrayToDataTable($chartData);
json_encode creates json object, not and javascript array.
using json_encode in your controller function chart() try:
$chartData['cols'] = array(
array('id' => 'visit_date', 'label' => 'Visit date', 'type' => 'date'),
array('id' => 'count_visits', 'label' => 'Count', 'type' => 'number')
);
foreach($results AS $result) {
$time = strtotime($result[0]['visit_date']);
$dateJs = 'Date('.date("Y", $time).', '.(date('n', $time) - 1).', '.date('j', $time).')';
$row = array(
'c' => array(
array('v' => $dateJs),
array('v' => $result[0]['count_visits']),
)
);
$chartData['rows'][] = $row;
}
//make the data available for view
$this->set('chartData', json_encode($chartData);
in your view use :
var data = new google.visualization.DataTable($chartData);
Related
I am trying to return values from a query that is only returning null values or is giving me errors based upon everything that I have tried. I have created a WP Plugin to put this code. I have pasted my code below
I have edited this code to what is currently working, but it is only giving me that last entry in the DB table. How would I get them all to display
function register_contact_form_fields() {
register_graphql_field( 'RootQuery', 'contactForm', [
'description' => __( 'Get a user submission', 'codmoncai-contact' ),
'type' => 'ContactForm',
'resolve' => function( $root, $args, $context, $info ) {
global $wpdb;
$combined_data = [];
$results = $wpdb->get_results("SELECT * FROM wpxd_contact_us");
}
return $data;
] );
}
By changing the 'type' in the register_graphql_field function to
'type' => [ 'list_of' => 'ContactForm' ],
Fixed my issue and allowed me to get all rows in the query
I am filling a json array with data from an sql query, but when i try to use on of the elements it comes out undefined.
$select = ['id', 'first_name', 'last_name', 'grade', 'site_id', 'role_id', 'school_id', 'email', 'sites.alias'];
$usersTable = TableRegistry::get('Users');
$sitesTable = TableRegistry::get('Sites');
$users = $usersTable->find('all')
->select($select)
->innerjoin('sites', 'Users.site_id = sites.id')
->where(['first_name LIKE' => '%'.$filter.'%'])
->orWhere(['last_name LIKE' => '%'.$filter.'%'])
->andWhere($where);
$total = $users->count();
if(isset($users) && !empty($users)) {
echo json_encode($users);
} else {
$errors = ['No users found'];
echo json_encode($errors);
}
When I try to access the sites.alias in my html code it come out undefined.
Is this Laravel? If yes, try using ->join('sites', 'users.site_id', '=', 'sites.id') instead of that innerjoin.
I have a result json file namely result.json and I would to insert another sub array in total and rows.
This is my JSON File
{"total":"9","rows":[
{"id":"16","firstname":"Melanie","lastname":"Toledo","phone":"091919191","email":"test#gmail.com"}, {"id":"29","firstname":"x","lastname":"x","phone":"1","email":"test#gmail.com"}, {"id":"30","firstname":"y","lastname":"y","phone":"2","email":"test#gmail.vp"}, {"id":"31","firstname":"xxx","lastname":"xxxx","phone":"12345","email":"test#gmail.com"}, {"id":"33","firstname":"xy","lastname":"xy","phone":"1","email":"test#gmail.com"}, {"id":"34","firstname":"yyy","lastname":"yyy","phone":"2","email":"test#gmail.com"}, {"id":"35","firstname":"n","lastname":"n","phone":"1","email":"test#gmail.com"}, {"id":"36","firstname":"q","lastname":"q","phone":"1","email":"x#g.com"}, {"id":"37","firstname":"","lastname":"","phone":"","email":""}
]
}
Here is my code
<?php
$current_data = file_get_contents('result.json');
$array_data = json_decode($current_data, true);
$extra['rows'] = array (
'id' => '101',
'firstname' => 'marlon',
'lastname' => 'berces',
'phone' => '12',
'email' => 'test#gmail.com'
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
file_put_contents('result.json',$final_data);
?>
If I understood your question correctly, you will need to add the new row in the current list of rows, and after that, you have to update the sum, as this is all static data that came from the file.
<?php
// Read json
$current_data = file_get_contents('result.json');
$array_data = json_decode($current_data, true);
// New row
$extra = array(
'id' => '101',
'firstname' => 'marlon',
'lastname' => 'berces',
'phone' => '12',
'email' => 'test#gmail.com'
);
// Add the new row
$array_data['rows'][] = $extra;
// Update the sum
$array_data['total'] = count($array_data['rows']);
// Write json
$final_data = json_encode($array_data);
file_put_contents('result.json',$final_data);
?>
you can do it by convert it an array then use array_push for add new value agani convert to json...
I think what you are looking for is the Array.prototype.push() method(In JavaScript). Or if you are using PHP, then array_push().
The push()/array_push() method adds one or more elements to the end of an array and returns the new length of the array.
I have array in controller file:
$total_data = array();
$totals = $this->model_sale_order->getOrderTotals($order_id);
foreach ($totals as $total) {
$total_data[] = array(
'title' => $total['title'],
'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
);
}
How pass 'text' in this same controller file?
for example:$text = $total['text'];I getting an error undefined index: text in....
Where is a problem?
I don't know what you want to do, but you can send a variable from controller to view by $data in Opencart 2.x
You can send $total_data to tpl file this way:
$data['total_data'] = $total_data;
so your code must be:
$total_data = array();
$totals = $this->model_sale_order->getOrderTotals($order_id);
foreach ($totals as $total) {
$total_data[] = array(
'title' => $total['title'],
'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value'])
);
}
$data['total_data'] = $total_data;
and use it in view (tpl file):
<?php
foreach($total_data as $total){
echo $total['text'];
}
?>
What I am trying to do is display a table with checkboxes on the press of a button by ajax. The table should be initially hidden and get populated on the fly by a function call.
If initially I load $options1 with some dummy values , then after ajax call it throws in an error saying-
Notice: Undefined index: red in theme_tableselect() (line 3285 of
D:\wamp\www\drupal7\includes\form.inc).
where 'red' is the index of a dummy row value and #options don't get populated with the new values. What is the way to get this working ?
Here is the code for the form-
$form['mltag_new']['tag'] = array(
'#type' => 'button',
'#value' => t("Suggest Tags"),
'#ajax' => array(
'callback' => 'mltag_suggest_tags_ajax',
'wrapper' => 'mltag_suggest_tags_table_div',
'effect' => 'slide',
),
);
$options1 = array(); //initial dummy values
$options1['red']['tag'] = "A red row";
$options1['red']['chi'] = "A red row";
$form['mltag_new']['myselector'] = array (
'#type' => 'tableselect',
'#title' => 'My Selector',
'#header' => $header,
'#options' => $options1,
'#prefix' => '<div id="mltag_suggest_tags_table_div">',
'#suffix' => '</div>',
);
return $form;
and the Ajax callback looks something like this-
function mltag_suggest_tags_ajax($form, $form_state) {
//$content has some content
//pass the content to a function
include_once 'includes/content_tag.inc';
$tags = mltag_content_tag($content, variable_get('algo_type'), 20);
if (empty($tags)) {
$output .= t('Content is insufficient to generate Tags using this algorithm. <br>Please choose other algorithm from Settings Page.');
$form['mltag_new']['sample_text']['#markup'] = $output;
return $form['mltag_new']['sample_text'];
}
else {
$algo = variable_get('algo_type');
if ($algo == 1) {
$header = array(
'tag' => t('Tag'),
'frequency' => t('Frequency'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'frequency' => $value,
);
}
}
elseif ($algo == 2) {
$header = array(
'tag' => t('Tag'),
'chi' => t('Chi Square Value'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'chi' => $value,
);
}
}
$form['mltag_new']['myselector']['#header'] = $header;
$form['mltag_new']['myselector']['#options'] = $options;
return $form['mltag_new']['myselector'];
}
}
I replied to your post on Drupal.org about how I'm working on a somewhat similar problem. Try adding
$form['mltag_new']['myselector'] =
form_process_tableselect($form['mltag_new']['myselector']);
just before your return. Hopefully that helps you more than it did me. Beware that the #options just get rendered when the block reloads from the ajax, but the original $form object doesn't seem to be aware.
I know that this is a few years later, but I found this while searching for my own solution:
The tableselect module creates checkboxes in the $ form that have to be removed. in the example above, they would be in $form['mltag_new']['myselector'] with keys equal to the original $option1 in your original code. If you unset those, then call
$form['mltag_new']['myselector'] = form_process_tableselect($form['mltag_new']['myselector']);
before your return, it will eliminate the dummy rows.