Jump to content

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
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.

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

 

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];
        }

 

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...