Displaying array results question

Hello All
First please go gentle with me as I am back to php after a long break and my mind is not as sharp as once was :smiley:

I am in the prettifying the public display of data and would like some help in how to achieve this little bug bear of mine

In a foreach loop I would like to display my $row->date only for the fist time and then the result equaling this date then the next date.

Currently i have the output but the date is dis-plaid for all rows which make the list look too busy.

I have tried with doing an if $date = $date2 kind of filter but then either get no date or just the first row of each date involved.

I hope that makes sense and you would be kind enough to kick an old brain into touch :smiley:

DC x

The most general way of doing this is to index/pivot the data around the date column, storing the fetched rows of data from the query as a sub-array for each date value/index -
[php]$result = [];
while($row = your_fetch_statement_here…)
{
$result[$row[‘date’]][] = $row;
}[/php]

To produce the output, just loop over the data, getting the index (date) and the array of data for each index value -
[php]foreach($result as $date=>$arr)
{
// $date will be the common index/date for the current set of data
// output any one-time heading here…
foreach($arr as $row)
{
// $row will be each row of data for the current date
// output the data under the heading here…
}
// output any footer for each set of data here…
}[/php]

Thank you phdr for the code help.

I pondered and with the code you supplied my old brain kicked in and I figured out the foreach loop insertion and here it is below for others who may find it helpful

[php]
$date = $row->matchdate;
$nicedate = date(‘d-m-Y’, strtotime($date)); // creates UK date format to output
$Mdate =’’;
foreach ($rows as $row){
if ( $Mdate != $row->matchdate ) {
$showMdate = $nicedate; // $showdate is used in the $output array of table cells and rows.
} else {
$showMdate = ‘’;
}
$output .= ‘

enter your output here inc $showdate var’;
}
[/php]

This will display only the first display of the date td cell and then black the rest if it $Mdate equals the date. Then its starts over with the next date

Again thank you for kicking my old grey cells into action and I hope this helps someone.

DC x

Sponsor our Newsletter | Privacy Policy | Terms of Service