How to repeat columns up to "x" times

6

I have a column in my database with names, I write it on the screen, through this code:

<html>
    <head>
        <title>Pagina Inicial</title>
        <meta charset="utf8">
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
<?php
;   include("connect.php");
    $consulta = "SELECT * FROM materias";
    $con = $mysqli->query($consulta) or die($mysqli->error);

    ?>
    <?php while($dado = $con->fetch_array()){
        ?>
<?php for($nomes =1; $nomes <= 9; $nomes++){?>
<div id="nomes">
        <?php echo $dado["$nomes"];?>
    </div>
    <?php } ?>      
        </div>
        <?php } ?>
    </body>
</html>

    
asked by Bruno Ferreira 28.09.2015 в 01:04
source

3 answers

5

One tip: Organize your code, opening and closing the PHP tag several times may end up causing you to get lost. One tip is also to save a variable in HTML, and give echo at the end, or several echo during execution.

On the problem, I believe that I just add one more for going from 1 to 4, as I observed in the image along with a <br> tag to break line.

<?php 
    for ($i = 1; $i <= 4; $i++) {

        for($nomes =1; $nomes <= 9; $nomes++){

            <div id="nomes">
                <?php echo $dado["$nomes"];
            </div>

        }
        <br>
   } 
?>  
    
answered by 28.09.2015 / 01:33
source
5

I know the question already has a chosen answer, but I will still respond and make some comments.

1st

Why is the table structure like this ? Since there are several users / names for different uses, why are they all on the same line?

See an example of the reshaped table:

CREATE TABLE IF NOT EXISTS materias (
id INT(11) NOT NULL AUTO_INCREMENT,
nome VARCHAR(20) NOT NULL,
INDEX(nome),
UNIQUE KEY(id),
PRIMARY KEY(id)
) DEFAULT CHARSET=latin1;

INSERT INTO 'materias'('id', 'nome') VALUES (NULL, 'Bruno'), (NULL, 'Bruna'), (NULL, 'Gabriel'), (NULL, 'Gabriela'), (NULL, 'Felipe'), (NULL, 'Andre'), (NULL, 'Luiz'), (NULL, 'Gustavo'), (NULL, 'Otavio');

If possible delete the current table in use, and run this SQL code in the database in use.

2

Do I really need to run looping twice ? See, in the example you posted above, you have a looping for within a while . I mean, here I do not see any need to do this, or maybe you have been forced to do this because of estrutura of your table.

I would do something like this:

$sql = $mysqli->query("SELECT * FROM materias");
if($sql->num_rows > 0){
    while($linha = $sql->fetch_array()){
        print "<div id=\"nomes\">"; 
        print $linha["nome"];   
        print "</div>";
    }
    mysqli_free_result($sql);   
} else {
    die("Sem resultados");  
}
mysqli_close($mysqli);

Notice that I have a single looping , which does exactly the same thing as your code did, as well as being a good practice is simple.

Or, this way:

...
print "<div id=\"nomes\">"; 
while($linha = $sql->fetch_array()){
    print $linha["nome"] . "<br/>";   
}
print "</div>";
...

That would make the returned names stay in a single <div> tag.

Another thing is, if the goal is to limit the number of results to be processed and subsequently displayed, why not use the LIMIT clause?

See another example:

$sql = $conexao->query("SELECT * FROM materias LIMIT 4");

This means that of the 9 names in the table, only the first 4 are selected, and then printed in the loop.

I hope you understood. Good luck.

    
answered by 28.09.2015 в 02:29
0

It is simple to do but has to have a table that calls t_vez which contains only fields "number name" , which has numbers from 1 to 200. So you can see from the sql below, that a search is done on the t_vendas table, which has a field named vendaquantidade , which could be how many records are going to be printed.

The search checks whether you have vendasquantidades greater or equal to number, example if you have in the field salesquantities the number three then it will repeat 3 lines because in the field number of the table you have numero one (minor), two (minor) and three (equal).

SELECT * FROM  T_VENDAS AS A, T_VEZ AS B WHERE A.VENDAQUANTIDADE >= B.NUMERO 
  AND A.VENDANUMERO = ' + INTTOSTR(RESULTADO) + ' AND A.VENDAIMPRIMIR = "SIM" 
  ORDER BY A.VENDAQUANTIDADE, A.PRODUTONUMERO
    
answered by 15.07.2017 в 22:54