join 2 table in one function - php

i want to join the data of 2 tables in 1 table and display the data of 2 tables. I already have function that load the one table. can someone modify this function code to get the 2 tables ? im just a beginner in php.
Model_users.php
public function getUserGroup($userId = null)
{
if($userId) {
$sql = "SELECT * FROM user_group WHERE user_id = ?";
$query = $this->db->query($sql, array($userId));
$result = $query->row_array();
$group_id = $result['group_id'];
$g_sql = "SELECT * FROM groups WHERE id = ?";
$g_query = $this->db->query($g_sql, array($group_id));
$q_result = $g_query->row_array();
return $q_result;
}
}
Controller User.php
public function index()
{
if(!in_array('viewUser', $this->permission)) {
redirect('dashboard', 'refresh');
}
$user_data = $this->model_users->getUserData();
$result = array();
foreach ($user_data as $k => $v) {
$result[$k]['user_info'] = $v;
$group = $this->model_users->getUserGroup($v['id']);
$result[$k]['user_group'] = $group;
}
$this->data['user_data'] = $result;
$this->render_template('users/index', $this->data);
}

Here you go
$this->db->select("ug.*,g.*");
$this->db->from("user_group ug");
$this->db->join('groups g', 'ug.user_id = g.group_id');
$this->db->where("ug.user_id",1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result();
} else {
$result = null;
}
return $result;

Related

get column with the same value on the database using codeigniter

