Listing in indication system

Asked

Viewed 143 times

-2

I’m having a hard time. I’m creating a PHP referral system and I’m having difficulty listing the clients and the amount of referrals he made. The system has two tables, the table 'clients' which is where the data of the same is next to the indication code generated automatically, and the table 'indicated' where the id of the client who has just registered and the code of the person who indicated it. Now I’m not sure which logica to use to make the listing of the customer who has more nominees.

Tabela clientes

Tabela indicados

I gave it a try, but it still didn’t work out so well.

<?php
                    $PDO = conectar();
                    $sql = "SELECT * FROM clientes,indicados WHERE cod_cli=codcli_indic ORDER BY id_cli ASC";
                    $stmt = $PDO->prepare($sql);
                    $stmt->execute();
                    while($resultado = $stmt->fetchAll(PDO::FETCH_ASSOC)){
                        foreach($resultado as $user){
                            $qtd = count($user['codcli_indic']);
                        echo "
                        <tr>
                        <td>".$user['id_cli']."</td>
                        <td>".$user['nome_cli']."</td>
                        <td>".$user['loja_cli']."</td>
                        <td>".$qtd."</td>
                    </tr>
                        ";}
                    }
                ?>

The result was so: inserir a descrição da imagem aqui

Mauricio indicated 2 people, so there in indicated should appear 2 and not appear twice Mauricio in the table.

Good people, I tried several ways and I arrived at a satisfactory result, I will leave here as it was the code, in case someone has the same difficulty.

<?php
                    $PDO = conectar();

                    $sql = "SELECT *, count(*) as count FROM clientes,indicados WHERE cod_cli=codcli_indic GROUP BY codcli_indic";

                    $stmt = $PDO->prepare($sql);
                    $stmt->execute();
                    $resultado = $stmt->fetchAll(PDO::FETCH_ASSOC);

                    //$qtd = count($resultado);


                   foreach($resultado as $user){
                       if ($user['cod_cli'] == $user['codcli_indic']){
                        $qtd = count($user['id_indic']);
                       }


                        echo "
                        <tr>
                        <td>".$user['id_cli']."</td>
                        <td>".$user['nome_cli']."</td>
                        <td>".$user['loja_cli']."</td>
                        <td>".$user['count']."</td>
                    </tr>
                        ";
                    }
                ?>
  • Could [Edit] the question and add the table structures?

  • Sorry, I forgot to include

  • Tables have foreign key?

  • Your question is unclear, but see if it helps select max(sum), nome_cli from (Select count(c.id_cli) as sum, c.nome_cli from clientes c join indicados i on i.idcli_indic = c.id_cli group by c.id_cli, c.nome_cli)

  • I edited with a test I did, but it didn’t work out so well.

1 answer

0

'Cause then, in your case, I think it would be good for you to wear a INNER JOIN, in case there is a positive result in your query... Which would be in the case so:

SELECT * 
FROM clientes t1
INNER JOIN indicados t2
ON t1.cod_cli = t2.codcli_indic
ORDER BY t1.id_cli ASC

Suggesting you also read an illustration of how the queries occur JOINS for greater understanding of such consultations! :)


The duplication of results is due to the fact of having a foreach within a while, where you do not have the slightest need, and your code can stay like this:

while($resultado = $stmt->fetchAll(PDO::FETCH_ASSOC)){
    $qtd = count($resultado['codcli_indic']);
    echo "
    <tr>
    <td>".$resultado['id_cli']."</td>
    <td>".$resultado['nome_cli']."</td>
    <td>".$resultado['loja_cli']."</td>
    <td>".$qtd."</td>
    </tr>
    ";
}
  • Still, the repetition of the column continues to appear, but thanks for your help.

  • I saw where you went wrong and got your code, try now... Since you don’t need to have one foreach within a while, for the while will already make the result loop and put the foreach it will duplicate these results! :)

  • The replay is gone, thank you very much!! Now, the numbers of nominees is with the value 1 for the two users, and one of them has two nominees

  • Have you ever tried to do the count() only of $resultado? Because within the query it already does the checking, and there you are doing the counting only of 1 column. Try to do your Count() only of $outworking and return here for other doubts! :)

  • I tried yes, when I do the Count only of the $result, it presents the number 2 in both users :(

Browser other questions tagged

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