unemployment Posted May 1, 2011 Share Posted May 1, 2011 I have rows being generated in a foreach loop. I need to find the time in the row before it (or maybe after it if my logic is screwed up) to see if the times are equal. If they are equal, I need to see if the next row after that is equal. If it is not equal then stop. mysql user id time action 1 5 bought 1 5 purchased 1 5 sold 1 4 created foreach($newsfeed as $k => $news) { if ($news['time'] == $news['time'] of $k--) { echo " you bought purchased and created"; } else { echo $news['action']; } } Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/ Share on other sites More sharing options...
Zane Posted May 1, 2011 Share Posted May 1, 2011 update a temporary variable at the end of the loop... and declare it null beforehand. example $lastItemtime = null; foreach($newsfeed as $k => $news) { if ($lastItemtime == $news['time']) { echo " you bought purchased and created"; } else { echo $news['action']; } $lastItemtime = $news['time']; } Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/#findComment-1209238 Share on other sites More sharing options...
unemployment Posted May 1, 2011 Author Share Posted May 1, 2011 update a temporary variable at the end of the loop... and declare it null beforehand. example $lastItemtime = null; foreach($newsfeed as $k => $news) { if ($lastItemtime == $news['time']) { echo " you bought purchased and created"; } else { echo $news['action']; } $lastItemtime = $news['time']; } That's almost the solution except it doesn't work for the first row. Any way to make that work? Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/#findComment-1209241 Share on other sites More sharing options...
Zane Posted May 2, 2011 Share Posted May 2, 2011 I don't understand why not, $lastItemtime is set to null before the loop even starts. So it's not failing. What about it isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/#findComment-1209244 Share on other sites More sharing options...
unemployment Posted May 2, 2011 Author Share Posted May 2, 2011 I don't understand why not, $lastItemtime is set to null before the loop even starts. So it's not failing. What about it isn't working? These are the results I am getting. I want the works to disappear and for it to only form 1 row when the rows happen to have the same timestamp. You have made a purchase. 6 Hours ago works 6 Hours ago works 6 Hours ago Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/#findComment-1209246 Share on other sites More sharing options...
Zane Posted May 2, 2011 Share Posted May 2, 2011 I want the works to disappear and for it to only form 1 row when the rows happen to have the same timestamp. then you probably need to break out of the loop foreach($newsfeed as $k => $news) { if ($lastItemtime == $news['time']) { echo " you bought purchased and created"; break; } else { echo $news['action']; } $lastItemtime = $news['time']; } Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/#findComment-1209248 Share on other sites More sharing options...
unemployment Posted May 2, 2011 Author Share Posted May 2, 2011 Unfortunately I have multiple ifs in this loop and a break just breaks the whole thing. Plus I might have multiple scenarios in 1 loop mysql 1 5 bought 1 5 purchased 1 5 sold 1 4 created 1 5 bought 1 5 purchased 1 5 sold 1 4 created 1 5 bought 1 5 purchased 1 5 sold 1 4 created Where I would want the results to be You just bought purchased sold You just created you just bought purchased sold you just created etc... Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/#findComment-1209251 Share on other sites More sharing options...
unemployment Posted May 2, 2011 Author Share Posted May 2, 2011 I now have got my results to display... You just purchased and remove the next results of things that happened, but that still not the result I want. Any other way to check the variable value of the next key? Quote Link to comment https://forums.phpfreaks.com/topic/235306-find-value-of-previous-row/#findComment-1209432 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.