codeigniter join query issue in email - php

My Codeigniter join query is not work. In a function just try to match RC code in users table and get email, this function is properly work, next target is match email id from article table and get article its work properly, but ill try to join the users table and article table beacause i need to get the users firstname & lastname from users table, I don't know is that right way or not, check my code below. Controller Part :
public function user_article()
{
$rc=$_GET['rc'];
$data['title'] = "User Article";
if ($this->session->userdata ('is_logged_in')){
$data['profile']=$this->model_users->profilefetch();
$data['results']=$this->article_m->u_article($_GET);
$this->load->view('sd/header',$data);
$this->load->view('sd/user_article', $data);
$this->load->view('sd/footer', $data);
}
else {
}
}
My Model :
function u_article($rc)
{
$query=$this->db->select('email')->where('rc',$rc['rc'])->get('users');
$result=$query->result_array();
if ($query->num_rows() > 0) {
$row = $query->row_array();
$array = array ('email' => $row['email'], 'a.status' => '1');
$query1=$this->db->select('a.id,title,a.status,description,image,a.email,tags,postdate,firstname,lastname,rc')->join('users u','u.email = a.email','left')->where($array)->get('articles a');
if ($query1->num_rows() > 0) {
foreach ($query1->result() as $row) {
$data[] = $row;
}
return $data;
}
else {return NULL;}
} else {return NULL;}
}
check my code and tell me my mistakes Thanks in advance.
Error Shoe in View :
A Database Error Occurred
Error Number: 1052
Column 'email' in where clause is ambiguous
SELECT `a`.`id`, `title`, `a`.`status`, `description`, `image`, `a`.`email`, `tags`, `postdate`, `firstname`, `lastname`, `rc` FROM (`articles` a) LEFT JOIN `users` u ON `u`.`email` = `a`.`email` WHERE `email` = 'admin#gmail.com' AND `a`.`status` = '1'
Filename: F:\wamp\www\project\system\database\DB_driver.php
Line Number: 331

Refactored your code. Just copy-paste and check its working.
Controller:
public function user_article()
{
$rc = $this->input->get('rc');
$data['title'] = "User Article";
if ($this->session->userdata ('is_logged_in'))
{
$data['profile']=$this->model_users->profilefetch();
$data['results']=$this->article_m->u_article($rc);
$this->load->view('sd/header',$data);
$this->load->view('sd/user_article', $data);
$this->load->view('sd/footer', $data);
}
else
{
}
}
Model:
function u_article($rc)
{
$query = $this->db->select('email')->where('rc',$rc)->get('users');
$result = $query->result_array();
if ($query->num_rows() > 0)
{
$row = $query->row_array();
$array = array ('u.email' => $row['email'], 'a.status' => '1');
$query1 = $this->db->select('a.id,a.title,a.status,a.description,a.image,a.email,a.tags,a.postdate,u.firstname,u.lastname,u.rc')->join('users u','u.email = a.email','left')->where($array)->get('articles a');
if ($query1->num_rows() > 0)
{
foreach ($query1->result() as $row)
{
$data[] = $row;
}
return $data;
}
else {return NULL;}
} else {return NULL;}
}

Related

join 2 table in one function

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;

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();

how to using SQL INSERT INTO SELECT with codeigniter

How to use the SQL insert into select in CodeIgniter.
Model...
public function history($book_id)
{
$query = $this->db->query('INSERT orders (book_id, title)
SELECT book_id, book_title
FROM books
WHERE book_id = \'$book_id\'');
return true;
}
First of get all books from books table then INSERT order according to your $book_id
Example:
public function history($book_id)
{
$this->db->select('book_id, book_title');
$this->db->from('books');
$this->db->where('book_id', $book_id);
$query = $this->db->get();
if ( $query->num_rows() > 0 ) // if result found
{
$row = $query->result_array(); // get result in an array format
$data = array();
foreach($row as $values){
$data = array(
'book_id' => $values['book_id'],
'title' => $values['book_title']
);
$this->db->insert('orders', $data); // insert in order table
}
return true;
}
else{
return false;
}
}

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;
}

Can't return data from table - Codeigniter

The below query is only returning one result, even though there are multiple entries in the table with user_is 2. I m not sure what I m doing wrong.
function get_payments($user_id)
{
$sql = "SELECT txn_id, payer_email FROM orders WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
//echo $this->db->last_query();
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
return $data;
}
}else{
return false;
}
}
This is what $this->db->last_query(); returns. SELECT txn_id, payer_email FROM orders WHERE user_id = '2'
Controller:
$profile_data['user_payments'] = $this->profile_model->get_payments($user_id);
View:
<?php
//var_dump($user_payments);
foreach($user_payments as $payment)
{
echo '<p>';
echo $payment->txn_id;
echo $payment->payer_email;
echo '</p>';
}
?>
try this:
<?php
function get_payments($user_id){
$this->db->select('txn_id, payer_email')
->from('orders')
->where('user_id', $user_id);
$qry = $this->db->get();
$res = $qry->result();
if($res){
return $res;
}else{
return false;
}
}
?>
there is a slight mistake here
foreach($query->result() as $payment)
{
$data[] = $payment;
return $data; // this line returns the data and it stops execution in first loop itself
}
it should be like
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
}
return $data;
}
You should return $data from model after foreach loop completes.
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
}
return $data;
}
else
{
return false;
}
Simple CI way:
...
$query = $this->db->query($sql, $user_id);
return $query->result_array();
No looping needed, nothing.
And I see you're using the object form, in that case do this:
return $query->result();
Then just loop it in your view like you're already doing.
So this is the whole model method:
function get_payments($user_id) {
$sql = "SELECT txn_id, payer_email FROM orders WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
return $query->result();
}
See the relevant docs here: http://ellislab.com/codeigniter/user-guide/database/results.html
And using Active Records (the most "natural" to CI):
function get_payments($user_id) {
return $this->db
->select('txn_id, payer_email')
->where('user_id', $user_id)
->get('orders')
->result();
}

Categories