Here are my resources first
As you can see on my database I have a Referal_Code and Referral_Link
Now what I am trying to here is I want to get all those Referral_Link that has the same value on the Referal_Code
I know I am no where near to what I am trying to do but I am trying my best really to get this done.
Here what I have tried so far
On my model
public function get_same_referral_code()
{
$this->db->select('referal_code');
$this->db->where('referal_code', 'PoVsw0Epne');
$query = $this->db->get('investor');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
and on my controller
public function get_all()
{
$data = array();
$data['title'] = "Rewards";
$data["data"] = $this->investor->get_all_investor();
$data["get_all_flevel"] = $this->investor->get_all_first_level();
$data["referrals"] = $this->investor->get_same_referral_code();
$this->template->load('default_layout','contents','rewards',$data);
}
and I am calling it on my view like this
<?php
echo ($referrals[0][id]);
echo ($referrals[0][firstname]);
echo ($referrals[0][referal_code]);
echo ($referrals[0][referral_link]);
?>
What my expected result should be like this
Could someone help me .
EDIT
I've tried again this way to get what I want .
First I tried to get all the referal_code
public function get_same_referral_code()
{
$this->db->select('referal_code');
$query = $this->db->get('investor');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
and now I will going to use this method get_same_referral_code() to check if
referral_link has the same value on the referal_code
public function get_same_referral_link()
{
$this->db->select('referral_link');
$this->db->where('referral_link','get_same_referral_code();');
$query = $this->db->get('investor');
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
but the problem is that I got this error now
Message: Invalid argument supplied for foreach()
i think sub query will help you
$SQL= "SELECT * FROM tableName WHERE referal_code EXISTS (SELECT DISTINCT Referral_Link FROM tableName)";
$query = $this->db->query($SQL);
return $query->result_array();
or
$SQL= "SELECT * FROM investor WHERE referral_link IN (SELECT DISTINCT referal_code FROM investor)";
$query = $this->db->query($SQL);
return $query->result_array();

After fetching a value from DB then assign it to a variable, then i can't use it to the next query

This is my model:
public function GetStudentData()
{
$sID = $this->input->get('id');
$class_id = $this->db->query("SELECT student.class_id FROM student WHERE student.sID = '$sID'");
$query = $this->db->query("SELECT * FROM student WHERE(sID='$id'AND class_id = '$class_id')");
if($query->num_rows() > 0)
{
return $query->row();
}else{
return false;
}
}
How can I use $class_id in the next query?
you could do it in one query... (from your example [SIC])
public function GetCSData() {
$id = $this->input->get('id');
$query = $this->db->query("SELECT student.*
FROM student
WHERE student.sID = '$id'
AND student.class_id = (SELECT class_id
FROM student
WHERE student.sID = '$id')");
if($query->num_rows() > 0){
return $query->row();
}else{
return false;
}
}//..GetCSData end
looking at it though, that will give the same result as
SELECT *
FROM student
WHERE sID = '$id'
maybe I am missing a point?

php with framework of codeigniter

function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = mysql_query("select * from sent_invitations where user_id='$user_id'");
if (mysql_num_rows($qry) > 0)
{
while ($row = mysql_fetch_array($qry))
{
$mobile_number = $row['mobile_number'];
$qry1 = mysql_query("select mobile_number from users where mobile_number = '$mobile_number'");
$row1 = mysql_fetch_array($qry1);
$mobile_number = $row1['mobile_number'];
$users[] = array('mobile_number' => $mobile_number );
return $users;
}
}
}
The above is my coding.
In sent_invitation table there are 3 rows with user_id=11.
from there i m trying to get mobile_number of all three rows based on user_id.
the mobile_number of all 3 rows is different.
after geting mobile_number from sent_notification table.
i m comparing the mobile_number with mobile_number of users table.
i m seeing that 3 of them mobile_numbers are there in users table.
i m trying to show all of them.
bt problem is that my code id showing only one row and that is of last mobile number from 3
Since you are using Codeigniter You should follow the MVC pattern :
So the code in the controller should be :
public function invitation_result(){
$userId = 0;
if(($_GET['user_id']) && is_numeric($_GET['user_id']) && $_GET['user_id'] > 0){
$userId = $_GET['user_id'];
}
$data = $this->User_model->getDataFromID($userId);
return $data;
}
And the code for Model should be :
public function getDataFromID($userId){
$arrReturn = array();
if(($userId) && is_numeric($userId) && $userId > 0){
$this->db->select('*');//You can put the required fields here like : name,mobile_number...
$this->db->from('sent_invitations');
$this->db->where("user_id",$user_id);
$query = $this->db->get();
$result = $query->result_array();
if(!empty($result)){
foreach($result as $key=>$value){
$this->db->select('*');
$this->db->from('mobile_number');
$this->db->where("mobile_number",$value['mobile_number']);
$querySub = $this->db->get();
$resultSub = $querySub->result_array();
if(!empty($resultSub)){
array_push($arrReturn,$resultSub);
}
}
return $arrReturn;
}
}else{
return $arrReturn;
}
}
By the time I have written the Answer you would have solved the error ,but this answer will help future Users.
You should use while loop where you are getting mobile_number in inner loop.
function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = $this->db->query("select * from sent_invitations where user_id='$user_id'");
if ($qry->num_rows() > 0)
{
while ($qry->result_array() as $row)
{
$mobile_number = $row['mobile_number'];
$qry1 = $this->db->query("select mobile_number from users where mobile_number = '$mobile_number'");
if($qry1->num_rows() > 0){
while($qry1->result_array() as $row1){
$mobile_number = $row1['mobile_number'];
$users[] = array('mobile_number' => $mobile_number );
}
return $users;
}
}
}
}
You should use codeigniter database query instead of mysql custom query. because mysql is deprecated. You can use below code:
function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = mysqli_query("select * from sent_invitations where user_id='$user_id'");
if (mysql_num_rows($qry) > 0)
{
while ($row = mysql_fetch_array($qry))
{
$mobile_number = $row['mobile_number'];
$qry1 = mysql_query("select mobile_number from users where mobile_number = '$mobile_number'");
$row1 = mysql_fetch_array($qry1);
$mobile_number = $row1['mobile_number'];
$users[] = array('mobile_number' => $mobile_number );
return $users;
}
}
}
first, can you simplified the query something like this ?
$sql = "select a.*, b.mobile_number as user_mobile_number
from sent_invitations a
join users b on a.user_id = b.user_id";
or
$sql = "select a.*, b.mobile_number as user_mobile_number
from sent_invitations a
join users b on a.mobile_number = b.mobile_number";
it's depend on how this two tables can be related.
Now you can see if the sent_invitations.mobile_number also found in users.mobile_number just in one query. If this returns multiple same rows, then put distinct keyword after SELECT
second, back to your code, i think you should move return $users out form while block
while ($row = mysql_fetch_array($qry)) {
// your code
}
return $users;
good luck;
function invitation_result()
{
$this->load->database();
$user_id = $_GET['user_id'];
$qry = $this->db->query("select * from sent_invitations where user_id='$user_id'");
$results = $qry->result();
foreach ($results as $result)
{
$mobile_number = $result->mobile_number;
$qry1 = $this->db->query("select mobile_number from users where mobile_number = '$mobile_number'");
$results1 = $qry1->result();
foreach ($results1 as $result1)
{
$row1 = mysql_fetch_array($qry1);
$mobile_number = $result1->mobile_number;
$users = array('mobile_number' => $mobile_number );
return $users;
}
}
}

search function using framework codeigniter, php

my problem is i whenever i input fname and mname i got an error from the database. i just want to search both of fname and mname in search field.
my search controller function
public function search()
{
$li = $this->session->userdata('logged_in');
$id = $this->session->userdata('idnumber');
if($li == TRUE)
{
$this->load->model('users_model');
$this->load->helper('smiley');
$this->load->library('table');
$image_array = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array = $this->table->make_columns($image_array, 8);
$image_array2 = get_clickable_smileys('http://localhost/efg/images/smileys', 'status');
$col_array2 = $this->table->make_columns($image_array2, 8);
$this->data['smiley_table1'] = $this->table->generate($col_array);
$this->data['smiley_tables'] = $this->table->generate($col_array2);
if($this->input->post())
{
$search = $this->input->post('search');
$memb = $this->users_model->search($search);
$usersearch = $this->users_model->search($search);
$grpsearch = $this->users_model->searchgrp($search);
redirect ('home/profile/'.$memb);
}
}
}
My users_model model
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
if($hehe==NULL)
{
echo $error; exit;
}
else
{
return $memb;
}
}
Always check the varibles using in condition are declared before the condition.
If it doesnt pass the condition what would be the output
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = null;
foreach ($query->result_array() as $row)
{
$memb = $row['idnumber'];
}
$error = 'There is no record for the one you searched. Please go Back.';
$query1 = $this->db->query("SELECT * FROM users WHERE idnumber =$memb");
$hehe = $query1->result_array();
if($hehe==NULL)
{
return $error;
}
else
{
return $memb;
}
}
Make sure the variables that youll be using in queries are set. Trap before executing queries.
Also, use Query Builder (Active Record) when you can.
public function search($search)
{
$this->db->select('*');
$this->db->from('users');
$this->db->like('username',$search);
$this->db->or_like('fname',$search);
$this->db->or_like('lname',$search);
$this->db->or_like('mname',$search);
$query = $this->db->get();
$memb = '';
if ($query->result_array() > 0) {
foreach ($query->result_array() as $row) {
$memb = $row['idnumber'];
}
if ($memb) {
$this->db->select("*");
$this->db->where("idnumber", $memb);
$query1 = $this->db->get("users");
//$query1 = $this->db->query("SELECT * FROM users WHERE idnumber ='$memb'");
$hehe = $query1->result_array();
return ($hehe) ? $memb : false;
}
return false;
}
$error = 'There is no record for the one you searched. Please go Back.';
return $error;
}

