Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there I am a beginner of PHP, I am attempting to design a website using PHP and PHPMyAdmin. This dashboard is supposed to show sales of graphs.

Error Example:Notice: Undefined variable: jan in C:\xampp\htdocs\FYP2\admin\dashboard.php on line 459 the same applies to as mentioned previously, lines 460, 461, 462... 470



I have included the line numbers next to the lines of code with the errors (e.g.Line 460)

What is causing these problems and what do I need to do to resolve them ???

What I have tried:

PHP
  1  <?php
  2  
  3              include("includes/db.php");
  4  
  5              $query = "select * from sales_chart";
  6              $result = mysqli_query($con,$query);
  7  
  8              if(mysqli_num_rows($result) >1){
  9                  while($row = mysqli_fetch_assoc($result)){
 10  
 11                      $jan = $_row['jan'];
 12                      $feb = $_row['feb'];
 13                      $march = $_row['march'];
 14                      $april = $_row['april'];
 15                      $may = $_row['may'];
 16                      $june = $_row['june'];
 17                      $july = $_row['july'];
 18                      $august = $_row['august'];
 19                      $sept = $_row['sept'];
 20                      $oct = $_row['oct'];
 21                      $nov = $_row['nov'];
 22                      $dec = $_row['dec'];
 23  
 24                  }
 25              }
 26                  else
 27                  {
 28                      echo "something went wrong";
 29                  }
 30                  
 31                 
 32  
 33          ?>
 34              <html>
 35              <head>
 36                 
 37                  <link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.css" rel= "stylesheet">
 38                  <link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" rel= "stylesheet">
 39  
 40  
 41              </head>
 42              <body>
 43              
 44              <canvas id="myChart" style="height:auto; width: 500px;"></canvas>
 45  
 46              
 47              <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js"></script>
 48              <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
 49              <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"></script>
 50              <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
 51  
 52              <?php
 53  
 54              echo "<input type='hidden' name='jan' id='jan' value = '$jan'>";
 55              echo "";
 56              echo "";
 57              echo "";
 58              echo "";
 59              echo "";
 60              echo "";
 61              echo "";
 62              echo "";
 63              echo "";
 64              echo "";
 65              echo "";
 66              ?>
 67  
 68  
 69              
 70                  
 71  
 72                  var jan = document.getElementById("jan").value;
 73                  var feb = document.getElementById("feb").value;
 74                  var march = document.getElementById("march").value;
 75                  var april = document.getElementById("april").value;
 76                  var may = document.getElementById("may").value;
 77                  var june = document.getElementById("june").value;
 78                  var july = document.getElementById("july").value;
 79                  var august = document.getElementById("august").value;
 80                  var sept = document.getElementById("sept").value;
 81                  var oct = document.getElementById("oct").value;
 82                  var nov = document.getElementById("nov").value;
 83                  var dec = document.getElementById("dec").value;
 84  
 85                  window.onload = function()
 86                  {
 87                      var randomScalingFactor = function(){
 88                          return Math.round(Math.random() *100);
 89                      };
 90  
 91                      var config = {
 92                          type: 'bar',
 93                          datasets: [{
 94                              data: [
 95                                   jan,
 96                                   feb,
 97                                   march,
 98                                   april,
 99                                   may,
100                                   june,
101                                   july,
102                                   august,
103                                   sept,
104                                   oct,
105                                   nov,
106                                   dec
107                               ],
108                               borderColor: "#fff",
109                               borderWidth: "3",
110                               hoverBorderColor: "#000"
111  
112                               label: 'Monthly Sales Report',
113  
114                               backgroundColor:[
115  
116                                  pink,
117                                  blue,
118                                  navyblue,
119                                  cyan,
120                                  red,
121                                  green,
122                                  dark green,
123                                  yellow,
124                                  purple,
125                                  lime,
126                                  magenta,
127                                  skyblue
128  
129                               ],
130  
131                               hoverBackgroundColor: [
132  
133                                  black,
134                                  black,
135                                  black,
136                                  black,
137                                  black,
138                                  black,
139                                  black,
140                                  black,
141                                  black,
142                                  black,
143                                  black,
144                                  black
145  
146                               ],
147  
148                          }],
149  
150                          Labels: [
151  
152                              'jan',
153                              'feb',
154                              'march',
155                              'april',
156                              'may',
157                              'june',
158                              'july',
159                              'august',
160                              'sept',
161                              'oct',
162                              'nov',
163                              'dec'
164                          ]
165                      },
166  
167                      options: {
168                          responsive: true;
169  
170                      }
171                  };
172  
173                  var ctx = document.getElementById('myChart').getContext('2d');
174                  window.myPie = new Chart(ctx, config);
175  
176              };
Posted
Updated 15-Jul-20 3:09am
v2
Comments
Richard Deeming 15-Jul-20 9:10am    
You haven't posted line 459, or any other line which you say has an error.

Just dumping your code on us and saying "it's on line 459" isn't just rude; when there aren't 459 lines in the code dump it isn't helpful either: we have no idea what line you mean so we can't help you.

That doesn't mean "dump more code, with line numbers maybe" - that would be even ruder.
Instead, look at your source file, find line 459 (most editors support CTRL+g as "go to line number") and look at it. Look at the lines above it, and at the error message. Check where "jan" is defined, and what it's scope is likely to be - variables in a function for example can't normally be accessed outside that function. Problems like this are normally pretty obvious if you check the relevant scopes carefully.

If you still can't see the problem, extract the faulty line and the function it's in (or a subset of the function if it's large) and a few lines round the declaration (maybe 20 lines total) and post that telling us exactly which line shows the problem.

But dumping a load of irrelevant code on us and saying "fix that" doesn't make you any friends.
 
Share this answer
 
Apart from finding the needle in this haystack (which I did not), there are a few problems with your code...

Firstly, at the top use some error reporting to see why you receive errors -

PHP
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>


The error returned means that 'jan' does not exist, does your table sales_chart contain the fields jan, feb etc, are thye named correctly, spelled correctly etc.

You are trying to return rows but counter starts from row 2, you need to say
PHP
if(mysqli_num_rows($result) >0){
to include row 1 as well.

Try not to include html in php code, rather escape, do html and then include returned php values -
HTML
<input type='hidden' name='jan' id='jan' value = <?PHP echo $jan; ?>>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900