woolyg Posted August 13, 2008 Share Posted August 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119564-solved-referencing-previous-loop-items/ Share on other sites More sharing options...
ignace Posted August 13, 2008 Share Posted August 13, 2008 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)); } .. } Quote Link to comment https://forums.phpfreaks.com/topic/119564-solved-referencing-previous-loop-items/#findComment-615976 Share on other sites More sharing options...
woolyg Posted August 13, 2008 Author Share Posted August 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119564-solved-referencing-previous-loop-items/#findComment-615995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.