0

So I have a code in PHP to display MySQL database information. This is the PHP code:

<?php
$servidor = mysqli_connect ("localhost","root","");
mysqli_select_db($servidor, "produtos");

if (isset($_GET['texto'])) {
    $pesquisa = $_GET['texto'];
    $query = mysqli_query($servidor, "SELECT * FROM produtos WHERE Nome LIKE '%$pesquisa%' OR Referencia LIKE '%$pesquisa%'");

    if(mysqli_num_rows($query) > 0) {
        while($resultados = mysqli_fetch_array($query)) {
            echo "<h3>Nome: ".$resultados['Nome']."</h3>Referência: ".$resultados['Referencia']."";
        }
    } else {
        echo "<h3>Não foram encontrados resultados!</h3>";
    }
}
?>

But right now, that information is displayed in one single line, like this:

enter image description here

and I want to display it in rows, like this:

enter image description here

How can I do this in PHP?

Thank you

5
  • Use modulus operator to check if the current row is divisible by 4 ,also add a div which holds 4 columns and if the row number is divisble by 4 add a close and open div tag for the next row.Just a hint since I see you don`t like to accept or upvote answers. Commented May 20, 2018 at 18:30
  • 1
    The major part is done in html Commented May 20, 2018 at 18:30
  • @Mihai ok....I like to accept or upvote but sometimes I forget to. Also, I would like to get my questions upvoted Commented May 20, 2018 at 18:42
  • WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented May 20, 2018 at 19:01
  • Note: The object-oriented interface to mysqli is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete mysql_query interface. Before you get too invested in the procedural style it’s worth switching over. Example: $db = new mysqli(…) and $db->prepare("…") The procedural interface is an artifact from the PHP 4 era when mysqli API was introduced and should not be used in new code. Commented May 20, 2018 at 19:01

2 Answers 2

1

You might consider using the new column attributes introduced by CSS 3.

Example:

p {
  column-count: 3;
  column-width: 50px;
}
<!doctype html>
<html lang="de">

<body>
  <p>
    First column
  </p>
  <p>
    Second column
  </p>
  <p>
    Third column
  </p>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

0

This part can be achieved usign HTML + CSS as @Bernhard said. Or, something that I DON'T RECOMMEND, like this:

<div style="width:25%;float:left;padding:10px;">{item}</div>

Comments

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.