Error: Using $ this when not in object context

0

I am using PHP, and JavaScript along with the highcharts library to generate a bar graph, but I get the following error:

  

Fatal error: Using $ this when not in object context in
  C: \ xampp \ htdocs \ graphics \ index.php on line 55

What I want is to pass the result of the query to a variable, so that it can then be displayed in a table and in this way generate the graph. I leave my code:

The connection

<?php
 class Conexion
 {
   var $conexion;
   function Conexion()
   {
     $this->conexion=mysqli_connect("localhost","root","","futuro"); 
   }
 }
?>

Index

<?php

    require_once("clases/conexion.php");
        $objeto=new Conexion;
?>

<html>
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Grafico Comparativo de Ventas Anuales por Sucursal</title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <style type="text/css">
 ${demo.css}
    </style>
    <script type="text/javascript">
 $(function () {
 $('#container').highcharts({
    data: {
        table: 'datatable'
    },
    chart: {
        type: 'column'
    },
    title: {
        text: 'Grafico Comparativo de Ventas Anuales por Sucursal'
    },
    yAxis: {
        allowDecimals: false,
        title: {
            text: 'Millones'
        }
    },
    tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' +
                this.point.y + ' ' + this.point.name.toLowerCase();
        }
       }
      });
     });
    </script>
    </head>
    <body>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/data.js"></script>
    <script src="https://code.highcharts.com/modules/exporting.js"></script>

    <?php 

    $sql="SELECT SUM(detalle_boleta.costo) FROM detalle_boleta INNER JOIN      venta_boleta ON detalle_boleta.numero_bol = venta_boleta.numero_bol
    WHERE venta_boleta.fecha_bol BETWEEN '2015-01-01' AND '2015-01-31'";
    $enero1=mysqli_query($this->conexion,$sql);


                    ?>
  <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

  <table id="datatable">
  <thead>
    <tr>
        <th></th>
        <th>2015</th>
        <th>2016</th>
    </tr>
</thead>
<tbody>
    <tr>
        <th>Enero</th>
        <td><?php echo '$enero1' ; ?></td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Febrero</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Marzo</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Abril</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Mayo</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Junio</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Julio</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Agosto</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Septiembre</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Octubre</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Noviembre</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
    <tr>
        <th>Diciembre</th>
        <td>40000000</td>
        <td>42500000</td>
    </tr>
</tbody>
</table>
</body>
</html>

Line 55

The line that has the problem is after the query, which would be:

$enero1=mysqli_query($this->conexion,$sql);

I'm not sure if the problem is how I'm making the connection, and just today I started using highchart.

    
asked by CristianOx21 11.11.2016 в 05:21
source

1 answer

1

The problem is because $this is already in another file and outside the class Connection, within the class you can use $this because it refers to the same class to use the variables and functions within it, but outside of the class what you should do is instantiate the class and then call the methods, for example:

<?php
class Conexion{

  public $conn = null;

  public function __construct(){
    $this->conn = mysqli_connect("localhost","root","","futuro");
  }
}

Then at the top of the other file you have $objeto=new Conexion; and you must correct it so that the object can be instantiated, so $objeto = new Conexion();

Since the Conexion class already exists with that, then in the line where you have the error you must change the $this for $objeto staying like this:

$enero1 = mysqli_query($objeto->conn, $sql);//$conn variable publica en la clase
    
answered by 11.11.2016 в 07:29