So I currently have graphs with have static data from JQ, I'm attempting to get the data from a DB using PHP. I've began the logic but struggling to move forward, Any ideas would be appreciated.. So far the graph outputs blank, only works when I use static data.
Used 0 in the output array as a test
PHP<?php
$userID = $_SESSION["userid"];
$days = array();
$query = "SELECT timeSpent
FROM gymTime
WHERE userid = '$userID'";
$result = mysqli_query($conn, $query);
$i=0;
while($row = $result->fetch_assoc()) {
$days[$i] = $row["timeSpent"];
$i++;
}
?
JQ <script>
// in php you simply need to create the two arrays and teh functionality will work
// monthly set to minutes
var myTimeMon = ;
var myMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
// weekly data in mins
var myTimeWeek = [<?php echo $days[0]; ?>];
var myDays= ["Mon","Tue","Wed","Thur","Fri","Sat","Sun"];
// default values to be displayed when the page loads
var myLabels = myDays;
var mydata = myTimeWeek;
// store value of radiobutton
var currentValue = "m"
var displayData=[];
function contoMins(){
// change the values of the array
for(i=0; i<mydata.length; i++){
mydata[i]=mydata[i]*60;
}
destroyChart();
}
// destroy the chart so that it does not load on top of itself
function destroyChart(){
window.myBar.destroy();
buildChart();
}
function buildChart(){
displayData = mydata;
var barChartData = {
labels : myLabels,
//barValueSpacing : 25,
//barStrokeWidth : 40,
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: displayData
}
]
}
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
barValueSpacing : 10,
});
}
buildChart();
//sendData();
</script>
DB Structure
http://prntscr.com/avdg9r
Webpage
http://prntscr.com/avdgeq
This always sets $i equal to 0.
while($row = $result->fetch_assoc()) {
$i=0;
$days[$i] = $row["timeSpent"];
$i++;
}
Move $i = 0 outside the while loop.
$i=0;
while($row = $result->fetch_assoc()) {
$days[$i] = $row["timeSpent"];
$i++;
}
Also, loop through your $days array.
var myTimeWeek = [<?php echo $days[0]; ?>];
will only display the first element in $days. You need to loop through the array.
var myTimeWeek = [
<?php
$i = 0;
$total = 0;
foreach ($days as $day)
{
if ($i > 0) echo ', ';
echo '"' . $day . '"';
$total = $total + $day;
$i++;
}
?>
]
Move your $i outside the loop-block, otherwise the $i will be 0 for each iteration
$i=0;
while($row = $result->fetch_assoc()) {
$days[$i] = $row["timeSpent"];
$i++;
}
Related
I don't know what is wrong. This query is returning in PHPMyAdmin normaly.
Can someone help me please?
$result = mysql_query("SELECT depoimento, nome from depoimentos WHERE avaliado = '1'");
<?PHP
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($i < 1){
printf("<div style='display:block' id='id-$i'><p>%s<br><br><i>%s</i></p></div>", $row[0], $row[1]);
}else{
printf("<div style='display:none' id='id-$i'><p>%s<br><br><i>%s</i></p></div>", $row[0], $row[1]);
}
$i= $i+1;
}
?>
<script type="text/javascript">
// store state that we can update
var currentVisible = 0;
// function to update the state and display
function change() {
// hide the old item
document.getElementById('id-'+currentVisible).style.display = 'none';
// update the current index
if (currentVisible < 10){
currentVisible++;
}else{
currentVisible=0;
}
// show the new item
document.getElementById('id-'+currentVisible).style.display = 'block';
setTimeout(change, 8000);
}
// queue the first change
setTimeout(change, 8000);
</script>
Hello I have this code and its returnin this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/public_html/pampulhatech.com.br/empresa.php on line 91
your variable $result is out of <?php so that when you use mysql_fetch_array() it can't find the right variable.
try this on instead:
<?PHP
$result = mysql_query("SELECT depoimento, nome from depoimentos WHERE avaliado = '1'");
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($i < 1){
printf("<div style='display:block' id='id-$i'><p>%s<br><br><i>%s</i></p></div>", $row[0], $row[1]);
}else{
printf("<div style='display:none' id='id-$i'><p>%s<br><br><i>%s</i></p></div>", $row[0], $row[1]);
}
$i= $i+1;
}
?>
<script type="text/javascript">
// store state that we can update
var currentVisible = 0;
// function to update the state and display
function change() {
// hide the old item
document.getElementById('id-'+currentVisible).style.display = 'none';
// update the current index
if (currentVisible < 10){
currentVisible++;
}else{
currentVisible=0;
}
// show the new item
document.getElementById('id-'+currentVisible).style.display = 'block';
setTimeout(change, 8000);
}
// queue the first change
setTimeout(change, 8000);
</script>
Right Im using php to generate a json file
<?PHP
header('Content-type: application/json');
include 'includes/db/connect.php';
$category = $_REQUEST['catname'];
$sql = "SELECT `CDID`, `CDTitle`, `CDYear`, `pubID`, `CDPrice` FROM `tiptop_cd` INNER JOIN tiptop_category ON tiptop_cd.catID=tiptop_category.catID WHERE catDesc = '{$category}'";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){
$returned[] = $row;
}
echo json_encode($returned);
?>
and I want my php page to use a select box that gets the categories from the database
<?php
$result = mysqli_query($con,"SELECT * FROM tiptop_category");
// echo "<table>";
echo "<select id=\"catname\">";
while($row = mysqli_fetch_array($result))
{
?>
<option name="<?=$row['catDesc']?>"><?=$row['catDesc']?></option>
<?php
}
echo "</select>";
?>
now im trying to use this ajax to output my data
<script type="text/javascript">
var catname = "Classical";
$.ajax({
type: "GET",
url: "./json.php?catname=" + catname,
accepts: "json",
dataType: "json",
success: function(data, status, jqXHR){
someFunction(data);
},
error: function(jqXHR, status, HTTPerror){
alert(HTTPerror);
}
});
function someFunction(data){
console.log(data);
var list = "<ul>";
for (var i = 0; i < data.length; i++) {
list += "<li><ul>";
for (var j = 0; j < data[i].length; j++) {
list += "<li>" + data[i][j] + "</li>";
};
list += "</ul></li>";
};
list += "</ul>";
$('#wrapper').append(list);
}
</script>
<div id="wrapper">
</div>
but all i get is an empty bulleted list
From how the db query looks like, you should get a list of associative arrays, which are encoded as objects:
var list = "<ul>";
for (var i = 0; i < data.length; i++) {
list += "<li><ul>";
for(var key in data[i])
if(data[i].hasOwnProperty(key))
list += "<li>" + data[i][key] + "</li>";
list += "</ul></li>";
};
list += "</ul>";
i used php implode method to an array inside javascript.Yesterday it was working fine but now am getting SyntaxError: syntax error <br/> in firebug.
I closed the php tag correctly but not sure why is this coming.
function create() {
var sTop = Math.floor(Math.random() * (windowHeight));
<?php
for ($i = 1; $ i <= 28; $i++) {
if(${'h'.$i} != NULL) {
$sel[] = ${'h'.$i};
}
}
$format= implode('","', $sel); ?>
var selectedImg = new Array("<?php echo $format; ?>");
}
I am getting above the
var selectedImg = new Array("<?php echo $format; ?>");
when I viewed the JavaScript in FireBug.Can anyone help?
A fix ..
function create() {
var sTop = Math.floor(Math.random() * (windowHeight));
<?php
$sel = array();
for ($i = 1; $ i <= 28; $i++) {
if(${'h'.$i} != NULL) {
$sel[] = ${'h'.$i};
}
}
$format = !empty($sel) ? implode('","', $sel) : ""; ?>
var selectedImg = new Array("<?php echo $format; ?>");
}
Other method with json encode
function create() {
var sTop = Math.floor(Math.random() * (windowHeight));
<?php
$sel = array();
for ($i = 1; $ i <= 28; $i++) {
if(${'h'.$i} != NULL) {
$sel[] = ${'h'.$i};
}
}
?>
var selectedImg = <?php echo json_encode($sel); ?>;
}
I'm getting this JSON result from mySQL with the following php query:
/* grab the posts from the db */
$query = "SELECT * FROM ko_timetable";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
JSON Result:
post = {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
}
{
post = {
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
post = {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};
But how can I change this query to get this result group by "DAY_OF_WEEK". See example below, thanks for your help:
{
Sunday = {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
{
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
Monday = {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};
Thanks
You can use DAY_OF_WEEK as array index like this,
while($post = mysql_fetch_assoc($result)) {
$posts[$posts["DAY_OF_WEEK"]] = array('post'=>$post);
}
But remember that in the example you have shown the indexes of array are names of the days then the result of previous Sunday will be replaced with this weeks Sunday and so on so forth.
You could also use a count variable to avoid replacing of keys with duplicate names, like this.
$count = 1;
while($post = mysql_fetch_assoc($result)) {
$posts[$posts["DAY_OF_WEEK"].$count] = array('post'=>$post);
$count++;
}
change this:
while($post = mysql_fetch_assoc($result)) {
$posts[ $post['DAY_OF_WEEK'] ] = $post;
}
i need a help
in php i have a select box like this
<?php
$perpage = 5;
$total = 128;
$num = ceil( $total / $perpage );
$i = 1;
echo "<FORM NAME=\"form1\">";
echo "Select <SELECT NAME=\"select\" onChange=\"goto(this.form)\" SIZE=\"1\" >";
echo "<OPTION VALUE=\"\">----Select Page----";
for($i = 1; $i <= $num; $i++)
{
$sel = $i;
$goto = ($i - 1) * $perpage;
if($goto == 0) { $goto = ''; }
echo "<OPTION VALUE=\"http://localhost/CI_doctrine/blog/filmnews/" . $goto . "\">" . $i . "";
}
echo "</SELECT> Page";
echo "</FORM>";
?>
javascript code is here
function goto(form) {
var index = form.select.selectedIndex;
if (form.select.options[index].value != "0") {
window.location = form.select.options[index].value;
form.select.selected = form.select.options[index].value;
}
}
the code is working fine but i want to change the selected option to set the selected number after the page redirection but here iam getting the "select page" as the selected option
any help appreciated.
thank you from your friend.
Once you redirect, that page is unloaded and a new page loaded (even if it has the same items). When the new page loads, you want to do:
window.onload = function() {
var i, options = form.select.options, curUrl = window.location.href;
for (i = 0; i < options.length; i++) {
if (options[i].value == curUrl) {
form.select.selectedIndex= i;
break;
}
}
}
This will select the current URL. Make sure the URLs in the select options are full URLs (including http://). Also, window.onload is the DOM1 way. You should probably use a real library to deal with this.
Alternatively, you can also select the right input in PHP using the same basic approach.