I have a simple query but I am trying to use $this->uri->segment(); in WHERE clause
I could not get it working.
Any suggestions would be greatly appreciated.
In my controller I have:
public function profile()
{
$var = (int) $this->uri->segment(3);
$query = $this->db->query('SELECT * FROM faculty WHERE id = $var;');
$data['name'] = '';
$data['section'] = "Profile";
$data['query'] = $query;
$this->load->view('view1', $data);
}
PHP won't evaluate variables inside string literals (' ').
Change
'SELECT * FROM faculty WHERE id = $var;'
to
"SELECT * FROM faculty WHERE id = $var;"
Related
This is my model
public function total_late()
{
$query = "SELECT sum(late_deduction) as late_deduction FROM tbl_dtr";
$result = $this->db->query($query);
return $result->row()->late_deduction;
}
This the image of the database:
This is the image of my table:
This is my another model for my table in view
function get_faculty_payroll($limit, $start, $string = "", $orderField, $orderDirection)
{
$this->db->group_start()
->where('tbl_employee' . '.department_id', 3)
->group_end()
->group_start()
->or_like('first_name', $string)
->or_like('last_name', $string)
->group_end()
->join('tbl_employee_department', 'tbl_employee.department_id = tbl_employee_department.department_id')
->join('tbl_rates', 'tbl_employee.ID_number = tbl_rates.ID_number')
->limit($limit, $start)
->order_by($orderField, $orderDirection);
$query = $this->db->get('tbl_employee');
return $query->result();
}
This is my table:
Your Controller
public function total_late()
{
$retRes=$this->modelname->total_late($ID_number);
}
Your model
public function total_late($ID_number)
{
$query = "SELECT sum(late_deduction) as late_deduction
FROM tbl_dtr WHERE ID_number = '$ID_number'";
$result = $this->db->query($query);
return $result->row()->late_deduction;
}
However, this code is better, as it handles parameters safely, and avoids SQL injection vulnerabilities:
$stmt = $this->db->prepare("
SELECT sum(late_deduction) as late_deduction
FROM tbl_dtr WHERE ID_number = ?
");
$stmt->execute(array($ID_number));
Try This,
public function total_late()
{
$query = "SELECT sum(late_deduction) as late_deduction FROM tbl_dtr where ID_number = 'C-08011'";
$result = $this->db->query($query);
return $result->row()->late_deduction;
}
Hi guys I tried to use the variable equation on Codeigniter as in the code I wrote below, Is my code writing correct? and here's my code so far I wanna using variable $amount in my controller
In Controller
$amount = $this->m_data->getTotalSales->$data['TOTAL_SALES'];
and this in Model
//GET TOTAL REVENUE
function getTotalSales(){
$this->db->select("(SELECT SUM(grand_total) FROM sales_order WHERE member = ".$this->session->userdata('ID').") - (SELECT SUM(amount) FROM payment WHERE member_id = ".$this->session->userdata('ID').") AS total_sales");
$query = $this->db->get();
if ($query->num_rows() >0){
foreach ($query->result() as $data) {
$hasilSemua[] = $data;
}
return $hasilSemua;
}
}
$amount = $this->m_data->getTotalSales();
function getTotalSales(){
$result1 = $this->db->select_sum("grand_total")
->from('sales_order')
->where("member = $this->session->userdata('ID')");
->get();
$result2 = $this->db->select_sum("amount")
->from('payment')
->where("member_id = $this->session->userdata('ID')")
->get();
return $result1->row()-$result2->row();
}
}
I believe this is what you are after. Note that I pass the UserID in as the second parameter to the WHERE function. This is in case there is any chance of the user having inputted it (always safter to do this anyway).
//GET TOTAL REVENUE
function getTotalSales(){
$db = $this->db;
$db->select('SUM(grand_total)');
$db->where('member',$this->session->userdata('ID'));
$q1 = $db->get_compiled_select('sales_order');
$db->select('SUM(amount)');
$db->where('member_id',$this->session->userdata('ID'));
$q2 = $db->get_compiled_select('payment');
$query = $db->query("SELECT ($q1) - ($q2) as total_sales");
$data = $query->row_array();
return $data['total_sales'];
}
You can then call $amount = $this->m_data->getTotalSales; to get your result.
I have a selet function where I pass two variables. The table and where.
However, whatever I put for where it always displays id = 1.
Could this be because it is recognising that id has a value and setting it to 1 or am I completely off the mark here?
The function is below:
public function select($table, $where){
$sql = "SELECT * FROM $table WHERE $where";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 1){
return $this->processRowSet($result, true);
}
return $this->processRowSet($result);
}
The call to the function is:
$rowSet = $db->select('users','username = wayne');
Or is it the way I am setting username in the parameter?
username is a String then it is necessary to write
$rowSet = $db->select('users','username = "wayne"');
I am trying to select the details of the users from a table in mysql database but it is not working. This is the code in my model :-
public function getuserdetails()
{
$user_email = $this->input->post('email');
$query_userdetails = $this->db->query("SELECT *
FROM users WHERE email = '$user_email' ");
return $query_userdetails->result_array();
}
This is not working. But if I put the actual email id in the query instead of $user_email it works but not properly i.e if I use :-
$query_userdetails = $this->db->query("SELECT * FROM users WHERE
email = 'myemail#gmail.com' ");
In this case it returns a result. My controller code to accept the result is :-
$data['details'] = $this->model_userdetails->getuserdetails();
But the problem is that when I access $details in view :-
echo $details['name']."<br />";
it does not recognize 'name'. name is the field in the database where the name of the users are stored. But if I try to retrieve it in a foreach loop it is working :-
foreach($details as $udetails)
{
echo $udetails['name']."<br />";
}
Run queries as per codeigniter suggestion
$query_userdetails = $this->db->query("SELECT * FROM users WHERE email = ?", array($user_email));
http://ellislab.com/codeigniter/user-guide/database/queries.html search for Query Bindings
I would try:
$query_userdetails = $this->db->query("SELECT * FROM users WHERE email = '". $user_email ."'");
I have solved the part where only foreach loop would work.
If we use row_array instead of returning result_array() the foreach constraint to diplay goes away.
Now I want to select the name from the database where email is $user_email
You have to write like this
public function getuserdetails()
{
$user_email = $this->input->post('email');
$this->db->where('email', $user_email)
->get('users')
->result_array();
}
After this, in view, you must to write like this
foreach($details as $udetails)
{
echo $udetails['name']."<br />";
}
You can try like This:
[CONTROLLER]
$data = array();
$data['udetails'] = $this->UserModel->getuserdetails();
$this->load->view('welcome_message',$data);
[MODEL]
public function getuserdetails() {
$user_email = 'webmaster';
$userdetails = $this->db->query("SELECT * FROM users WHERE umail = '$user_email' ");
return $userdetails->result();
}
[VIEW]
<?php
foreach($udetails as $row)
{
echo($row->uname.'<br/>');
echo($row->uname);
}
?>
Try this please it will help you.
Model
public function getuserdetails()
{
$user_email = $this->input->post('email');
$this->db->select("*");
$this->db->from('users');
$this->db->where('email',$user_email);
$query = $this->db->get();
$result= $query->result();
}
Controller
$data['details'] = $this->model_userdetails->getuserdetails();
view
foreach($details as $detail)
{
echo $detail->name;
echo $detail->email;
}
I'm attempting to modify a date field from my database before it gets outputted to the view, but I'm not having much luck. This code doesn't seem to work, what am I doing wrong?
function get_journal_entry($id)
{
$sql = 'SELECT * FROM journal WHERE user_id = '.$this->tank_auth->get_user_id().' AND id = '.$id;
$query = $this->db->query($sql);
$query['created'] = date("c", strtotime($query['created']));
return $query->row_array();
}
$this->db->query returns a query object, not your results. You need to modify the row after calling $query->row_array.
$query = $this->db->query($sql);
$result = $query->row_array();
$result['created'] = date("c", strtotime($result['created']));
return $result;
Another version of the code, which may work:
function get_journal_entry($id)
{
$sql = 'SELECT * FROM journal WHERE user_id = ' . intval($this->tank_auth->get_user_id()) . ' AND id = ' . intval($id);
$row = $this->db->query($sql)->row_array();
$row['created'] = date("c", strtotime($row['created']));
return $row;
}