News Feed Actions Issue

Ask about a PHP problem here.
Post Reply
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

News Feed Actions Issue

Post 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?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Unsolvable? Probably Not

Post 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.
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Unsolvable? Probably Not

Post 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?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: News Feed Actions Issue

Post 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.
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post 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.
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: News Feed Actions Issue

Post by jacek »

If "current output...2 is in $array
$array[0] = array_merge($array[0], $array[1]);
unset($array[1]);
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: News Feed Actions Issue

Post 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.
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post by unemployment »

That does work, but it only works as a static example. Any nice way to make that dynamic?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: News Feed Actions Issue

Post by jacek »

how are you getting this information ?
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: News Feed Actions Issue

Post by jacek »

That looks like it would work ;) good good.
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: News Feed Actions Issue

Post 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 :)
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post 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. :(
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: News Feed Actions Issue

Post by jacek »

4 days of battling then one post on this forum... there is a slogan in there somewhere,
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: News Feed Actions Issue

Post by unemployment »

jacek wrote:4 days of battling then one post on this forum... there is a slogan in there somewhere,
Too funny
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: News Feed Actions Issue

Post 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 :)
Post Reply