Mikel Max
Mikel Max

Reputation: 185

DIV tag creating new line in php

I am trying to style results from database but when I echo <div> it is creating new line after each result. How can I force the div not to create a new line?

.message {
    border:2px solid;
    background-color:white;
}

php

$user = $_SESSION['username'];
$mydb = new mysqli('localhost', 'root', '', '');
$stmt = $mydb->prepare("SELECT * FROM messages where from_user = ?  ");
$stmt->bind_param('s', $user);
$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    echo"<div class='message'>";

    echo $row['to_user'];

    echo"</div>";
}

Upvotes: 0

Views: 1859

Answers (5)

DevZer0
DevZer0

Reputation: 13525

you can either display the div as inline-block or set float: [left|right]

Using inline style

style="display: inline-block;"

style="float: left;"

using css

.message {
   display: inline-block;
}

.message {
   float: left;
}

Upvotes: 0

Sujeet
Sujeet

Reputation: 43

Use css property display:inline-block e.g.

.message{
  border:2px solid;
  background-color:white;
  display:inline-block;
 }

It will arrange your messages linearly.

Upvotes: 0

tehAon
tehAon

Reputation: 122

Consider using <span> instead of <div>

<span> are already inline tags where <div> are blocks by default, which create a new line for the content in it.

Upvotes: 1

Leonardo DaVintik
Leonardo DaVintik

Reputation: 555

You create new DIVs every time. Every div by default starts with a new line.

Upvotes: 1

liyakat
liyakat

Reputation: 11853

.message{
        border:2px solid;
        background-color:white;
        float:left;
        }

try with above css will display in one line

Upvotes: 0

Related Questions