Show only 100 samples in Graph - javascript

I have an array that is printed in a javascript graph. Now is taken the first measurement until the last measurement. I wan't to display the first 100 measurements and if the measurements get over 100 (lets say 130) to display measurement 30 until 130, etc. How can I fix this in my code?
<?php
for ($x = 0; $x < (count($data) - 1); $x++) {
if($x % 5 == 0){
//echo $x; //5th element
$row = $data[$x];
$time = intval($row->{'time'});
echo $x;
} else{
echo ' '; //prints empty for other than 5th element
}
echo ', '; //prints ',' for every element
}
?>

change the start of your code to:
$total=count($data);
if($total>100){
$start= $total-100;
}else{
$start=0;
}
for ($x = $start; $x <= $total; $x++) {

Related

ChartJS sees date in string as numbers

I am trying to get some strings from a csv file, to put under a graph from chartjs.
I wrote this php function:
function getTitle($id) {
$stations = fopen("stations.csv", 'r');
//$csv_data = fopen(date("G") . '.csv', 'r');
$csv_data = fopen('16.csv', 'r'); //fopen("/mnt/weather-data/" .
date("Y/m/d/G") . ".csv", 'r');
$data = array();
$DAY_LENGTH = 60 * 60;
$currentDate = date('U');
while (($line = fgetcsv($csv_data)) !== FALSE) {
$date = date('U', strtotime($line[1]));
if ($date >= ($currentDate-$DAY_LENGTH) && $line[2] == $id) {
array_push($data, $line[0]);
}
}
for ($i = 0; $i < count($data); $i++) {
echo $data[$i] . ",";
}
fclose($stations);
fclose($csv_data);
}
which is working fine, everywhere I call this function, it prints the necesarry strings, which is a date, so 2018-02-08 for example.
When I try to call this function in the label part where i need them in ChartJS. it returns this date as a sum, so like 2018-02-08 will return as 2008.
labels: [<?php getTitle(($_GET['id']))?>],
What is it what I'm doing wrong?
Another problem when I try to change $line[0] to $line[1] which is a time with the following format: 20:17:36, my whole application stops working..
Hope anyone can help me!!
The Problem with your code is that the echo will just output the content of the variable but as you are parsing it's value into a javascript array you need to surround your value with quotation marks. So if you change
for ($i = 0; $i < count($data); $i++) {
echo $data[$i] . ",";
}
into
for ($i = 0; $i < count($data); $i++) {
echo "'" . $data[$i] . "',";
}
it should work. This will also solve your other problem. So with this change you should also be able to use the time.

how to export html table to excel, with pagination

