I am working on a project based on CodeIgniter and MySQL. I need suggestions in building CodeIgniter query.
What I want to achieve:
I have 3 tables, profile, activity and friends. The structure for these tables is:
profile
user_id | name
activity
activity_id | message | user_id | sharewith | createdtime
friend
id | user1 | user2 | arefriends
Now I want to display all users' posts to everyone's / friends' walls according to who the user shares with. I have only two options for share with, either share with everyone or share with only friends.
For share with everyone I am storing 1 in the sharewith column of activity for the user_id and 2 for share only with friends.
I have built this query so far:
function load_activity()
{
$user_id=$this->session->userdata('user_id');
$this->db->select('*');
$this->db->from('activity');
$this->db->order_by("activity_id", "desc");
$this->db->limit(15);
$this->db->join('profile', 'profile.user_id = activity.user_id');
$query = $this->db->get();
foreach ($query->result() as $row){
$data[]=$row;
}
echo json_encode($data);
}
How can I modify this query to display either all activities which are shared with everyone or shared only with friends?
Thanks in advance.
It seems like the only thing you need is a WHERE clause.
If the user isn't a friend they should only be able to see the 'share with everyone' activity.
function load_activity() {
$user_id=$this->session->userdata('user_id');
$this->db->select('*');
$this->db->from('activity');
$this->db->order_by("activity_id", "desc");
$this->db->limit(15);
$this->db->join('profile', 'profile.user_id = activity.user_id');
if(!IS_FRIEND) {
$this->db->where('sharewith', 1);
}
$query = $this->db->get();
foreach ($query->result() as $row){
$data[]=$row;
}
echo json_encode($data);
}
Related
I've tried to query using laravel eloquent where user is following a specific brand.
The problem: How to query listing of brand and let me know if current login user is following the brand?
got 3 tables:
Brand:
Id | name | logo
User:
Id | name | email | password
Followers:
brand_id | user_id
Now i tried to query all brand and inside of the collection i want to add
is_follow = 1 or is_follow = 0
if the user already follow or 0 if not exists.
I'm using fractal so maybe it can be easier to query. but i don't really get it how to query it out with check the user_id first.
Thanks
*Update
I manage to solve it. But i think its a bad practice.
$user_id = Request::get('user_id');
foreach($brands->followers as $value){
$array[] = $value->user_id;
}
if(in_array($user_id, $array)){
$is_follow = 1;
}
You can check if the authenticated User follows a specific Brand with:
$user = Auth::user();
$exists = $user->brands->contains($brand_id);
You can also do it with a raw query which will be better in terms of performance:
$exists = DB::table('user_brand')
->whereBrandId($brand_id)
->whereUserId(Auth::user()->id)
->count() > 0;
I am trying to display records of a particular job that has already been done by someone else before a new provider sees it. If the status is open, there should not be any information to be displayed as supposedly, no one has made any report about it. If the status is awarded, then necessary data should be displayed. Right now, the information to be shown are viewable. The problem the data is displayed in every job post even it is not the report for such a job.
Example,
Job ID | Title | Description | Subject | Job Status
2 | Math Tutor | I need Math tutor! I need Math tuto... | Mathematics | Open
1 | English Tutor | Edited... | French | Awarded
If I click "Open", I should not be able to see any record because it is still not done. If I click "Awarded", I should see details about the job. Right now, the data is showing properly for JOB ID 1 which was already awarded. However, the same data is shown as well in JOB ID 2.
How do I properly display the data in its proper place? I've been trying everything to do it. I included the JOB ID to be displayed to see if there's something wrong with it. But there's none, it shows JOB ID 1 in both jobs 1 and 2. How do I display it just in job 1 where it belongs?
Here's my code in controller:
public function view_tutors_tutorials()
{
$this->validateRole('provider');
$this->load->model('tutorial_model');
$this->load->model('auth_model');
$data['subject_list'] = $this->array_to_select( $this->tutorial_model->get_all_subjects(), 'id','name');
$my_preference = $this->tutorial_model->get_tutors_tutorials(isset($_GET['subject_id'])?$_GET['subject_id']:'0', isset($_GET['sort_by'])?$_GET['sort_by']:'');
$data['my_preference'] = $my_preference;
$this->load->view('provider/view_tutors_tutorials', $data);
}
and this in my model:
public function get_tutors_tutorials($subject_id = NULL, $sort_by = NULL)
{
//responsible for displaying job contracts for provider user.
$this->db->select('tutorial.status as status, tutorial.client_id as client_id, tutorial.id as tutorial_id, subject.name as name, tutorial.title as title, tutorial.description as description, tutorial.start_date as start_date, tutorial.update_date_time as update_date_time,tutorial_proposal.provider_id as provider_id,provider.first_name as first_name,provider.last_name as last_name,tutorial.contract_status as contract_status,tutorial.provider_feedback as provider_feedback,tutorial.client_notetoself as client_notetoself,tutorial.client_feedback as client_feedback,tutorial.provider_notetoself as provider_notetoself,tutorial.material_used,tutorial.recommendation')->from('tutorial');
$this->db->join('subject', 'subject.id = tutorial.subject_id');
$this->db->join('tutorial_proposal', 'tutorial_proposal.provider_id = tutorial.provider_id');
$this->db->join('provider', 'provider.id = tutorial_proposal.provider_id');
$this->db->where('tutorial.status', 'Awarded');
if ( ! empty($subject_id) )
{
$this->db->where('subject_id', $subject_id);
}
//if there's no sort selection made, the jobs will be sorted from newest to oldest
if ( empty($sort_by))
{
$sort_by = "update_date_time desc";
}
$this->db->order_by($sort_by);
$query = $this->db->get();
return $query->result_array();
}
I look forward to getting any help.
You need to remove where condition for status in you model's query :
$this->db->where('tutorial.status', 'Awarded'); // this should be removed
Also make sure, your subject_id passed properly.
i am trying to create an instant messaging box for users on my site, badically if a user goes onto another users profile they can write in the chat box and send an instant message to the other user.
I'm having trouble getting the mysql to get the results and filter it so that if
user 1 messages user 2 then only user 1 and user 2 can see the conversation.
At the moment though only the logged in user or ".$_SESSION['user_id']." can see the conversation but i want both users to be able to see their conversation between each other.
the other problem is that if user 3 messages user 2 as well as user 1 then user 2 gets all of the conversations from user 1 and 3 but i only want one conversation between each user per box.
my database ptb_chats looks like this:
id | to_user_id | from_user_id | date_added| content |
2 2 1 April 2011 hello
here's my php:
<?php
$chat_set = get_chats();
while ($chat = mysql_fetch_array($chat_set)) { ?>
<?php echo "<div class=\"chat_row\">".$chat['content']."</div>"; ?>
<? } ?>
here's my mysql:
function get_chats() {
global $connection;
global $profile_id;
$query = "SELECT *
FROM ptb_chats, ptb_profiles
WHERE ptb_profiles.user_id = ptb_chats.from_user_id
AND ptb_chats.to_user_id=".$_SESSION['user_id']."
ORDER BY ptb_chats.date_added ASC";
$chat_set = mysql_query($query, $connection);
confirm_query($query, $connection);
return $chat_set;
}
please can someone show me what i need to do to get this to work? thanks
I'm trying to wrap my head around this, but I seem to go in circles. I'm trying to list a users topics one by one, with the quotes belonging to that specific topic underneath. If that makes sense.
I have 3 tables, like so:
[USERS] user_id username
[TOPICS] topic_id user_id topic_name
[QUOTES] quote_id topic_id quote_name
I want to be able to do something like this in my view:
Username: Thomas
Topic 1: Whatever
Quotes: One quote, another quote, and a third quote, all belonging to Topic 1.
Topic 2: Another topic from Thomas
Quotes: Yes indeed, Okay thanks, I love Stack Overflow, These quotes belong to Topic 2.
But I can't get it to work, I've been trying everything, including weird stuff like:
public function get_quotes()
{
$this->db->select('*');
$this->db->from('topics');
$this->db->join('quotes', 'topic_id = quote_id');
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result() as $row) {
$data[] = $row;
}
}
return $data;
}
Is this strange, should I instead try using 'where' instead? Something like:
$this->db->where('user', $user_id);
$this->db->where('topic', $topic_id);
$this->db->where('quote', $quote_id);
I really appreciate any help I can get, or just a finger pointed in the right direction!
Right off the bat I would ask "What is not working?", secondly I would suggest you run the profiler to show you the EXACT SQL being generated, so that you can make a valid assessment of where the ACTIVE QUERY is failing you.
To use the profiler, stick this into your controller:
$this->output->enable_profiler(TRUE);
It will result in a nice output of all DB calls, all POST vars, etc;
Reference here: http://codeigniter.com/user_guide/libraries/output.html
UPDATE
So to fully do what you want, you need a query that returns the following columns:
user_id, username, topic_id, topic_name, quote_id, quote_name
Here is the active query you want (you can also use method chaining if that is clear enough):
$this->db->select('u.user_id, u.username, t.topic_id, t.topic_name, q.quote_id, q.quote_name');
$this->db->from('users u');
$this->db->join('topics t', 't.user_id = u.user_id'); // this joins the user table to topics
$this->db->join('quotes q', 'q.topic_id = t.topic_id'); // this joins the quote table to the topics table
$query = $this->db->get();
Your result set will then be something like:
user_id | username | topic_id | topic_name | quote_id | quote_name
1 |Thomas |1 |Whatever |1 |One quote, anot...
2 |Ryan |4 |Another... |6 |To be or not to...
Once you have that result set, simply loop through the data to output it, and check to see if you have multiple quotes from the same person (say sort by user_id and do a test on the 2nd loop if its the same person, otherwise output the new users name).
If you want all of the quotes for a specific user:
$this->db->join('TOPICS t', 'u.user_id on t.user_id')
->join('QUOTES q', 't.topic_id on q.topic_id')
->where('u.user_id', $userId)
->get('USERS u');
// I always echo my queries when developing to make sure they are what i'm expecting
echo $this->db->last_query();
If you want all of the quotes for all of the users
$this->db->join('TOPICS t', 'u.user_id on t.user_id')
->join('QUOTES q', 't.topic_id on q.topic_id')
->get('USERS u');
echo $this->db->last_query();
Am building a messaging system for a site, but i want it to have an instant messaging app feel.
I have table with a structure like this:
<!-- language: lang-none -->
id
R_id = Reciever's id
S_id = Sender's Id
message
read = 0 if unread, 1 if read
post_time
conv_id = conversation id
I am trying to build a query that retrieves all messages pertaining to a receiving user and display it in a group format i.e all messages from a user grouped with the user's name, sort of like facebook messages.
This is the method in my model, am working with codeigniter
function get_user_conversations($user_id) {
//Load Models
$this->load->model('conversation_model');
//Load helper
$this->load->helper('date');
//database query
$q = $this->db->select('*')
->from('conversations_inbox')
->where('R_id',$user_id)
->group_by('S_id')
->order_by('post_time','desc')
->get();
$conversations = $q->result();
return $conversations;
}
Maybe something like this?
// Presuming you want the most recent ones first
$q = mysql_query("SELECT message FROM `TABLE` WHERE `R_id`='USERNAME_HERE' ORDER BY `post_time` DESC");
while ($row = mysql_fetch_array($q)) {
echo "<li>Message: " . $row['message'];
}
I'm honestly not really sure what you're asking, so that's the best I can do.