For each result, a new line in php

0

I have a database where in the users table I have the columns:

+----------+------------+----------+------------+----------+------------+
| imp_cod1 | imp_local1 | imp_cod2 | imp_local2 | imp_cod3 | imp_local3 |
+----------+------------+----------+------------+----------+------------+
|       13 | RECEPÇÃO   |          |            |       18 | CEO        |
+----------+------------+----------+------------+----------+------------+

Note that the values of imp2 are all empty, because in this case only imp1 is with data. In my html code I use the following format to display the imp1 information:

 <div class="panel-body">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>#</th>
                <th>Impressora</th>
                <th>Modelo</th>
                <th>Contagem</th>
                <th>Situação</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th scope="row"><?php echo $imp_cod1; ?></th>
                <td><b><?php echo $imp_local1; ?></b></td>
                <td><?php echo $imp_modelo1; ?></td>
                <td><?php echo $imp_contador1; ?></td>
                <td><button type="button" class="btn btn-round btn-success"><?php echo $imp_situacao1; ?></button></td>
            </tr>
        </tbody>
    </table>
</div>

Now when I have a second given in imp2 , imp3 ... I go manually and create a new result field as the above example quoted <tr>

I would like to know if there is any way I can make it automatic so that I do not have to edit PHP every time I insert or delete new data in the table.

Connection code with db (every page that requires the user to be logged in has an include for this page): link

p>

Code from my page where I'm having trouble automatically adding new rows: link (on line 251 has the first fill of data , it ends at 257. Below is another 7 fills where I already left it ready.)

Now suppose that the user only has imp1 and all others are empty, even so my system will look for all other ones and leave it blank, and because the html code is already created, it leaves the lines created but in white too. I would like to know if I can make it become automatic in theory: if the imp field is empty do not create line, otherwise create line and fill data ...

If I'm going the wrong way in logic I'm able to change, I'm a beginner and I was urgently at the end of it.

Here is a photo of how the system is left with blank data:

    
asked by anonymous 18.01.2017 / 20:27

1 answer

1

It's obvious that your code is written in the normalization of data .

Although your problem is not repetition of data between records, you end up creating repeated columns, which is practically impossible to maintain only by PHP code (there is always a problem that resolves). Your problem is corrected by 1FN

Basically you have a 1. * (one-to-many or one-to-many) relationship. Since your unique record is user and the related record imp .

As a diagram, we can define the relationship as (I will use only the required fields):

Thisaggregation-typerelationshipallowsyoutohaveaninfinitenumberoflocationstoassociatewiththeuser.

Anothercasemayoccurifyouhavealimitedanddefinedlistoflocations.Inthatcase,therelationshipwillchangeandwillbeconsideredasimpleassociation:

Basically,theaboverelationshipisforwhenyouhave,forexample,alistofprinters.Andtheseprinterscanbesharedamongusers,buttherecanbenoprintersotherthanthosepreviouslyregisteredintheimptable.

Asamatterofcuriosity,thereisanothercase,whichisanextensionofthesecondexample,iswhenyouhaveadefinedamountofitems(10printers),butprinterscannotbeshared.Anyonewho"gets" the printer will have it reserved and will be accessible only to the linked user.

In the latter case, just add the column imp_id of table user_has_imp to unique key . In this way, a printer can not be shared between users and will continue with the possibility of a user having more than one printer.

Regardless of the chosen approach, both will give you the possibility and flexibility of how many associations you create.

For code question, just return the association:

//prepared statement da PDO
$query = 'SELECT * FROM imp WHERE user_id = :userid';

or

//prepared statement da PDO
$query = 'SELECT * FROM imp WHERE id IN (SELECT imp_id FROM user_has_imp WHERE user_id = :userid)';

With the return of the data, just loop the result:

$statement = $pdo->prepare('SELECT * FROM imp WHERE user_id = :userid');
$statement->bindValue(':userid' , $userId);
$statement->execute();

foreach($statement->fetch() as $row)
{
    var_dump($row);
}
  

Now suppose that the user only has imp1 and all others are empty, even so my system will look for all other ones and leave it blank, and because the html code is already created, it leaves the lines created but blank as well . I would like to know if I can make it become automatic in theory: if the imp field is empty do not create line, otherwise create line and fill data ...

Just use the logic of foreach , shown above, to create HTML. It will only be created for the results that exist.

PS: All codes are just examples.

PS. 2: names created by MySQLWorkBench.

    
19.01.2017 / 15:01