I have a form which displays mysql data in the form of an HTML table, using pagination of 5 records per page. I'm trying to export my table to excel, but it's only exporting data of the rows that are currently show, meaning if there are 20 rows, it only shows the first 5 as those get displayed. How can I make it export the one's that are not being displayed but still part of the search? I saw a few other people with a similar question but none have answers( How to export all HTML table rows to Excel file from a paginated table?). Hopefully I can get one!
<?php
if(isset($_GET['submit']) || isset($_GET['page'])){
// Setting up the Pagination below
echo '<center>';
$page_query = "SELECT * FROM tbl_call_log_detail ";
$page_query .= "WHERE
(dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
OR ( Time <= '$endDate' AND Time >= '$startDate'
AND (dealer_id = '$call_id' OR'$call_id'='' ))
OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01' AND '$call_id'='') ";
$page_result = mysqli_query($conn, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records/$record_per_page);
$start_loop = $page;
$difference = $total_pages - $page;
if($difference <= $total_pages){
$start_loop = $total_pages - $difference;
}
$end_loop = $start_loop + 2;
if($end_loop > $total_pages){
$end_loop = $total_pages;
}
if($difference > $total_pages){
$end_loop = $total_pages;
}
echo '<div class = "center">';
echo '<div class = "pagination">';
if($page > 1){
echo "<a href= 'dealer_call_log.php?page=1".$urlparameter."'>First</a>";
echo "<a href= 'dealer_call_log.php?page=".($page - 1).$urlparameter."'> << </a>";
}
for ($i = $start_loop; $i <= $end_loop; $i++){
echo "<a href= 'dealer_call_log.php?page=".$i.$urlparameter."'>".$i."</a>";
}
if($page < $end_loop){
echo "<a href= 'dealer_call_log.php?page=".($page + 1).$urlparameter."'> >> </a>";
echo "<a href= 'dealer_call_log.php?page=".$total_pages.$urlparameter."'>Last</a>";
}
if($page < 1){
$page = 1;
}
echo '</div>';
echo '</div>';
echo '<br>';
$sql = "SELECT Name, SFID, Comment, Time FROM tbl_call_log_detail
WHERE
(dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
OR ( Time <= '$endDate' AND Time >= '$startDate'
AND (dealer_id = '$call_id' OR'$call_id'='' ))
OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01' AND '$call_id'='')
ORDER BY Time DESC LIMIT $start_from, $record_per_page ";
$result = mysqli_query($conn, $sql);
$row = mysqli_num_rows($result);
$all_property = array();
echo "<table class = 'data-table' border = '1' cellpadding = '9' bgcolor = '#CCCCCC' id = 'data-table'>
<tr class = 'data-heading'>";
while($property = mysqli_fetch_field($result)){
echo '<td><b> '. $property ->name. ' </b></td>';
array_push($all_property, $property ->name);
}
echo '</tr>';
while ($row = mysqli_fetch_array($result)){
echo '<tr>';
foreach($all_property as $item){
echo '<td> '. $row[$item] . ' </td>';
}
echo '</tr>';
echo '</center>';
}
echo '</table>';
echo '<br>';
?>
// This is what is getting the current rows, but not all
<input type = "submit" onclick = "window.open('data:application/vnd.ms-excel, '+encodeURIComponent(document.getElementById('data-table').outerHTML));" value = "Export into excel" />
<?php
}
?>
UPDATE: Found the answer I was looking for I simply ran a new sql query without the LIMIT clause and stored it in a hidden table. I then use the hidden table to export data
// SQL and hidden table for exporting to excel
$page_query2 = "SELECT * FROM tbl_call_log_detail ";
$page_query2 .= "WHERE
(dealer_id = '$call_id' AND '$endDate'='1970-01-01' AND '$startDate' ='1970-01-01')
OR ( Time <= '$endDate' AND Time >= '$startDate'
AND (dealer_id = '$call_id' OR'$call_id'='' ))
OR ('$endDate'='1970-01-01' AND '$startDate' ='1970-01-01' AND '$call_id'='') ORDER BY TIME DESC ";
$page_result2 = mysqli_query($conn, $page_query2);
$row2 = mysqli_num_rows($page_result2);
$all_property2 = array();
echo "<table class = 'data-table2' border = '1' cellpadding = '9' bgcolor = '#CCCCCC' id = 'data-table2' hidden>
<tr class = 'data-heading2'>";
while($property = mysqli_fetch_field($page_result2)){
echo '<td><b> '. $property ->name. ' </b></td>';
array_push($all_property2, $property ->name);
}
echo '</tr>';
while ($row2 = mysqli_fetch_array($page_result2)){
echo '<tr>';
foreach($all_property2 as $item){
echo '<td> '. $row2[$item] . ' </td>';
}
echo '</tr>';
echo '</center>';
}
echo '</table>';
?>
<input type = "submit" onclick = "window.open('data:application/vnd.ms-excel, '+encodeURIComponent(document.getElementById('data-table2').outerHTML));" value = "Export into excel" />
<?php
}
?>
You can use JQuery table2excel plugin following is the link
http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html
Try following code pen to only JavaScript
https://codepen.io/kostas-krevatas/pen/mJyBwp

Unix Timestamp Chart.js with PHP echo

I made a line chart with chart.js. For the values is some PHP script added. The axis only displayed the timestamp numbers. When I use a PHP function for timestamp conversion:
<?php echo date('H:i', $time); ?>
It totally crashes.
This is my code for the PHP time echo. How can I display HH:ii on my X axis of the chart?
<?php
$total = (count($data)) - 1;
if ($total > 100) {
$start = $total - 100;
$end = $total;
}
else {
$start = 0;
$end = $total;
}
for ($x = $start; $x < $end; $x++) {
if ($x % 5 == 0) {
//echo $x; //5th element
$row = $data[$x];
$time = intval($row->{'time'});
echo $time;
} else{
echo ' '; //prints empty for other than 5th element
}
echo ', '; //prints ',' for every element
Fixed it with:
echo '\'' . date('H:i', $time) . '\'';

Grab two values in loop, check box and variable. Only grabbing checkbox? php w screenshot

i make a sql query asking for data(its a text question), i output the (question) with a checkbox to the left of it and an input field underneath it to give point worth to it(like a teacher making an exam) . All in a loop w arrays. It outputs the correctly checked questions but only will assign point values to first three questions if there checked. so if i check q1 q2 and q4 it will output q1 q2 q4 and q1 points q2points. Thats my problem, I only want to be able to select three total questions and assign those questions their points.
php in the html
$sql = " SELECT Question FROM examQuestions";
$result = mysqli_query($dbCon, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<input type="checkbox" name="checkboxvar[]" value="'.$row["Question"].'">'.$row["Question"].'<input placeholder="10" class="form-control" type="number" name="points[]">'."<br />";
}
}
im trying to output the data using this:
$checkboxvar = $_POST['checkboxvar'];
$examName = $_POST['examName'];
$questionWorth = $_POST['points'];
$i=1;
$total = 0;
while ($i < 4) {
$x = $i - 1;
echo $checkboxvar[$x];
echo $questionWorth[$x]."<br />";
$total = $total + $questionWorth[$x];
$i = $i +1;
}
echo $total;
As I told you in the comments try modifying your code like this:
$sql = " SELECT Question FROM examQuestions";
$result = mysqli_query($dbCon, $sql);
$i = 0;
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<input type="checkbox" name="checkboxvar[]" value="'.$i++.'-'.$row["Question"].'">'.$row["Question"].'<input placeholder="10" class="form-control" type="number" name="points[]">'."<br />";
}
}
And then, to have the right points for the correspondig question something like this:
$checkboxvar = $_POST['checkboxvar'];
$examName = $_POST['examName'];
$questionWorth = $_POST['points'];
$i = 0;
$total = 0;
while ($i < 3) {
$question = explode("-", $checkboxvar[$i]);
echo $question[1];
echo $questionWorth[$question[0]]."<br />";
$total += $questionWorth[$question[0]];
$i++;
}
echo $total;
This should do the work.
If i understand your implied question correctly, you will need some client side (javascript) code that keeps track of the number of checked checkboxes. As soon as one checks three boxes all remaining ones and corresponding text boxes are disabled.
for naive vanilla js solution your php could look like this:
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
echo "<label><input class='questions' type='checkbox' name='question_{$index}' value='{$row["Question"]}' on_change='limit_to_three(this);'>{$row["Question"]}<label><br>";
echo "<input class='scores' name='score_{$index}'><br>";
$index++;
}
For modern browsers you would need the following in your javascript :
var selected_count = 0;
function limit_to_three(selected_checkbox) {
if (selected_checkbox.checked) {
selected_count++;
} else {
selected_count--;
}
var limit_reached = (selected_count == 3);
var checkboxes = document.getElementsByClassName('questions');
var scores = document.getElementsByClassName('scores');
for (var i=0; i<checkboxes.length; i++) {
if (!checkboxes[i].checked) {
checkboxes[i].disabled = scores[i].disabled = limit_reached;
}
}
}
Now, upon submit, you can assume that only checked question checkboxes will be submitted, so your php code could be like this:
$total = 0;
$length = strlen('questions_');
foreach ($_POST as $name => $value) {
if (substr($name, 0, $length) == 'questions_') {
$index = substr($name, $length - strlen($name));
echo $_POST[$name];
echo "<br>";
echo $_POST["scores_{$index}"];
$total += $_POST["scores_{$index}"];
}
}
As i said, this is a naive implementation, that sends question texts back and forth. If i were you i would add ID column to your questions table and use it instead of dynamically generated indexes
Then your html could be generated like this:
while($row = mysqli_fetch_assoc($result)) {
echo "<label><input class='questions' type='checkbox' name='question_{$index}' value='{$row["ID"]}' on_change='limit_to_three(this);'>{$row["Question"]}<label><br>";
echo "<input class='scores' name='score_{$row["ID"]}'><br>";
}
and your receiving php could be like this:
$checked = explode(',', $_POST['questions']);
for ($checked as $id) {
$total += $_POST["scores_{$id}"];
}
Also you could retrieve the checked questions by
$sql = "SELECT * FROM Questions WHERE ID IN ({$_POST['questions']})";

