0

I'm doing a local project and i have a few problems that cant resolve, i search information in this phorum but couldnt fix this problem.

First: I do a SQL query, thats the result:

Cantidad  anio
115.77    2015
301.35    2016
1603.81   2017

Now i want to do a graphic line canvas, where on data i will put the range of years (range is chosen by user). I Will draw on the PHP file, this is my file now where the error is:

Notice: Array to string conversion on line 42

I though do a auxiliar acumulative variable and put this var on the script, but didnt work. I dont know if there are another best way to do it, and i will be glad with help.

Regards and thanks for read!

PD: I fixed the line 42, sorry about that: $aux+= " {year: '$anio', value: $dinero}";

<?php
include dirname(__FILE__).'/../Database/dbcon.php';
header('Content-Type: text/html; charset=utf-8');

$inicio=$_REQUEST['inicio'];
$fin=$_REQUEST['final'];
$anio=0;
$dinero=0;
$sql="Select SUM(gastos.Cantidad) 'Cantidad', year(gastos.Fecha) 'anio'
        from ciclo 
        inner join gastos on ciclo.Identificador=gastos.year_id 
        where ciclo.Ciclo BETWEEN $inicio and $fin GROUP by anio";

$resultado=$conn->query($sql) or die('fecha_fin');
$resul_field=$resultado->field_count;
$array_mes[]=array();
$array_cantidad[]=array();
$total[]=array();
$contador=0;
$c=0;
$aux=0;
        while($fila = mysqli_fetch_assoc($resultado)){
            foreach ($fila as $key => $value) {
                $anio=$fila['anio'];
                $Cantidad=$fila['Cantidad'];
            }
            $array_cantidad[$c]=$Cantidad;
            $array_mes[$c]=$anio;
            $c++;           
        }
        $long=count($array_cantidad);

for ($i=0; $i < $long; $i++) { 
    $contador=$array_mes[$i];
    if($contador<$fin){
        $anio=$array_mes[$i];
        $dinero=$array_cantidad[$i];
        $aux+=" {year: '$anio', value: $dinero},";
    }else{
        $anio=$array_mes[$i];
        $dinero=$array_cantidad[$i];
        **$aux+= " {year: '$anio', value: $dinero}";**
    }
}

echo "
<script>
new Morris.Line({
        element: 'graph_line',
        xkey: 'year',
        ykeys: ['value'],
        labels: ['Value'],
        hideHover: 'auto',
        lineColors: ['#26B99A', '#34495E', '#ACADAC', '#3498DB'],
        data: [
        $aux; 
        ]
    });   
    </script>";


?>
  • 2
    As people tend to leave things out of the code that show, can you tell us which line is line 42 – RiggsFolly Jan 31 '17 at 23:31
  • 1
    And the COMPLETE error message would also be nice – RiggsFolly Jan 31 '17 at 23:32
  • Counting from the top, line 42 is the second `$aux+= " {year: '$anio', value: $dinero}";`, where `$anio` and `$dinero` are being converted to strings. – Nathaniel Verhaaren Jan 31 '17 at 23:43
  • 1
    Please also read : http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Tim S. Jan 31 '17 at 23:49
  • To me, it looks like either `$anio` or `$dinero` is being converted from an array to a string. I think andergmartins' solution below will resolve the error message and successfully convert them to strings, but if the issue is that they aren't supposed to be arrays in the first place, then I think the problem is in your SQL query. You might want to tag this question under sql as well. – Nathaniel Verhaaren Jan 31 '17 at 23:54

1 Answers1

1

I think you can fix this changing a few things. Instead of concatenating your piece of data in a string variable, you can append it to and array and convert it to JSON before print on the script tag.

Change:

$aux=0;

To this:

$aux = array();

Change:

$aux+=" {year: '$anio', value: $dinero},";

and

$aux+= " {year: '$anio', value: $dinero}";

To this:

$aux[] = (object)array(
    'year'  => $anio,
    'value' => $dinero
);

Now we have an array with all the items you need. So you can convert it to an string, using JSON:

Change:

data: [
    $aux; 
]

To this:

data: " . json_encode($aux) . "
andergmartins
  • 2,738
  • 2
  • 13
  • 7
  • I had no chance to test running the code, so let me know if it doesn't work. You can check the full code here: https://gist.github.com/andergmartins/3d571ad67c21449b9bad66515cf2b297 – andergmartins Jan 31 '17 at 23:51
  • Thx for answer, when i execute it and inspect the script this is the script: data: [{"year":[],"value":[]}] Dont write the value of the array. –  Feb 01 '17 at 00:26
  • @Tote, yw. Why do you have 2 loops for the $fila variable? I think you just need the "while" loop, which will return each row of the result. I tried to simplify your script so you can get it working adding the values to the javascript code first, then add more logic or filters later. Please, could you check again: https://gist.github.com/andergmartins/3d571ad67c21449b9bad66515cf2b297 – andergmartins Feb 02 '17 at 15:38
  • Hi, I put 2 loops because usually practice with mysqli_fetch_array, and a few times need the associative arrays, i didnt know that i can do it with a while loop, im still studying Programming at University :S I tried with your code, if i put the route to PHP (in browser) i can see the script working fine, but when it is executed on the browser (with jquery ajax), it only show me : data: [] I think maybe its caused by the way that do the json, because i tried do a simple json and jquery received and show it well. –  Feb 02 '17 at 20:16