Narendra Verma Narendra Verma - 1 month ago 10
PHP Question

ul li tag is repeating in view page source using php

I am fetching menu from database and it displaying proper but when I checked on view page source it displaying like below code:

<div class="menu-header">
<ul>
<li>Home</li>
</ul>
</div>
<div class="menu-header">
<ul>
<li>About us</li>
</ul>
</div>
<div class="menu-header">
<ul>
<li>Contact us</li>
</ul>
</div>

etc..


It should be display like this below code

<div class="menu-header">
<ul>
<li>Home</li>
<li>About us</li>
<li>Contactus</li>
</ul>
</div>


This is my php code:

if (isset($result->num_rows) > 0) {
// output data of each row
while($row = $result->fetch_assoc()) { ?>
<div class="menu-header">
<ul>
<li><?php echo $row['Menu_name'];?></li>
</ul>
</div>
<?php
}
}

Answer

You should put only <li> line inside while loop like this:

<div class="menu-header">
    <ul>
        <?php if (isset($result->num_rows) > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) { ?>
                <li><?php echo $row['Menu_name'];?></li>
            <?php }
        } ?>
    </ul>
</div> 
Comments