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;
}
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;
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();
I am trying to get all categories with related parent ID with a recursive function.
If i print that array its showing me all data but if i want to return this is not working. I am sure i am doing some mistake.
My code for recursive function is:
public function get_child($id,$level=0){
//$level=0;
$this->db->select('*');
$this->db->from('whole_category');
$this->db->where('parent_id',$id);
$this->db->order_by("id", "ASC");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$res= $query->result();
echo $current_node_id=$res[0]->id;
$level++;
$this->get_child($current_node_id,$level);
$cat["level".$level][] =$res;
return $cat;
}else{
return $cat;
}
}
Calling it like:
public function getcategories(){
echo "<pre>";
$a=$this->get_child(1);
var_dump($a);
}
I had created a Recursive function for a Multi level Marketing Project
Please try it.
public function getSubMembers($memberID, $tableName = 'member_master') {
$data = arra[enter image description here][1]y();
$strQuery = "select MEMBER_ID,LOGIN_NAME,FIRSTNAME,LASTNAME,SPONSOR,PARENT_ID,PAYMENT_STATUS from member_master where SPONSOR='" . $memberID . "' order by POSITION ";
$result = mysqli_query($this->conn, $strQuery);
if (mysqli_field_count($this->conn)) {
while ($row = mysqli_fetch_assoc($result)) {
$data[$row['LOGIN_NAME']] = $row;
foreach ($data as $values) {
$data[$values['LOGIN_NAME']]['SUB'] = $this->getSubMembers($values['LOGIN_NAME']);
}
}
}
return $data;
}
Trying to get result from database by calling stored procedure in code-igniter. Below is my controller & model. Model is working fine getting result from stored procedure and can show the result when I do print_r() in model. But I am not able to get the result of query in controller method. Not able to understand the error here.
Controller:
function weeklysalesstatus()
{
$this->data['rpt'] =$this->reports_model->weeklysalesstatus(true);
$this->data['page_title'] ='Weekly Sales Status [ Residential ]';
print_r("here...");
$this->page_construct('reports/weeklysalesstatus', $this->data);
}
Model:
public function weeklysalesstatus($sender = false)
{
$sql = 'select * from region';
$sql1 = 'CALL GetWeeklySalesStatus()';
$query = $this->db->query($sql1);
if(count($query) > 0) {
foreach (($query->result()) as $row) {
$data[] = $row;
//var_dump($data);
}
return $data;
} else {
print_r("no rows");
}
return $data;
}
The issue is somewhere in this line : $this->data[$rpt] = $this->reports_model->weeklysalesstatus();
But can't figure it out.
Thanks
EDIT:
These errors arise only when I call Stored Procedure CALL. For other queries it work perfectly. So is there any settings I need to do on Codeigniter DB class for calling a Stored procedure thru active record?
$q = $this->db->query('CALL GetWeeklySalesStatus');
Change this line to
$q = $this->db->query('CALL GetWeeklySalesStatus')->result();
In Model
public function weeklysalesstatus()
{
//$sql = 'select * from region';
$sql1 = 'CALL GetWeeklySalesStatus()';
$query = $this->db->query($sql1);
if(!empty($query))
{
$data = $query->result_array();
return $data;
}
else
{
return FALSE;
}
}
In Controller
function weeklysalesstatus()
{
$rpt =$this->reports_model->weeklysalesstatus();
if ($rpt == FALSE) {
echo "Empty";
} else {
$data['rpt'] = $rpt;
}
$data['page_title'] ='Weekly Sales Status [ Residential ]';
$this->page_construct('reports/weeklysalesstatus', $data);
}
Modify the controller like the following hope it works
function weeklysalesstatus()
{
$this->data['rpt'] =$this->reports_model->weeklysalesstatus();
$this->data['page_title'] ='Weekly Sales Status';
$this->page_construct('reports/weeklysalesstatus', $this->data);
}
your model
public function weeklysalesstatus($sender = false)
{
$sql = 'select * from region';
$sql1 = 'CALL GetWeeklySalesStatus()';
$query = $this->db->query($sql1);
$data["result"] = array();
if(count($query) > 0) {
foreach (($query->result()) as $row) {
$data["result"][] = $row;
//var_dump($data);
}
}
return $data;
}
your controller
function weeklysalesstatus()
{
$data['rpt'] =$this->reports_model->weeklysalesstatus(true);
$data['page_title'] ='Weekly Sales Status [ Residential ]';
print_r("here...");
$this->page_construct('reports/weeklysalesstatus', $data);
}
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();
}