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
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));
   }
   ..
}

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

  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.

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