Page 1 of 1

News Feed Actions Issue

Posted: Wed May 18, 2011 3:56 pm
by unemployment
I have a fairly large issue. One that probably will not make sense but I'll do my best to explain. I'm in the process of making a news feed.

The feed might say something like.. Tino just changed his location to Heerlen, Netherlands.

Both the city an country are added in a separate actions when you change both the city and the country in the account settings.

If you update both of these fields at the same time the news feed query will concatenate those actions. Resulting in...

action_id details
userlocation~usercity Netherlands~Heerlen

Then I use explode to seperate them into an array.

Then I use a foreach loop to output the data.

In my foreach I have...
if($i > $max) break;
if($i == $max || ($i == count($information) && $i > 1)) echo ' and ';
else
{
	if($i > 1) echo ', ';
}
That works great for all of the other actions because it will say something like...

Tino has update his profile picture and settings.

But it fails when I need it to say...
Tino has update his location to Heerlen, Netherlands.

Any easy php fix for this or would I have to adjust the query?

Re: Unsolvable? Probably Not

Posted: Wed May 18, 2011 4:10 pm
by jacek
Why cant it be stored as two action,

one for the city change and one for country.

Or you could separate them with a character like | and then not allow that in the input box.

Or maybe use a serialised array, instead of explode.

Also. Please give any topics you create a descriptive title.

Re: Unsolvable? Probably Not

Posted: Wed May 18, 2011 4:49 pm
by unemployment
jacek wrote:Why cant it be stored as two action,

one for the city change and one for country.


I am storing the data as two actions, but the news query concatenates the actions if actions have occurred simultaneously.

resulting in a sentence structure like.... tino has update his action, action, action and action

This fails when you treat location as separate entities. Yet I need them to be separate for my privacy settings.
jacek wrote:Or maybe use a serialised array, instead of explode.
What would be the benefit?

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 6:22 pm
by jacek
Why not just store one action then :? for the user changing the location.
unemployment wrote: What would be the benefit?
You would not have to explode on a ' ' which would cause problems if the place name has a space in it. But you dont need any exploding if you just store one action.

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 6:24 pm
by unemployment
jacek wrote:Why not just store one action then :? for the user changing the location.
unemployment wrote: What would be the benefit?
You would not have to explode on a ' ' which would cause problems if the place name has a space in it. But you dont need any exploding if you just store one action.
I can't just store one action because both the city and the county/state have separate privacy settings. Thus, they can't be stored as one entity. I need both to be split to add privacy settings on each.

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 6:33 pm
by unemployment
I think it comes down to an array issue.

How can I merge the array, but I keep in mind that I only need to merge userlocation and the usercity arrays.

Current output...
Array
(
    [0] => Array
        (
            [0] => userlocation
            [1] => 19
        )

    [1] => Array
        (
            [0] => credentials
            [1] => 
        )

    [2] => Array
        (
            [0] => specialties
            [1] => 
        )

    [3] => Array
        (
            [0] => usercity
            [1] => place1
        )

)
Desired output...
Array
(
    [0] => Array
        (
            [0] => userlocation
            [1] => 19
            [2] => usercity
            [3] => place1
        )

    [1] => Array
        (
            [0] => credentials
            [1] => 
        )

    [2] => Array
        (
            [0] => specialties
            [1] => 
        )
)
if (strpos($news['action_id'], '~') !== FALSE)
{
	$actions = explode('~', $news['action_id']);
	$action_details = explode('~', $news['details']);
	
	$information = array();

	foreach ($actions as $k => $action)
	{
		$information[] = array($action, $action_details[$k]);
	}
}
How can this be done? The array is dynamic.

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 7:10 pm
by jacek
If "current output...2 is in $array
$array[0] = array_merge($array[0], $array[1]);
unset($array[1]);

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 7:22 pm
by unemployment
jacek wrote:If "current output...2 is in $array
$array[0] = array_merge($array[0], $array[1]);
unset($array[1]);
Huh? Please reference the example.

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 7:25 pm
by jacek
Sorry I got the keys wrong... but if the variable $array held this array
Array
(
    [0] => Array
        (
            [0] => userlocation
            [1] => 19
        )
 
    [1] => Array
        (
            [0] => credentials
            [1] =>
        )
 
    [2] => Array
        (
            [0] => specialties
            [1] =>
        )
 
    [3] => Array
        (
            [0] => usercity
            [1] => place1
        )
 
)
then after doing this...
    $array[0] = array_merge($array[0], $array[3]);
    unset($array[3]);
It would hold the array you wanted.

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 7:35 pm
by unemployment
That does work, but it only works as a static example. Any nice way to make that dynamic?

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 7:42 pm
by jacek
how are you getting this information ?

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 7:52 pm
by unemployment
jacek wrote:how are you getting this information ?
So far I think I fixed it. I just had to use...
$information = array();

foreach ($actions as $k => $action)
{
	$information[$action] = array($action, $action_details[$k]);
}

if(array_key_exists('userlocation', $information))
{
	$information['userlocation'] = array_merge($information['userlocation'], $information['usercity']);
	unset($information['usercity']);
}

Hopefully this will work for what I need. Thanks

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 7:57 pm
by jacek
That looks like it would work ;) good good.

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 8:00 pm
by unemployment
jacek wrote:That looks like it would work ;) good good.
I should probably check to see if both array keys exist right? You should try to merge them if they both don't exist.

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 9:31 pm
by jacek
unemployment wrote: I should probably check to see if both array keys exist right?
If there is a chance that they might not exist, yeah :)

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 10:20 pm
by unemployment
jacek wrote:
unemployment wrote: I should probably check to see if both array keys exist right?
If there is a chance that they might not exist, yeah :)
Fixed WOOT... only took 4 days of battling. :(

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 10:28 pm
by jacek
4 days of battling then one post on this forum... there is a slogan in there somewhere,

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 10:46 pm
by unemployment
jacek wrote:4 days of battling then one post on this forum... there is a slogan in there somewhere,
Too funny

Re: News Feed Actions Issue

Posted: Wed May 18, 2011 10:53 pm
by Temor
jacek wrote:4 days of battling then one post on this forum... there is a slogan in there somewhere,
Haha, thats almost as good as the one you have now :)