Rescue Data to Create Dynamic Tables and Charts

Asked

Viewed 59 times

0

all right?

I have a question here in a project I picked up, I am not an expert in database and much less in php, I am in the learning phase. I need a function to collect some database data in a way that I can use them to create some tables and graphs.

It should look more or less like this table: Tabela final

But she’s bringing the data like this: Tabela Nutella

I need the data to be listed in the way shown in the first table, and then I need to do the sum corresponding to the month, in order to generate a graph. Don’t notice the graph, but only the marking of the circle in red which is what I need at the moment: Dados mensagem com gráfico

There are two tables to work with this data. One is the font table, which would be where the person got the contact (google, Whatsapp, pamphlet, etc). The other table would be the registration table, which contains the "registration date" and the "ID" of the contact source.

My code is as follows:

Model

function total_fontes() {       

    $this->db->select('COUNT(*) AS total');
    $this->db->from('clientes cl');
    $this->db->join('fontes ft', 'ft.id_fonte = cl.fontes_id_fonte');

    $query = $this->db->get();  

    foreach ($query->result_array() as $val) {
        return $val['total']; 
    }
} 

Controller:

public function index() {

    $data = array(
        'page_title' => 'Gráficos',
        'show_clientes' => $this->mdl_clientes->get_datas(), //busca de datas com group_by
        'show_fontes' => $this->mdl_fontes->get_ordem_alfabetica(),//busca fontes e ordena alfabeticamente
        'fontes_quantidades' => $this->mdl_fontes->total_fontes(),//busca o total de fontes por dia (nao funciona)
    );

    $flash_msg = $this->session->flashdata('flash_message');
    if (isset($flash_msg) && !empty($flash_msg)) {
        $data['flash'] = $flash_msg;
    }

    $this->load->view('graficos/vwGraficos.php', $data);

}

View (no formatting, I’m searching first to be able to list):

<table border="1">

    <?php if($show_fontes != FALSE) : ?>

            <tr>
                <td>DATA</td>

                <?php foreach($show_fontes as $fnt) : ?>

                    <td>
                        <?= $fnt->titulo_fonte; ?>
                    </td>

                <?php endforeach; ?>
            </tr>
    <?php endif; ?>    

            <?php if($show_clientes != FALSE) : ?>

                <?php foreach($show_clientes as $clnt) : ?>
                    <tr>
                        <td>
                            <?= $clnt->data_registro; ?>
                        </td>
                        <td>
                            <?= $fontes_quantidades; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>

            <?php endif; ?>

If you can help me, I’d appreciate it.

  • To work with database you can use PDO or mysqli, but recommend using PDO

  • I can’t help you with the sql part. I’m not sure what query you want to do. But as for the Codeigniter part, you’re returning the total_fonts function within a loop - which wouldn’t make sense even if you wanted to return only the first value. Then, in the view, you repeat that same value on all rows, resulting in your 2640 repeated in the facebook enterprise column.

  • I need to perform a query in the customer table, and check how many contacts per day were conducted through pamphlets, google, facebook, Whatsapp, etc. In the table "sources" are the types of contact that can be registered by the customer or removed, and in the clients table there is a column with the ID of these sources. Then you would need a query that returns the number of contacts from each type of source, separated by date. I think I messed up the explanation a little bit but that’s basically it.

  • I think I get it. The query is wrong. Count(*) returns a single value which is the number of rows in the table. What you need is (if and only if your db->Join() is correct) a SELECT *, to select all data (The result is an array). You also have a problem in the view. To pre-refill all table data, the solution will probably have a loop inside a loop. This is relatively laborious. It will take a few hours. But, first hit the database query.

  • Got it, thanks a tip, gave a huge light now for what I’m doing. Thanks,

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.