如何在html和php中水平拆分大型动态List

I am trying to display a list of categories from a Database result, As the list will increase day by day how can i split the li elements so that they align themselves horizontally and vertically.

Here is my code snippet

<?php
$query="SELECT id,category FROM category`";
$res=mysql_query($query);
$count=mysql_num_rows($res);
while($data=mysql_fetch_array($res))
{
?>
<table>
<tr>
    <td>
        <li><a><?= $data['category']?></a></li>
    </td>
</tr>
</table>
<?php
}
?>

I tried using css, it worked for smaller results, but it became difficult to organize the list equally. Example: Flipkart lists their categories in the following way enter image description here

Any suggestions, Or any other simple way to handle large list of categories?

Thank you.

Here is a small code i wrote for you write now. the "breakafter" variable will define when to create a UL. I think this is what you are looking for.

As far as Flipkart goes, I think they have a CMS where they manage each column separately. Because the number of links that are generated are not equal to each other & this flexibility is very difficult to manage by coding.

Check this out, maybe exactly what you want. Also, I just printed the '$i' so that you know when the ul & li are generated.

$count  = mysql_num_rows($query);
echo $count;
$i = 0;
$breakafter = 5;

while($data=mysql_fetch_array($query)){ 
$i++;
if($i == 1){ 
echo '<ul><li>'.$data['samplecode'].'-i is - '.$i.'</li>';

} elseif ($i % $breakafter == 0) {

echo' <li>'.$data['samplecode'].'-i is - '.$i.'</li></ul><ul>';


} else {

echo' <li>'.$data['samplecode'].'-i is - '.$i.'</li>';
}



}