PHP中的动态行列显示

I have 3 tables as I mentioned in my logic. No. of categories and no. of domains for each category are not fixed because they are managed from admin panel. So 3 columns per row are fix but how many rows will be there is not fixed.

I want to display categories and data of each category in following mentioned way.

I want to display them in following way (as explained in below image.

I am using below logic to achieve desired output it's giving different output.Please help to correct my logic so it can work like above example.

<?php
$str_query_select = "SELECT d.domainpkid,d.domainname,c.categorytitle,dc.categorypkid FROM tr_domainname_category dc ";
$str_query_select .= "LEFT JOIN t_domainname d ON dc.domainpkid=d.domainpkid ";
$str_query_select .= "LEFT JOIN t_domainname_category c ON dc.categorypkid=c.categorypkid ";
$str_query_select .= "WHERE c.visible='YES' AND d.visible='YES' ORDER BY c.categorytitle,d.domainname ";
$rs_cat_list = GetRecordSet($str_query_select);

if (!$rs_cat_list->eof()) { // Check if recordset is not empty
$int_cnt = 0; // Used to make new row
$int_cat_pkid = 0; // Used to validate categorypkid
    while (!$rs_cat_list->eof()) { // loop starts
        if (($int_cnt % 3) == 0) { // To display 3 columns per row
        ?>
        <tr> 
        <?php } ?>
            <td width="250" align="left" valign="top">
            <?php
            if ($int_cat_pkid != $rs_cat_list->fields("categorypkid")) {
                $int_cat_pkid = $rs_cat_list->fields("categorypkid");
                print("<b>" . $rs_cat_list->fields("categorytitle") . "</b><br/>");
            }
            print($rs_cat_list->fields("domainname") . "<br/>");
            ?>
            </td>
            <?php
            $int_cnt++;
            $rs_cat_list->MoveNext();
            if (($int_cnt % 3) == 0) { // 
            ?>
        </tr>
            <?php
            }
        }
    }
    ?>

While below is output I am getting. Please help to resolve this issue. enter image description here

Thank you in advance, KRA

Create an array of categories first:

$cat = array();
while (!$rs_cat_list->eof()) { // here is your while loop
  $cat[$rs_cat_list->fields("categorypkid")][] = $rs_cat_list->fields("domainname");
  $rs_cat_list->MoveNext();
}

Now use the $cat array to do the processing.

foreach($cat as $key=>$val) { //here you can put the above logic just use the $cat array to do the things if (($int_cnt % 3) == 0) { // To display 3 columns per row ?> ", $val); ?>

        if (($int_cnt % 3) == 0) { // 
        ?>
    </tr>
        <?php
        }
    }

}

Note: Please do changes according to your need.

Use just 1 row with 3 columns;

Print normal <div> with a fixed size for each item in category.