I have different communities in my database, such as: community_newsfeed, community_marketplace, community_games_requested, community_games_offered, events and custom_posts.
Each of those communities stores a post_id, as well as the user_id.
Then I have a community_users table that stores user_id and community_id.
I want to create a mechanism which I, as the authenticated user, can view all the posts of a user, as long as that user and I belong to the same community.
And on that regard, I must view only the posts for the communities which we both belong to.
So I do the following:
Get all the community id's of the communities which I(auth user) belong to:
$community_user_member = DB::table('community_users')->whereUserId($user->id)->pluck('community_id')->toArray();
Then I do the same for the user which I want to see the posts:
$requested_user = DB::table('community_users')->whereUserId($user_id)->pluck('community_id')->toArray();
Fine. Now I use array interect to give me all the community_id that we share only:
$members = array_intersect($community_user_member, $requested_user);
Now, this is where I get stuck.
How can I get the communities which the id match the values I get in the $members value(not the key) and the user id matches $user_id( this is passed in the url )?
if(!empty($members)){
$newsfeed = CommunityNewsfeed::whereIn('newsfeed_community_id', $members)->whereUserId($user_id)->get();
}
I am sure that this user has 4 posts from community_newsfeed table, but I only see two posts, and also I dont see the correct posts.
Live example:
User A belongs to community_newsfeed.
User B belongs to community_newsfeed and community_marketplace.
User A must see the community_newsfeed data where user_id is the id of User B.
Updated
Another option I tried was to use the in array function:
$common = [];
foreach ($community_user_member as $key => $member) {
if (in_array($member, $requested_user)) {
$common[] = $member;
}
}
print_r($common);
The end result should be something like this:
$common = [ '4','5','10','20' ];
Because then I would be able to loop through $common array and do whatever I want.
But I get the following error when I refresh the page:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Related
I am using a json api to bring data in a wordpress function, this is for a sports related website and the purpose of this particular call is to create a list of fixtures for the current competition. I need the data to be grouped by match date with the most recent date first, My first step has been to create a new array that groups the data by matchdate as a $key
<?php
$matchLists = json_decode(json_encode($fixture_data), True);
$newlist = array();
foreach ($matchLists as $key => $matchitem) {
if (array_key_exists('matchDate', $matchitem)) {
$newlist[$matchitem['matchDate']][$key] = ($matchitem);
}
}
?>
This returns a new array grouped as I expected - but the problem I am having is how to then use the matchDate converted to datetime to act as the heading for each fixture grouping as now each item in the $newlist contains a matchDate. an example of the $newlist
in attached picture
My first thought was to create a variable the used the 'match date' labels of the newly created arrays but when I tried this It said the label did not exist.
I then thought to try using [] to go down levels of the array and again this did not return the desired results
Can anybody help?
I have made a custom dropdown field in leads module. Its a dynamically fetching users from users table from the leads module as key => value pair.
The field works fine but when in the edit mode (create a new lead)...the value is not getting stored and instead the key is getting stored not value..
I mean like instead of 'James Bond' the id is getting stored ..which is like '7896877'
Now the funny thing is that in the detail view in sugarcrm (leads module) the name is displayed properly as i wanted it to work. ONly in the list view it displays the ID and also in the database its getting stored as KEY i.e the hash ID.
This is the function:
function getUSERS($bean) {
$resultArray = Array();
$query = "select id,(first_name + ' ' + last_name) AS Name from dbo.users ORDER BY first_name ASC";
$resultArray [''] = '';
$result = $bean->db->query($query);
while ($row = $bean->db->fetchByAssoc($result)) {
$resultArray[$row['id']] = $row['Name'];
}
return $resultArray;
}
Dropdowns in Sugar work as key/value pairing, the key is what is stored in the database and Sugar does the appropriate lookup to display the value. Except the list view seems to work differently for dynamic dropdowns.
Instead of building your array as $resultArray[$row['id'] ]= $row['Name'] you could use the username -$resultArray[$row['username'] ]= $row['Name']as usernames have to be unique but will be more meaningful to your users in the list view.
However, is there any reason you're not using a relate field to the Users module? That should solve all your problems without any coding.
We need to extract specific fields, no matter if they have data or not.
If I f.ex. want an putput with only the field with external id : 'logo' i am trying this:
$limit = 10;
$app_id = xxxxxxx;
$items = PodioItem::filter($app_id,array('Limit' => $limit,'external_id' => 'logo'));
Within podio not all the fields are filled in, and the result is that we do not get a return value for field 'logo'.
When we don't get a return from field logo - the array output is not structured in a way so we can grab the data we need.
How do we achieve this ?
Added text
Rough Example on print output:
items[0] = Companyname,Logo,Address,Phone,Country
(has data in Logo)
items[1] = Companyname,Address,Phone,Country,ContactPerson
(no data in Logo)
items[2] = Companyname,Address,Phone,Country
PROBLEMS ARISING
items[2][4] is not existing (but i won't know this)
items[0][2] is Address Field (but i won't know this)
items[1][2] is Phone Field (but i won't know this)
Looking for Company Contact Person [x][4] accross items will end in
[0] = Wrong (country)
[1] = Right data (ContactPerson)
[2] = PHP error
Like in SQL, i would take the coloumn_name_1,coloumn_name_2, etc.. and grab data from only these fields.
PodioItem objects have a collection of item fields. As you've noted only fields that have values are included (since including empty fields is redundant). As you've noted you can't reliably access the fields by their array offset.
What you should do it access it by field_id or external_id. These are the unique identifiers. You can use the field method on the PodioItem object for this purpose:
$items = PodioItem::filter(...);
foreach ($items['items'] as $item) {
// Get field with external_id "logo". You can also pass in field_id
$field = $item->field('logo');
if ($field) {
print "Found a logo field!";
}
}
I have two collections, Group and User. Originally, my Group document contained an array of User references. But I've changed my mapping so that Users now hold a reference to a Group. I am wondering how I can write a query to update all the existing User documents to reference the group that is currently referencing them, then remove any references on the groups.
I didn't notice the language you are using at first - I had to do something very similar in Python so here is my example code:
# I changed my collection names to users/groups hope I mapped them right :)
users = db.users
groups = db.groups
for u in users.find():
grp = groups.find( { "users" : u['userId'] } )
u['groups'] = [ g['groupId'] for g in grp ]
users.save(u)
Basically, for every user, I look in groups to find a list of groups which contain userId in the array users, and I make a list called groups in u and when I save it adds an array of groups to users document.
Should be not hard to do it in PHP
$users = $db->users;
$groups = $db->groups;
// find all users
$ucursor = $users->find();
// iterate through users, find groups which have the user,
// update user with array of groups
foreach ($ucursor as $u) {
$gcursor = $groups->find( ... );
foreach ($gcursor as $g) {
...
}
}
But this seems like a one time operation, you could do it in mongo shell using something similar to:
var users = db.users.find();
while (users.hasNext()) {
u = users.next();
var gs = db.groups.find({"users":u.userId},{"groupId:1}).toArray();
db.users.update({"groupId":groupId},{$set:{"groups":gs}});
}
I have two columns member id and author id
Member A gives a list of author ids in a array format. I need to run a check against my database to see if these author ids are already there in my database, if they are present take the member ids of these authors and link them to the member id of A.
What is the efficient method to do this? Is it advisable to store the author ids to a temporary table and run the check and fetch it. The number of author ids in the array runs to few thousands.
Can you share some sample codes please? I am currently using PHP and MySql.
Update - I get an array of JSON objects with author id and name as follows:
{
"name": "Harold Robinson",
"id": "912065"
},
{
"name": "Gilbert Patten",
"id": "1212140"
},
{
"name": "Leo Tolstoy",
"id": "6012954"
}
I already have a table called Authors against which these data needs to be compared and if the ids match the user id assigned to the author has to be linked to User A.
Table Authors
User id: 109876
id: 912065
FYI, you won't get a useful answer if your question is not clear and with enough detail. This is a though one because the question is not very clear but I will attempt to the best of my understanding of the question. We can then tweak the answer as you clarify the question further.
//Not sure how you are collecting the ids into an array so I will just create one here for show.
$member_authors = array (1, 2, 3);
foreach ($member_authors as $authorID) {
$query = "SELECT author_id FROM authors WHERE author_id ='" . $authorID . "'";
$result = mysql_query($query);
if (mysql_affected_rows() > 0) { //If a value was returned then the author does exist in the database.
//Do whatever you want with the authorID.
}
}
I hope this helps.