Jump to content

Find value of Previous Row?


unemployment

Recommended Posts

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'];
         }
}

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

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

 

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.