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;
}
Related
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;
public function get_inner_refs($referral){
$query = $this->db->query("SELECT username FROM users WHERE binery_referral='$referral'");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$referral = $row['username'];
$referrals[] = $row['username'];
$this->get_inner_refs($referral, $referrals);
}
}
return $referrals;
}
here i want to return an array of all users but it returns only first value from database,
Try to create a private variable to assign to and create a get method:
private $referrals;
public function get_inner_refs($referral)
{
$query = $this->db->query("SELECT username FROM users WHERE binery_referral='$referral'");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$username = $row['username'];
$this->referrals[] = $username;
$this->get_inner_refs($username);
}
}
return $this;
}
public function getReferrals()
{
return $this->referrals;
}
Then to use:
$referrals = $ReferralClass->get_inner_refs($username)->getReferrals();
i think you can edit like this
public function get_inner_refs($referral='',$referrals = []){
$query = $this->db->query("SELECT username FROM users WHERE binery_referral='$referral'");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$referral = $row['username'];
$referrals[] = $row['username'];
$this->get_inner_refs($referral, $referrals);
}
}
return $referrals;
}
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?
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;
}
}
}
Here is my code.... and my doubts along with the code
See in the function toltalRetailerComm($userId) ... I am fetching the value of
$shoppeId
and
$storeId
and this same variable can be used in the next function retailerDailyComm($userId, $fromDate, $toDate) by using in this manner function retailerDailyComm($shoppeId,$storeId, $fromDate, $toDate)
function toltalRetailerComm($userId) {
$sql = "SELECT shoppe_id FROM atm_super_shoppe WHERE user_id='$userId'";
$shoppe_query = $this->db->query($sql);
$sql = "SELECT shoppe_id FROM atm_store WHERE user_id='$userId'";
$store_query = $this->db->query($sql);
if ($shoppe_query->num_rows() > 0) {
$result = $shoppe_query->row();
$shoppeId = $result->shoppe_id;
$sql = "SELECT COALESCE(sum(commission),0) as commission FROM atm_shoppe_commission WHERE shoppe_id ='$shoppeId'";
//$sql = "SELECT commission FROM atm_shoppe_commission WHERE shoppe_id='$shoppeId ' BETWEEN '03-5-2011' AND '05-5-2011'";
$query = $this->db->query($sql);
print_r($shoppeId);
if ($query->num_rows > 0) {
$result = $query->row();
$commission = $result->commission;
return $commission;
}
} else
if ($store_query->num_rows() > 0) {
$result = $store_query->row();
$storeId = $result->shoppe_id;
$sql = "SELECT COALESCE(sum(commission),0) as commission FROM atm_store_commission WHERE shoppe_id ='$storeId'";
//$sql = "SELECT commission FROM atm_store_commission WHERE shoppe_id='$storeId ' BETWEEN '03-5-2011' AND '05-5-2011'";
$query = $this->db->query($sql);
print_r($storeId);
if ($query->num_rows > 0) {
$result = $query->row();
$commission = $result->commission;
return $commission;
}
}
}
/**********TOTAL RETAILER COMMISSION ENDS*****************************/
/**********TOTAL RETAILER START TO END DATE COMMISSION STARTS*****************/
function retailerDailyComm($userId, $fromDate, $toDate) {
$sql = "SELECT shoppe_id FROM atm_super_shoppe WHERE user_id='$userId'";
$shoppe_query = $this->db->query($sql);
$sql = "SELECT shoppe_id FROM atm_store WHERE user_id='$userId'";
$store_query = $this->db->query($sql);
if ($shoppe_query->num_rows() > 0) {
$result = $shoppe_query->row();
$shoppeId = $result->shoppe_id;
$sql = "SELECT commission as retailDailyCommission FROM atm_shoppe_commission WHERE shoppe_id='$shoppeId ' BETWEEN '$fromDate' AND '$toDate'";
$query = $this->db->query($sql);
if ($query->num_rows > 0) {
$result = $query->row();
$retailDailyCommission = $result->retailDailyCommission;
return $retailDailyCommission;
} else
if ($store_query->num_rows() > 0) {
$result = $store_query->row();
$storeId = $result->shoppe_id;
$sql = "SELECT commission as retailDailyCommission FROM atm_store_commission WHERE shoppe_id='$storeId' BETWEEN '$fromDate' AND '$toDate'";
$query = $this->db->query($sql);
if ($query->num_rows > 0) {
$result = $query->row();
$retailDailyCommission = $result->retailDailyCommission;
return $retailDailyCommission;
}
}
}
}
You're inside a class, change your variables and it should work.
$this->shoppeId
$this->storeId