push data in an array using codeigniter php

I have user_sport_tbl,sport_tbl tables. in user_sport_tbl i have sport_id but not have sport name.Sport name is in sport_tbl.
i have a function of getUserSport() of Model.
my query :-
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
i want sport_name for every sport_id and want to merge sport_name in array.
my code :-
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if($query->num_rows > 0)
{
foreach($query->result() as $item)
{
$query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
$sql1 = $query1->row();
$data[] = array_push($item, $sql1->sport_name);
}
return $data;
}
}
my present return data:-
[user_match_id] => 7 [sport_id] => 2 [match_id] => 3 [username] => admin
i want to add [sport_name] => cricket.
I want to return an array with sportname
Try this coding
public function getUserSport()
{
$data = array();
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if($query->num_rows > 0)
{
foreach($query->result() as $key=>$item)
{
$query1 = $this->db->query("select sport_name from sport_tbl where sport_id=$item->sport_name order by sport_id limit 1");
$sql1 = $query1->row();
$item[$key]['sport_name'] = $sql1->sport_name;
$data[] = $item;
}
return $data;
}
}
Try This
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->get_where('user_sport_tbl',array('username' => $username));
if(empty($query))
{
foreach($query->result_array() as $item)
{
$sportsId = $item[0]['sport_name'];
$query1 = $this->db->query("SELECT sport_name FROM sport_tbl WHERE sport_id = $sportsId ORDER BY sport_id LIMIT 1");
$result = $query1->result_array();
$data[] = array_push($item, $result[0]['sport_name']);
}
return $data;
}
}
Or can do with join query too (Not tested. If any error fix it.
or post the message her)
public function getUserSport()
{
$username = $this->session->userdata('username');
$query = $this->db->query(
"SELECT user_sport_tbl.sport_name, sport_tbl.sport_name
FROM user_sport_tbl
INNER JOIN sport_tbl
ON user_sport_tbl.username = sport_tbl.sport_name
WHERE user_sport_tbl.username = '$username' ORDER BY sport_tbl.sport_id LIMIT 1");
$result = $query->result_array();
return $data;
}

Categories