public function follow(Request $request){
$response = array();
$response['code'] = 400;
$following_user_id = $request->input('following');
$follower_user_id = $request->input('follower');
$element = $request->input('element');
$size = $request->input('size');
$following = User::find($following_user_id);
$follower = User::find($follower_user_id);
if ($following && $follower && ($following_user_id == Auth::id() || $follower_user_id == Auth::id())){
$relation = UserFollowing::where('following_user_id', $following_user_id)->where('follower_user_id', $follower_user_id)->get()->first();
if ($relation){
if ($relation->delete()){
$response['code'] = 200;
if ($following->isPrivate()) {
$response['refresh'] = 1;
}
}
}else{
$relation = new UserFollowing();
$relation->following_user_id = $following_user_id;
$relation->follower_user_id = $follower_user_id;
if ($following->isPrivate()){
$relation->allow = 0;
}else{
$relation->allow = 1;
}
if ($relation->save()){
$response['code'] = 200;
$response['refresh'] = 0;
if ($following && $follower){
$relationz = new UserRelationship();
$relationz->main_user_id = $following_user_id;
$relationz->relation_type = 1;
$relationz->related_user_id = $follower_user_id;
$relationz->allow = 1;
$relationz->save();
}
}
}
if ($response['code'] == 200){
$response['button'] = sHelper::followButton($following_user_id, $follower_user_id, $element, $size);
}
}
return Response::json($response);
}
Hey guys, I have this code which creates a follow among two users.
I added a function to be personal friends 'relationz' as well.
Currently when you follow a user, you become 'relationz' automatically..
I would like to create a new 'relationz' only when the follower is also followed by the same person, my question is what change must I make here to either:
a) stop an auto-friend when only one user follows..
b) detect when each person follows each other..
I'm not sure which is the better logic?
I was wrongly thinking the simple "if (follower && following)" was enough, maybe it is just in the wrong place?
Thanks for any help!
How is your relations set up?
since i see a whole lot of code and if the follow function is in you controller, that will be a mess to sort out later when bug fixing.
so you have a many to many relation ship called followers in the user model?
so if user a would follow user b user a would give a follower result of 1
and if user b would have followers than it would be 0 correct?
so what i would do, if both are following each other you should have 2 rows with both users in the pivot table.
so I would create a new relationship relationz with a wherehas query in it
public function relationz {
return $this->belongsToMany(Follower::class)->whereHas('followers' , function($query) {
$query->where('followed_id', auth()->user_id);
})
}
Something like that.
Related
Currently I'm using Mysql and CodeIgniters MVC framework to fill in my data. The table logs every status change that has been made and when it was made. This is what the database currently looks like:
I've added new columns in the table called status_from and status_to, where I want these columns to take the substring from action column.
But now how do I display it in my database with my following code:
Controller class:
public function status($status){
$statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
if($this->input->post('id')){
foreach($this->input->post('id') as $key => $id):
$check = $this->listings_model->loadlisting_check($id);
$log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
$this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log)));
$data=array('status'=>$status);
if($status == 'T' || $status == 'Y' || $status == 'S'){
$pub = 1;
}else{
$pub =0;
}
$this->listings_model->lpupdate(array('property_publish'=>$pub),$id);
endforeach;
}
return true;
}
listings_model:
function loadlisting_check($id)
{
$this->db->select("refno, status, archive");
$id=$this->db->escape_str($id);
$cond=array("$this->table_name.$this->primary_key"=>$id);
$this->db->where($cond);
$this->db->from($this->table_name);
$query = $this->db->get();
return $query->row();
}
logs_model:
public function insert_log($log)
{
$log['agent_id'] = $this->session->userdata('clientsessuserid');
$log['logtime'] = date('Y-m-d H:i:s');
$this->db->insert($this->table_name, $log);
return true;
}
Basically I want to fill my status_from column with $statusar[$check->status] and status_to column with $statusar[$status] when every new entry is made
You can pass the variables you want to add in the array in insert_log method parameter with proper key matched with column name in your table
public function status($status){
$statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
if($this->input->post('id')){
foreach($this->input->post('id') as $key => $id):
$check = $this->listings_model->loadlisting_check($id);
$log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
$this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log) , 'status_from'=>$statusar[$check->status] ,'status_to'=>$statusar[$status]));
$data=array('status'=>$status);
if($status == 'T' || $status == 'Y' || $status == 'S'){
$pub = 1;
}else{
$pub =0;
}
$this->listings_model->lpupdate(array('property_publish'=>$pub),$id);
endforeach;
}
return true;
}
And you will not need to add anything else to your model
I am making a quiz module in which there is only one user for now. So if the new user plays a quiz then the user will be added to the database and if the existed user is playing the quiz the we just have to update the coins
I have made a logic but it doesn't works for me
public function story1_home_coin(Request $request)
{
$coins = $request->input('coins');
$user_id = Auth::user()->id;
$activity_users = activity_users::all();
$found = false;
foreach($activity_users as $activity_user){
if($user_id == $sctivity_user->user_id)
{
$found= true;
}
else{
$found = false;
}
}
if($found = true){
activity_users::where([
['user_id', '=', $user_id],
['activity_id', '=', 1]
])->update(['coins' => $coins]);
}
else{
$newactivity_users = new activity_users;
$newactivity_users->userid = $user_id;
$newactivity_users->activity_id = 1;
$newactivity_users->coins = $coins;
$newactivity_users->save();
}
}
Your logic should be like this:
public function story1_home_coin(Request $request)
{
$coins = $request->input('coins');
$user_id = Auth::user()->id;
$activity_users = activity_users::where('user_id',$user_id);
if($activity_users->count()){
activity_users::where([
['user_id', '=', $user_id],
['activity_id', '=', 1]
])->update(['coins' => $coins]);
}else{
$newactivity_users = new activity_users;
$newactivity_users->userid = $user_id;
$newactivity_users->activity_id = 1;
$newactivity_users->coins = $coins;
$newactivity_users->save();
}
}
In your logic, you are checking the user by looping on to the data after fetching all the data, while you should only fetch the related user.
There are two problems in your code that lead to an unexpected behaviour.
First, in your foreach-loop, you're not dropping out of the loop when you have found a matching user_id, hence you're (almost) always setting $found to false.
For example, if you're looking for user_id 3, you'd go through your users 1-5.
user_id == 1 -> $found = false
user_id == 2 -> $found = false
user_id == 3 -> $found = true
user_id == 4 -> $found = false
user_id == 5 -> $found = false
This can be resolved by using a break; in your loop, like this:
foreach($activity_users as $activity_user){
if($user_id == $sctivity_user->user_id)
{
$found= true;
break;
}
}
Second problem is the line where you check the $found variable. You're actually not checking the variable, you're setting the variable:
if($found = true){
compared to this
if($found == true){
From my application, I send a list of users that I want to search the group they are subscribed:
$selectedUsers = ["John", "Carlos", "Anna", "Julia"]
I have in my database many different groups with many different users in
each of them:
$football = ["**John**" ,"**Carlos**" ,"Daniel" ,"Rob" ,"Frank" ,"Bob"]
$cooking = ["**John**" , "**Anna**" , "**Julia**" , "Claudia" , "Rob" , "Adriana"]
$startups = ["**John**", "**Carlos**", "**Anna**", "**Julia**", "Rob", "Adriana"]
The output I want to have is the sorted list of groups with the amount of the selectedUsers in it:
$returnArray[0] = $startups //4 users inside group
$returnArray[1] = $cooking //3 users inside group
$returnArray[2] = $football //2 users inside group
Here is the code I have so far, but the loop I'm using is based on the group_id I've stored and I want to change that:
<?php
//fetch groups with users
$returnValue = array();
$groupUsersNumber = 0;
$selectedUsers = htmlentities($_REQUEST["selectedUsers"]);
$lastGroupID = htmlentities($_REQUEST["lastGroupID"]); //remove
if($lastGroupID == ""){
$lastGroupID = getLastGroupID();
$lastGroupID = $lastGroupID + 1;
}
if($selectedUsers == ""){
//return all groups ordered by ID desc
$group = getGroupWithID($lastGroupID);
} else{
$usersArray = explode(', ', $selectedUsers);
$foundGroup = false;
while($foundGroup == false){
$group = getGroupWithID($lastGroupID);
$fetchedGroupUsers = explode(', ', $group["users"]);
for($i = 0; $i < count($usersArray); $i++){
if(in_array($usersArray[$i], $fetchedGroupUsers)){
$foundGroup = true;
break;
} else{
$lastGroupID = $group["group_id"];
}
}
}
}
for($i = 0; $i < count($usersArray); $i++){
if(in_array($usersArray[$i], $fetchedGroupUsers)){
$groupUsersNumber = $groupUsersNumber + 1;
}
}
if(empty($group))
{
$returnValue["status"]="403";
$returnValue["message"]="No more groups with that users.";
echo json_encode($returnValue);
return;
} else{
$returnValue=$group;
$returnValue["groupUsersNumber"]=$groupUsersNumber;
}
echo json_encode($returnValue);
?>
Is there any other way to have a better/ more efficient way to search into my database? Appreciated!
It seems that your database is not normalized. A normalized database may be the more efficient way. Do not store users in a describing varchar. Instead establish a many to many relation.
Beside that the FilterIterator class of PHP is something for you. It is reusable and a bit more efficient at iterating over arrays.
Here 's a short example.
class NameFilterIterator extends FilterIterator {
protected $filter = null;
public function __construct(Iterator $iterator, $filter) {
parent::__construct($iterator);
$this->filter = $filter;
}
public function accept() {
$current = $this->getInnerIterator()->current();
if (strpos($current, $this->filter) !== false) {
return true;
}
return false;
}
}
// Usage
$aUsers = [ 'John', 'Carlos', 'Anna', 'Julia' ];
$oFootball = new ArrayIterator(["**John**" ,"**Carlos**" ,"Daniel" ,"Rob" ,"Frank" ,"Bob"]);
foreach ($aUsers as $sUser) {
$oFilter = new NameFilterIterator($oFootball, $sUser);
foreach ($oFilter as $sName) {
var_dump($sName); // outputs: John, Carlos
}
}
The internal memory usage of the FilterIterator object is way more efficient.
You appear to be mixing up php and mysql, and it would be better to redesign your database.
However as a basic idea you can do roughly what you want in MySQL. It is not nice, and not efficient but something like this:-
SELECT a.group_description ,
GROUP_CONCAT(b.wanted_name)
FROM some_table a
LEFT OUTER JOIN
(
SELECT "John" AS wanted_name UNION SELECT "Carlos" UNION SELECT "Anna" UNION SELECT "Julia"
) b
ON FIND_IN_SET(b.wanted_name, a.group_users)
GROUP BY a. group_description
Got a strange problem here on my little project, I have an account area that people can set a location so we can suggest job openings within the area they live in.
EDIT: Code indented - Sorry
The locations that do not work are "Maidstone" & "Medway". These locations are all stored in the database, When another user adds a job they can set the location the job is in and that will then add that job to the database under "State" String.
Other Locations such as Canterbury do work and the jobs that are listed under the Canterbury location show up on the users account as they should.
This php works for every location other then 2 of them which is really strange.
<?php
class SJB_Classifieds_SuggestedJobs extends SJB_Function
{
public function execute()
{
$tp = SJB_System::getTemplateProcessor();
$count_listing = SJB_Request::getVar('count_listing', 10, null, 'int');
$current_user = SJB_UserManager::getCurrentUser();
$properties = $current_user->getProperties();
foreach ($properties as $property) {
if ($property->getType() == 'location') {
$fields = $property->type->child;
$childProperties = $fields->getProperties();
foreach ($childProperties as $childProperty) {
if (in_array($childProperty->getID(), array('State'))) {
$value = $childProperty->getValue();
switch ($childProperty->getType()) {
case 'list':
if ($childProperty->getID() == 'State') {
$displayAS = $childProperty->display_as;
$displayAS = $displayAS?$displayAS:'State';
$listValues = SJB_StatesManager::getStatesNamesByCountry(false, true, $displayAS);
}
else
$listValues = $childProperty->type->list_values;
foreach ($listValues as $values) {
if ($value == $values['id'])
$phrase[strtolower($childProperty->getID())] = $values['caption'];
}
break;
default:
$phrase[strtolower($childProperty->getID())] = $value;
break;
}
}
}
}
}
$phrase = array_diff($phrase, array(''));
$phrase = implode(" ", $phrase);
$listing_type_id = "Job";
$request['action'] = 'search';
$request['listing_type']['equal'] = $listing_type_id;
$request['default_listings_per_page'] = $count_listing;
$request['default_sorting_field'] = "activation_date";
$request['default_sorting_order'] = "DESC";
$request['keywords']['relevance'] = $phrase;
$searchResultsTP = new SJB_SearchResultsTP($request, $listing_type_id, array('field'=>'keywords', 'value'=>$phrase));
$tp = $searchResultsTP->getChargedTemplateProcessor();
$tp->display('suggested_jobs.tpl');
}
}
Not sure if my problem is due to the PHP that tells which jobs to display.
Could someone just skip though the PHP and let me know if anythings wrong? I really want to work this out on my own however I'm alittle lost and if i can find out that the PHP is fine then I can figure it out some where else :)
Thanks!
Since I have several functions executing in the following control as a single transaction I couldn't surround each function as a transaction in the model. So I did it the following way. Please someone let me know if there is any problem. Works fine for now, but have no idea whether it will get any concurrency issues or there is any other way?
if(isset($_POST['btnsave']))
{
$mcodes = $_POST['tblmcode'];
$count = count($mcodes);
//echo $count;
$issue = new Materialissue_model();
$this->db->trans_start(); //Here starts my transaction
$issue->setIssuecode($this->input->post('txtissuecode'));
if($issue->checkNoExistence()) {
$issue->setDate($this->input->post('txtdate'));
$issue->setCustomer($this->input->post('txtcustomer'));
$issue->setFromlocation($this->input->post('txtlocation'));
$issue->setResponsible($this->input->post('txtresponsible'));
$issue->setComments($this->input->post('txtcomments'));
$issue->setTotal($this->input->post('txttotal'));
$issue->setUser($this->session->userdata('username'));
$issue->setStatus($this->input->post('txtstatus'));
for ($i = 0; $i < $count; $i++) {
$issue->setMaterialcode($_POST['tblmcode'][$i]);
$issue->setMaterialname($_POST['tblmname'][$i]);
$issue->setCost($_POST['tblcost'][$i]);
$issue->setQty($_POST['tblqty'][$i]);
$issue->setSubtotal($_POST['tblsubtotal'][$i]);
$issue->saveIssueDetail();
$stock = new Materialstock_model();
$stock->setItemcode($_POST['tblmcode'][$i]);
$stock->setItemlocation($this->input->post('txtlocation'));
$stock->setQty($_POST['tblqty'][$i]);
$stock->setRefno($this->input->post('txtissuecode'));
$stock->setLasttransaction('MATERIAL-ISSUE');
$stock->updateMaterialIssueStock();
$transaction = new Transaction_model();
$transaction->setDescription("MATERIAL-ISSUE");
$transaction->setItemcode($_POST['tblmcode'][$i]);
$transaction->setRecqty("0");
$transaction->setTransqty("0");
$transaction->setIssueqty($_POST['tblqty'][$i]);
$transaction->setDate($this->input->post('txtdate'));
$transaction->setUser($this->session->userdata('username'));
$transaction->saveMaterialTransaction();
}
$result = $issue->saveIssue();
$this->db->trans_complete(); //Here ends my transaction
if ($result) {
$message = new Message_model();
$data['message'] = $message->recordadded;
$data['type'] = "success";
$data['returnpage'] = base_url() . "index.php/materialissue_control/show";
$data["print"] = base_url() . "index.php/Notegenerator_control/showMaterialIssueNote?code=".$issue->getIssuecode();
$this->load->view('messageprint_view', $data);
}
}else{
$message = new Message_model();
$data['message'] = $message->issuecodeexists;
$data['type'] = "error";
$data['returnpage'] = base_url() . "index.php/materialissue_control/show";
$this->load->view('message_view', $data);
}
}
I prefer like using trigger to handle many functions in one controller, this make mycode clean and easy to track. example:
user writes article, this action will call one action in model write_article combine with 1 transaction, but this function run any query :
1.insert post
2.lock count post category
3.lock count user post
4.lock count post by date
example in code
public function write_article($post) {
$this->cms->db->trans_start(TRUE);
$this->cms->db->set('content', $posts->get_content());
$this->cms->db->insert('t_posts');
$this->cms->db->trans_complete();
if($this->cms->db->trans_status() === TRUE){
$this->cms->db->trans_commit();
}else{
$this->cms->db->trans_rollback();
}
}
This reference about trigger
www.sitepoint.com/how-to-create-mysql-triggers