Jump to content

[SOLVED] Referencing previous loop items?


woolyg

Recommended Posts

Hi,

 

I'm trying to reference an item in an already-passed section of a while loop. The idea is to group items by date, and display the date for that group, but if the date for a line is the same as the line directly previous to it, then don't output the date for that line, effectively creating the display below as an example:

 

Example:

DATE

ID - Firstname - Lastname

 

eg:

Thursday 14th August 2008

1 - John - Smith

2 - Dee - Brady

 

I'm using:

<?php
$get_players = "
SELECT players.player_id, players.date_joined, players.firstname, players.lastname 
FROM players
";
$run_players = mysql_query($get_players);
$num_players = mysql_num_rows($run_players);


while($display_players= mysql_fetch_assoc($run_players)){

$player_id = $display_players['player_id'];
$date_joined = $display_players['date_joined'];
$firstname = $display_players['firstname'];
$lastname = $display_players['lastname'];


echo $date_joined."<br />";
echo $player_id." - ".$firstname." - ".$lastname."<br /><br />";

}
?>

 

..but where it says

<?php
echo $date_joined."<br />";
?>

 

I'd like to run

 

<?php
if($date_joined == **SAME VALUE OF PREVIOUS INSTANCE OF LOOP**){
//Do Nothing
} else {
echo $date_joined."<br />";
}
?>

 

Can anyone help? I hope this is clear enough..

WoolyG

Link to comment
https://forums.phpfreaks.com/topic/119564-solved-referencing-previous-loop-items/
Share on other sites

SELECT player_id, date_joined, firstname, lastname

FROM players GROUP BY date ORDER BY date DESC

 

$currentDate = '';
while ($row = mysql_fetch_assoc($result)) {
   if ($currentDate !== $result['date']) {
       $currentDate = $result['date'];
       list($date, $time) = explode(' ', $result['date']);
       list($year, $month, $day) = explode('-', $date);
       list($hour, $minute, $second) = explode(':', $time);
       echo date('<your-format>', mktime($hour, $minute, $second, $month, $day, $year));
   }
   ..
}

 

 

Ah nice one - I've kinda taken what you're saying & done the following:

 

<?php
while($display_players = mysql_fetch_assoc($run_players)){

$players _id = $display_players[player_id'];
$players _datetime = $display_players['date_joined'];
$players _date = substr($players_datetime, 0, 10);

if($cur_date == $players_date){
} else {
echo "<br/><br/>$players_date<br/>";
}
echo "players ID: ".$players_id."<br />";


$cur_date = substr($players_datetime, 0, 10);

}
?>

 

Thanks for your help

WoolyG

Archived

This topic is now archived and is closed to further replies.

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