Jump to content

How do you list two columns but not show duplicates from first column?


Recommended Posts

Hi All,

I've been trying to work out how to list items in a two column database in a table side by side without repeating the left hand items.

The left column is a main category column, the right column is a sub category column. 

I'd like it to look like this:

 Main Category                         Sub Category

Apportioned Expenses             Home Office

Bank Fees Loans                       Loan Drawdowns

                                                    Bank Fees

                                                   Loan Fees

                                                   Loan Repayments

GST Claimable                          Marketing/Media

                                                   Office Expense

                                                   etc

and so on. 

this is what I'm seeing instead

     image.png.86efb2d903a2c6cdc63b8789db66cf65.png 

The database "categories" looks like this   

image.png.de9d0c65a68179551d42f40179e747cc.png           

Code so far looks like this

   

$query3= "SELECT DISTINCT* FROM categories ORDER BY maincat";
try{
    $stmt = $mysqli->prepare($query3);
    $stmt->execute();
    $resultSet1 = $stmt->get_result(); 
    $result1 = $resultSet1->fetch_all();
}

and 

<?php  foreach ($result1 as $output_1) {?>
                                            <tr>
                                                <td> <a href=""><?php echo $output_1[0];?> </a></td>
                                                <td> <a href=""> <?php echo $output_1[1];?></a></td>
                                                <?php } ?>                      
                                            </tr>

     Any thoughts would be much appreciated. Thanks in advance.

image.png

image.png

MYSQL version

image.png.651ee9ed0c90e898ff886980ab41ab69.png

image.png

Edited by Plugnz13

when you fetch the data from the query, you would index/pivot it using the maincat as the main array index. this will give you a sub-array of rows for each maincat value. when you loop to display the result, you can use php’s count() function to get the number of rows for each maincat value and use this as a rowspan attribute in the column where you are displaying the maincat value.

 

Only out the maincat when it changes to a new value.

set prevcat = maincat
foreach record
    start new row
    if maincat != prevcat
        output maincat
        set prevcat = maincat
    else
        output blank
    endif
    output subcat
    end row
end foreach

 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.