Jump to content
IndianaGuy

fetch statement with grouping

Recommended Posts

Hello folks, I am afraid i may not be using the correct wording with the "Title", but I hope I can clarify. I have a table with three columns

column 1: FileName column 2: FinishDate column 3: FinishTime

 

When I do a fetch the rows and display them I get

FileName, FinishDate, FinishTime

FileName, FinishDate, FinishTime

FileName, FinishDate, FinishTime

etc etc

 

What I am trying to do is to group the same FileName to display

FileName

    FinishDate, FinishTime

    FinishDate, FinishTime

etc
FileName

    FinishDate, FinishTime

    FinishDate, FinishTime

etc

 

Is this possible? and can you lead me to the correct tutorial or link with explanations. Thank you so much

 

Edited by IndianaGuy

Share this post


Link to post
Share on other sites

Create an empty-string variable outside the loop that produces each row. Now create a if condition at the beginning of loop, that checks the current row title (filename) against the variable current value, if they do not match show the filename title, at the end before the loop returns to begin again! Make the variable equal the current filename title value. That's it! simples.

Share this post


Link to post
Share on other sites

Excellent. Thank you so much. I thought of a similar solution, but thought maybe I should do some research before I reinvent the wheel.

 

Share this post


Link to post
Share on other sites

This sounded easier to do that I had thought. I am trying to wrap my mind around the loop but it is not working out for me. I think "Create an empty-string variable outside the loop that produces each row." is what is confusing me.  How is this done? Thank you

 

Share this post


Link to post
Share on other sites

This is the same method applied to a foreach loop of array items

        $storeTitle = "";

        $data = [['filename1', 'finishdate1', 'finishtime1'], ['filename1', 'finishdate2', 'finishtime2'], ['filename2', 'finishdate3', 'finishtime3'], ['filename2', 'finishdate4', 'finishtime4'], ['filename3', 'finishdate5', 'finishtime5']];
        foreach ($data as $key => $row) {
            if ($storeTitle !== $row[0]) {
                echo '<h1>' . $row[0] . '</h1>';
            }
            echo $row[1] . ' : ' . $row[2] . '<br>';


            $storeTitle = $row[0];
        }

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×