I have the following code that will output a line chart as follows :
<script class="code" type="text/javascript">
$(document).ready(function(){
var line1=[['2008-08-12 ',14], ['2008-09-12 ',6.5], ['2008-10-12 ',5.7], ['2008-11-12 ',9], ['2008-12-12 ',8.2]];
var plot1 = $.jqplot('chart1', [line1], {
title:'Daily Sales',
axes:{
xaxis:{
renderer:$.jqplot.DateAxisRenderer
}
},
series:[{lineWidth:4, markerOptions:{style:'square'}}]
});
});
</script>
The output for the above code is correct, but i want to insert a PHP loop to select data from mysql and place it insde var line1 as an array
So I created a test code as follows :
<script class="code" type="text/javascript">
$(document).ready(function(){
<?php
$date = date('Y-m-d');
for($i=1;$i<6;$i++){
$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
?>
var line1=[['<?php echo $newdate; ?> ',<?php echo $i ?>]];
<?php
$date = $newdate;
}
?>
var plot1 = $.jqplot('chart1', [line1], {
title:'Daily Sales',
axes:{
xaxis:{
renderer:$.jqplot.DateAxisRenderer
}
},
series:[{lineWidth:4, markerOptions:{style:'square'}}]
});
});
</script>
This outputs the last value which is 2015-5-16 and 5 all i want is to output all the result from 1 to 5 and having dates incremented by each month 2014-12-16 to 2015-5-16.
I hope this makes sense ! Thank You
You're doing this wrong. You don't use PHP to dump text into a Javascript code block directly. That's a sure-fire way of introducing a javascsript syntax error and killing the entire code block.
You build a native PHP structure (array, object, whatever) and then you json_encode() that.
e.g.
<?php
$results = get_data_from_db();
$data = array();
while($row = fetch_row_from_result($results)) {
$data[] = array($row['foo'], $row['bar']);
}
?>
<script type="text/javascript">
var data_from_db = <?php echo json_encode($data); ?>;
</script>
Replace your PHP-Block for "var line1=[['..." with this:
<?php
$date = date('Y-m-d');
$js = " var line1=[";
for($i=1;$i<6;$i++) {
$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
if($i>1) { $js.= ", "; }
$js.= "['".$newdate." ',".$i."]";
$date = $newdate;
}
$js.= "];";
echo $js;
?>
Related
I need help with FullCalendar and Javascript and PHP.
I need to include this foreach PHP code into this Javascript.
<script type="text/javascript">
$(document).ready(function() {
// page is now ready, initialize the calendar...
$('#calendar').fullCalendar({
events: [
<?php
$sql = "SELECT * FROM eventi";
$eventi = $mysqli->query($sql);
foreach ($users as $row) {
echo '{';
echo 'title: "'.$eventi['nome'] . '"';
echo 'start: "'.$eventi['data'] . '"';
echo '},';
}
?>
]
});
});
</script>
By changing your query so ite returns the data with the desired column names, and then using json_encode to format the results, you can
simplify the code quite a bit.
<script type="text/javascript">
$(document).ready(function() {
// page is now ready, initialize the calendar...
$('#calendar').fullCalendar({
events: [
<?php
$sql = "SELECT `nome` as `title`, `data` as `start` FROM eventi";
$eventi = $mysqli->query($sql);
$allrows = $eventi->fetch_all(MYSQLI_ASSOC);
$rslt = json_encode($allrows);
echo $rslt;
?>
]
});
});
</script>
I am trying to disable dates in a jQuery UI Date Picker, it works when I hard code the dates in to the variable in the JS file as follows:
var bookedDays = ["2015-3-7","2015-3-8","2015-3-15"];
In my PHP file I have:
<?php
$testing = "SELECT fromdate, todate FROM messages WHERE listing_id = '".$_GET['listingid']."'";
$resulttesting = mysql_query($testing) or die(mysql_error() . "<br>" . $testing);
while ($rowtesting = mysql_fetch_assoc($resulttesting))
{
$from = $rowtesting['fromdate'];
$to = $rowtesting['todate'];
}
$start_time = strtotime($from);
$end_time = strtotime($to);
$date_list = array($from);
$current_time = $start_time;
while($current_time < $end_time) {
//Add one day
$current_time += 86400;
$date_list[] = date('Y-m-d',$current_time);
}
//Finally add end date to list, array contains all dates in order
$date_list[] = $to;
$date_list_res = '["' . implode('","', $date_list) . '"]';
print_r ($date_list_res);
?>
<script type="text/javascript">
var bookedDays = <?php echo json_encode($date_list_res); ?>;
</script>
When I run a console.log in the JS file for the variable bookedDays I get ["2015-03-05","2015-03-06","2015-03-07","2015-03-08","2015-03-08"] output which is read from the database which is correct but these dates are not disabling in the date picker. Does anybody know where I'm going wrong?
Instead of
<?php echo json_encode($date_list_res); ?>;
Type just
<?php echo $date_list_res; ?>;
Everything should be dandy.
This is the weirdest thing i've seen. I have an array that I created with php - then I used JSON_Encode to use it with a FLOT Graph. I echoed out the encoded array once - and it's perfect.
Then randomly, another alert box appears with "null".
Subsequently, the script also has a "null" when i echo it into the javascript.
the var d1 is null when I inspect it...
At first I get an alert box with [[0,50],[1,3],[2,488],[3,25],[4,90],[5,50],[6,90],[7,50],[8,5]] -- then I get a second alert box WHICH I DID NOT CODE with "null".
code:
<?php
$num = 0;
while($row = $sql->fetch(PDO::FETCH_OBJ)){
$line[] = array($num,intval($row->percent));
$num ++;
}
$TEST = json_encode($line);
echo "<script>alert('".$TEST."');</script>";
?>
<script>
var d1 = <?php echo $TEST;?>;
$(document).ready(function () {
$.plot($("#chart"), [d1]);
});
</script>
Output from Inspector:
var d1 = []; //Notice the empty array
$(document).ready(function () {
$.plot($("#chart"), [d1]);
});
Use $line = array(); for empty data from while loop
$line = array();
while(....
You can check empty
if(count($line)!=0) {
$TEST = json_encode($line);
echo "<script>alert('".$TEST."');</script>";
}
UPDATE:
$TEST = json_encode($line);
echo "<script>$.plot($('#chart'), \"<?php echo $TEST; ?>\");</script>";
$query = "SELECT id FROM server";
$result = mysql_query($query);
while($row =mysql_fetch_array($result))
{
$dServer = $row['id'];
?>
<script>
var dServer = <?php echo $dServer; ?>;
document.write(dServer);
var d=parseInt("dServer") + "<br>";
document.write(d);
</script>
}
here everything is okk,"dServer" prints the "id",but just parseInt() not converting "id" into integer. Could anyone please explain what is the problem.
Dont use as a String :
var d = parseInt(dServer) + "<br>";
You are inputting a string to parseInt(). Instead use as follows.
var d=parseInt(dServer) + "<br>";
Again why you are using javascript to print the same. You could use PHP to do print the value of id to the document.
<?php
$query = "SELECT id FROM server";
$result = mysql_query($query);
while($row =mysql_fetch_array($result))
{
$dServer = $row['id'];
?>
<script>
var dServer = <?php echo $dServer; ?>;
document.write(dServer);
var d=parseInt(dServer);
document.write("<br>"+d);
</script>
<?php
}
?>
I am new to PHP, I am trying to echo a javascript alert box if taskDueDate is Today.
Any suggestions:
$varTaskAlert = mysql_query("SELECT * FROM tasks");
while ( $rows = mysql_fetch_array($varTaskAlert)) {
$varTaskID = $rows['taskid'];
$varTaskTitle = $rows['tasktitle'];
$varTaskDetail = $rows['taskdetail'];
$varTaskResource = $rows['taskresource'];
$varTaskDue = $rows['taskduedate'];
$varTaskStatus = $rows['taskstatus'];
$varTaskType = $rows['tasktype'];
$active = Active;
$curdate = date('y/m/d');
if ($varTaskStatus == $active && $varTaskDue == $curdate) {
echo '<script type="text/javascript">alert("The Task <?php echo $varTaskTitle ; ?> is Today.")</script>';
}
}
Please check date format of
$varTaskDue = $rows['taskduedate'];
Is $varTaskDue formate similar to 'y/m/d' format?
Hope this will help.
On first look you should change
echo '<script type="text/javascript">alert("The Task <?php echo $varTaskTitle ; ?> is Today.")</script>';
to
echo "<script type='text/javascript'>alert('The Task {$varTaskTitle} is Today.')</script>";
Your Echo is wrong. You are already in php you just include js alert so you can directly use that variable. Try this
echo "<script type='text/javascript'>alert('The Task {$curdate} is Today.')</script>";