0

So I have this array data which i want to be coded somewhat like [lojas, raparacoes, valor],[nome_1, count_1, val_1],[nome_2, count_2, val_2], etc, etc...

lojas, reparacoes and valor are like headers

nome_* comes from $row['nome']

count_* comes from intval($row['COUNT( DISTINCT id_reparacao )'])

val_* comes from intval($row2['SUM(valor)'])

$data = array(array('Lojas'), array('Reparacoes'), array('Valor'));
$qry=mysql_query ('SELECT COUNT( DISTINCT id_reparacao ) , lojas.nome, lojas.id
FROM reparacoes
INNER JOIN lojas ON lojas.id = id_loja 
GROUP BY lojas.id ');

        while($row = mysql_fetch_array($qry))
        {
            $qry2=mysql_query ('SELECT SUM(valor) FROM re_servicos where id_reparacao=(select id_reparacao from reparacoes where id_loja='.$row['id'].' and estado="Fechada")');
            while($row2 = mysql_fetch_array($qry2))
                {
                    $data=[$row['nome'],intval($row['COUNT( DISTINCT id_reparacao )']), intval($row2['SUM(valor)'])];   
                }
        }

However, with this code I'm not getting the desired output in the array, I guess the problem is the way i fill it but I don't know how to properly fill it so it gets the output I posted in the first paragraph.

PS: I don't know if it matters but for better understanding, I need this array to build a google bar chart

1

1 Answer 1

1

You can try this code.

 $data = array();
$data[] = array('Lojas', 'Reparacoes', 'Valor');
$qry=mysql_query ('SELECT COUNT( DISTINCT id_reparacao ) , lojas.nome, lojas.id
FROM reparacoes
INNER JOIN lojas ON lojas.id = id_loja 
GROUP BY lojas.id ');

        while($row = mysql_fetch_array($qry))
        {
            $qry2=mysql_query ('SELECT SUM(valor) FROM re_servicos where id_reparacao=(select id_reparacao from reparacoes where id_loja='.$row['id'].' and estado="Fechada")');
            while($row2 = mysql_fetch_array($qry2))
                {
                    $data[]=array($row['nome'],intval($row['COUNT( DISTINCT id_reparacao )']), intval($row2['SUM(valor)']));   
                }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Working perfectly, Thank you. (I'm accepting this answer as soon as possible)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.