<?php
include('db.php');
$query=mysqli_query($con,"select * from reg");
while($row=mysqli_fetch_array($query)) {
echo $id;
echo $name;
}
?>


i want to display it as follows:


1.chaitanya  2.swati
3.rena       4.geeta  

Dani AI

Generated

A few notes to complement the replies already in this thread (original question from and the quick reply by ). The goal is to keep presentation in HTML/CSS and keep the SQL/PHP focused on safely outputting each record. That avoids ad-hoc line breaks from PHP and makes layout predictable across different name lengths and screen sizes.

A simple, safe pattern that separates concerns and produces two items per row uses a semantic list and CSS flexbox. Values must be escaped for HTML output (example uses htmlspecialchars) and the result set should be ordered explicitly in SQL:

<ul class="two-col">
<?php
while ($row = mysqli_fetch_assoc($result)) {
    echo '<li><strong>' . htmlspecialchars($row['id']) . '.</strong> '
         . htmlspecialchars($row['name']) . '</li>';
}
?>
</ul>

<style>
.two-col { display:flex; flex-wrap:wrap; list-style:none; margin:0; padding:0; }
.two-col li { width:50%; box-sizing:border-box; padding:6px 8px; }
</style>

If Bootstrap is already in use, a grid makes this trivial and responsive:

<div class="row">
<?php
while ($row = mysqli_fetch_assoc($result)) {
    echo '<div class="col-xs-6">' . htmlspecialchars($row['id']) . '. '
         . htmlspecialchars($row['name']) . '</div>';
}
?>
</div>

Additional practical points: use mysqli_fetch_assoc or PDO with prepared statements to avoid injection; include ORDER BY id in the query for stable ordering; handle empty result sets; and trim or ellipsize very long names in CSS. As noted, tables or monospace <pre> can work for strict column alignment, but CSS Grid/Flexbox gives more control. The slideshow question is a separate concern; as pointed out, that belongs in its own thread so both problems stay focused.

Recommended Answers

All 5 Replies

Member Avatar for Member #120589

A simple one...

$output = '';
$x = 0;
while($row=mysqli_fetch_array($query)) {
    $output .= $row['id'] . '. ' . $row['name'] . " ";
    if($x%2 == 0) $output .= "<br />";
    $x++;
}

However if you want fixed width items, then either make a table or fixed-width list items OR even <pre> tags with monospaced font and str_padded &nbsp;

thank you

        i want to display this slideshow dynamically  please help me. I am getting more than
        3 images if i put dynamic code

        <div class="row">
        <div id="carousel-reviews" class="carousel slide" data-ride="carousel">

        <div class="carousel-inner">
        <div class="item active">
        <!-------------------------------->
        <div class="col-md-4 col-sm-6">
        <div class="block-text rel zmin">
        <a title="" href="#">Active1</a>

        </div>
        </div>

        <!-------------------------------->
        <div class="col-md-4 col-sm-6 hidden-xs">
        <div class="block-text rel zmin">
        <a title="" href="#">Avtive2</a>

        </div>
        </div>
        <!-------------------------------->
        <div class="col-md-4 col-sm-6 hidden-sm hidden-xs">
        <div class="block-text rel zmin">
        <a title="" href="#">Avtive3</a>

        </div>
        </div>
        <!-------------------------------->

        </div><!------------------------------//item1------------------------------->   



        </div> <!---carousel-reviews ---->







        <a class="left carousel-control" href="#carousel-reviews" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
        </a>
        <a class="right carousel-control" href="#carousel-reviews" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
        </a>
        </div>
Member Avatar for Member #120589

I don't think that this second question is related to the original post nor the thread title. As such it is of very little use to anybody. Please post unique / non-related questions in their own threads (after searching daniweb for solutions of course).

If this is a duplicate question then may I respectfully ask that contributors go to the link posted by mattster rather than answer here.

The only contributions that should appear here are ones concerned with the original post - thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.