count lines in array and split into 2 divs php

I have a table in my website with 4 columns. I'm using ACF custom field to enter my content.
$string = get_field('projets');
my strings are split into two divs, date_index & texte_index... date_index display the date, and text_index displays the text, so each strings is split when finding a blank space.
like this :
here is the content I enter = 2013-07 PARIS COLLÈGE DE FRANCE
and here is how it is displayed :
<div class="date_index">2013-07</div><div class="texte_index">PARIS COLLÈGE DE FRANCE</div>
it works fine.
then my frist array is split into two column, when first column have 130 lines, then the next lines are displayed in another column.
here is my code :
<div class="columns_projets_1">
<p><?php
$string = get_field('projets');
$array = explode("\n", $string);
for($i = 0; $i <130; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>
<div class="columns_projets_2">
<p><?php
$string = get_field('projets');
$array = explode("\n", $string);
for($i = 130; $i <count($array)-1; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>
then I have a 3rd column, here is the code :
<div class="columns_conferences">
<p><?php
$string = get_field('conferences');
$array = explode("\n", $string);
for($i = 0; $i <300; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>
and my 4th column :
<div class="columns_monographies">
<p><?php
$string = get_field('monographies');
$array = explode("\n", $string);
for($i = 0; $i <300; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>
first of all I would like to delete the condition ( $i <300 )on the 3rd and 4th column, I don't want to have limits in this two columns, but when I try to delete the condition nothing is displayed...
secondly I would like to count the lines in the 3rd column, and this number will be the limit of my first column. so my 1st and 3rd column will have the same lines number.
do you understand what I mean ?
it should be something like that but I can't manage to make it work :
in my 1st column :
for($i = 0; $i <130; $i++){
instead of 130, the number of lines of my 3rd column array.
and for 2nd column ;
for($i = 130; $i
instead of 130, the number of lines of my 3rd column array.
I really hope you can help me with this,
here is a github link :
https://gist.github.com/mattieumoreau/7431037
thanks a lot for your help,
To fix your first issue, use sizeof(array) rather than a specific number, e.g.:
for($i = 0; $i < sizeof($array); $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
Edit:
Your third column would look a bit like this:
<div class="columns_conferences">
<p><?php
$string = get_field('conferences');
$array = explode("\n", $string);
for($i = 0; $i < sizeof($array); $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>
There are a few issues going on here.
If you delete the condition, either your for loop is erroring out or it's going on forever (depends on how you're trying to delete it). To get all the records, you want for ($i = 0; $i < count($array); $i += 1) { ...
To limit the length of the first column of 'projets' to the length of 'conferences', you'll need to pick up the data ahead of time, then display it. Here's what I mean:
$projets = explode("\n", get_field('projets'));
$conferences = explode("\n", get_field('conferences'));
$monographies = explode("\n", get_field('monographies'));
// Now we want to limit the length of the first column of projets (col 1) to the
// length of conferences (col 3).
$projets = array_chunk($projets, count($conferences)); // Split the projets into a set of arrays of the right length.
$projets1 = array_shift($projets); // Grab the first one... that's the data for the first column.
$projets2 = array_reduce($projets, 'array_merge', array()); // Merge the remaining arrays into a single array for display.
and then :
function displayDateAndText ($obj)
{
$dateAndText = explode(' ', $obj, 2);
echo "<div class=\"date_index\">{$dateAndText[0]}</div>";
echo "<div class=\"texte_index\">{$dateAndText[1]}</div>";
}
function displayRecords ($records)
{
echo '<p>';
$limit = count($records);
for ($i = 0; $i < $limit; $i += 1)
{
displayDateAndText($records[$i]);
}
echo '</p>';
}
?>
<div class="columns_projets_1">
<?php
displayRecords($projets1);
?>
</div>
<div class="columns_projets_2">
<?php
displayRecords($projets2);
?>
</div>
<div class="conferences">
<?php
displayRecords($conferences);
?>
</div>
<div class="monographies">
<?php
displayRecords($monographies);
?>
</div>

Categories

Resources