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>";


?>
5
  • 2
    As people tend to leave things out of the code that show, can you tell us which line is line 42 Commented Jan 31, 2017 at 23:31
  • 1
    And the COMPLETE error message would also be nice Commented Jan 31, 2017 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. Commented Jan 31, 2017 at 23:43
  • 1
    Please also read : stackoverflow.com/questions/60174/… Commented Jan 31, 2017 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. Commented Jan 31, 2017 at 23:54

1 Answer 1

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) . "
Sign up to request clarification or add additional context in comments.

4 Comments

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: gist.github.com/andergmartins/3d571ad67c21449b9bad66515cf2b297
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.
@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: gist.github.com/andergmartins/3d571ad67c21449b9bad66515cf2b297
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.