I want to select all the rows that emp id's are match to my session userdata (emp_id). Here's my code. I get so many errors and no row has been selected. Someone help me please. Thanks
Model:
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->row_array();
}
Controller:
public function show() {
$emp_id = $this->session->userdata('emp_id');
$data['save'] = $this->user_model->get_save_samp($emp_id);
$this->load->view('show',$data);
}
Views:
<?php foreach ($save as $row) { ?>
<td style="width: " ><?php echo $row->emp_id ?></td>
<td style="width: " ><?php echo $row->emp_code?></td>
<td style="width: " ><?php echo $row->emp_name ?></td>
<?php }>
Try This
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->result();
}
you can try solution for your problem :
Changes your modal function :
Modal.php
public function get_save_samp($emp_id) {
$this->db->select("*");
$this->db->where('id', $emp_id);
$this->db->get('tblsavesample');
return $query->row();
}
Views:
<?php foreach ($save as $row) {?>
<td style="width: " ><?php echo $row->emp_id ?></td>
<td style="width: " ><?php echo $row->emp_code ?></td>
<td style="width: " ><?php echo $row->emp_name ?></td>
<?php } ?>
row_array() returns the first row only. If you want all the records to be returned, use result_array() instead.
$result = $query->result_array();
return $result;
Link : click
Change your Modal Code like this :
public function get_save_samp($emp_id) {
//$query = $this->db->get_where('tblsavesample', array('emp_id' =>$emp_id));
$this->db->select("*");
$this->db->where_in('id', $emp_id);
$query = $this->db->get('tblsavesample');
return $query->result();
}
And Change your Controller code like this to check what data are we getting inside controller.
public function show()
{
$emp_id = $this->session->userdata('emp_id');
$data['save'] = $this->user_model->get_save_samp($emp_id);
echo "<pre>";
print_R($data['save']);
exit();
$this->load->view('show', $data);
}
row_array() used to return only single record. change your code in model as follows :
public function get_save_samp($emp_id) {
$query = $this->db->get_where('tblsavesample', array('emp_id' =>
$emp_id));
return $query->result_array();
}
and check number of records returned in your controller as follows:
$data['save'] = $this->user_model->get_save_samp($emp_id);
echo $data['save']->num_rows();
if this displays 0, then you have no matching records in your database table.
to test the query, try to execute the same query in mysql
Related
Im fairly new with php so please bear with me. I have two tables in my database, users and onlineform tables. The onlineform table have userid field as a foreign key for the id field in users table. Im trying to display data from onlineform table based on current user login. The problem is, it display data from all users and not the one who logged in. I have setup a query in my form_model but i dont know if the query is correct
this is the model
form_model.php:
function getEmployees()
{
$this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1,
onlineform.out1, onlineform.in2, onlineform.out2");
$this->db->from('users');
$this->db->join('onlineform','users.id = onlineform.userid', 'left');
$this->db->order_by('date asc');
$query = $this->db->get();
return $query->result();
}
this is the controller
form.php
public function table()
{
if($this->session->userdata('username') != '')
{
$this->load->model('form_model');
$query = $this->form_model->getEmployees();
$data['EMPLOYEES'] = null;
if($query) {
$data['EMPLOYEES'] = $query;
}
}
else
{
redirect(base_url() . 'index.php/form/login');
}
$this->load->view('table.php', $data);
}
and this is my view
table.php
<?php
foreach($EMPLOYEES as $employee)
{?>
<tr>
<td align="center"
<?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
<strong><?=$employee->day;?></strong>
</td>
<td align="center"
<?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
<strong><?=$employee->date;?></strong>
</td>
<td><?=$employee->out1;?></td>
<td><?=$employee->in1;?></td>
<td><?=$employee->out2;?></td>
<td><?=$employee->in2;?></td>
</tr>
<?php }?>
You can pass the current logged in userId into the model through getEmployees($userid)
$query = $this->form_model->getEmployees($userid);
And then use it in your query to get current userId that matches logged in user, to user in database;
function getEmployees($userid)
{
$this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1,
onlineform.out1, onlineform.in2, onlineform.out2");
$this->db->from('users');
$this->db->where('onlineform','users.id',$userid); //add this line
$this->db->join('onlineform','users.id = onlineform.userid', 'left');
$this->db->order_by('date asc');
$query = $this->db->get();
return $query->result();
}
And then pass it normally to your view.
Hope this helps.
You can save the current logged in userid in session then you can use it when you retrieve the data from database.
change your form_model.php code to this
function getEmployees()
{
$this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1,
onlineform.out1, onlineform.in2, onlineform.out2");
$this->db->from('users');
$this->db->join('onlineform','users.id = onlineform.userid', 'left');
$this->db->where("users.id", $this->session->userdata('current_user_id'));
$this->db->order_by('date asc');
$query = $this->db->get();
return $query->result();
}
I would like to create HTML table with my query results. The HTML table should look like this:
I don't really understand about Codeigniter's HTML Table Class, so I just create a loop for each column of the table on my view like this:
<tr>
<?php foreach ($nameloop as $row) { ?> <td><?php echo $row->username; ?> </td><?php } ?>
<?php foreach ($addressloop as $row) { ?> <td><?php echo $row->useraddress; ?> </td><?php } ?>
<?php foreach ($ageloop as $row) { ?> <td><?php echo $row->userage; ?></td> <?php } ?>
<?php foreach ($sexloop as $row) { ?> <td><?php echo $row->usersex; ?></td> <?php } ?>
</tr>
Turns out it was a very bad decision. It makes my table look horizontally instead vertically like it used to.
Here's my model:
<?php class Front_model extends CI_Model {
public function __construct()
{
parent::__construct();
$CI = &get_instance();
$this->db2 = $CI->load->database('db2', TRUE);
}
function namelist()
{
$query = $this->db->query("SELECT name as uername FROM table1 ORDER BY name");
return $query->result();
}
function addresslist()
{
$query = $this->db->query("SELECT address as useraddress FROM table1 INNER JOIN name
ON address.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
function agelist()
{
$query = $this->db->query("SELECT age as userage FROM table1 INNER JOIN name
ON age.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
function sexlist()
{
$query = $this->db->query("SELECT sex as usersex FROM table1 INNER JOIN name
ON sex.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
}
And then here's my function on my CONTROLLER:
public function index()
{
$data['nameloop'] = $this->front_model->namelist();
$data['addressloop'] = $this->front_model->addresslist();
$data['ageloop'] = $this->front_model->agelist();
$data['sexloop'] = $this->front_model->sexlist();
$this->load->view('home',$data);
}
This make me quite confused. Whether it's the HTML tag or I code it wrong.. I don't know...
Thank you for your help, guys...
You can really do this in a simpler way.
Your model can be simplified to get all the data with one method
<?php class Front_model extends CI_Model {
public function __construct()
{
parent::__construct();
$CI = &get_instance();
$this->db2 = $CI->load->database('db2', TRUE);
}
function userInformation()
{
$query = $this->db->query("SELECT name as username, address as useraddress, age as userage, sex as usersex FROM table1 INNER JOIN name ON table1.id = name.id GROUP BY id ORDER BY name");
return $query->result();
}
}
And then in your controller you can just call this one method instead
public function index()
{
$data['information'] = $this->front_model->userInformation();
$this->load->view('home',$data);
}
And finally in your view, you can just your one foreach
<tr>
<?php foreach ($information as $row) { ?>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->useraddress; ?></td>
<td><?php echo $row->userage; ?></td>
<td><?php echo $row->usersex; ?></td>
<?php } ?>
</tr>
Hope that helped.
Although I recommend using CI's table library!
UPDATE
If you want to work with your existing queries, then I recommend you modify your controller like this -
public function index()
{
$names = $this->front_model->namelist();
$addresses = $this->front_model->addresslist();
$ages = $this->front_model->agelist();
$sexes = $this->front_model->sexlist();
$length = sizeOf($names);
$data['information'] = [];
for ($i = 0; $i <= $length; $i++) {
$data['information'][$i] = [$names[$i], $addresses[$i], $ages[$i], ];
}
$this->load->view('home',$data);
}
Update your view like this -
<tr>
<?php foreach ($information as $info) { ?>
<td><?php echo $info[0]->username; ?></td>
<td><?php echo $info[1]->useraddress; ?></td>
<td><?php echo $info[2]->userage; ?></td>
<td><?php echo $info[3]->usersex; ?></td>
<?php } ?>
</tr>
I have two tables One for users and the other for project listing
My problem is that i want to display each project(from project_table)and email belonging to user who listed the project(from user_table) on a single row
The project_table has a row for user_id(i.e id from user_table that identifies the user who posted the project)
here's my view(project_view):
I this case im displaying data from project_table but i want to display email for a particular user from user_table
<?php
foreach($query as $row)
{
?>
<p> <?echo $row->pro_name ?></p>
<p> <?echo $row->duration ?></p>
<p> <?echo $row->budget ?></p>
<p>User email will be displayed here</p>
<?
}
my model:
function get_projects()
{
$query = $this->db->get('project_table');
return $query->result();
}
my controller:
function show_projects()
{
$data['query']=$this->project_model->get_projects();
$this->load->view('project_view', $data);
}
Any ideas on how to implement this will be much appreciated
You can use joined query in your model
function get_projects()
{
$query =$this->db->select('p.*,u.email')
->from('project_table p')
->join('user_table u','p.user_id = u.id')
->get();
return $query->result();
}
And in your view you can do so
<?php
foreach($query as $row)
{
?>
<p> <?php echo $row->pro_name; ?></p>
<p> <?php echo $row->duration; ?></p>
<p> <?php echo $row->budget; ?></p>
<p><?php echo $row->email;?></p>
<?php
}
?>
I would rewrite controller and model function to allow for a user parameter:
function get_projects($user=null)
{
if ($user == null){
$query = $this->db->get('project_table');
}else{
$query = $this>db->get('project_table')->where('user_id',$user);
}
return $query->result();
}
function show_projects($user)
{
$data['query']=$this->project_model->get_projects($user);
$this->load->view('project_view', $data);
}
Now you can call your controller with a user parameter
http://..../show_user/1
1st approach is JOINs
$this->db->select('project_table.* as project, user.id as user_id, user.email as email')
->from('project_table')
->join('user', 'project.id = user_id');
$result = $this->db->get();
2nd not recommended
function get_projects()
{
$query = $this->db->get('project_table');
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
function get_email($id = FALSE) {
if ($id === FALSE) return FALSE;
$query = $this->db->get_where('user', array('user_id' => $id));
return ($query->num_rows() > 0) ? $query->result() : FALSE;
}
somewhere in controller...
if (!$projects = $this->model->get_projects()) {
foreach ($projects as $key => $project) {
# code...
$projects[$key]->user_email = $this->model->get_email($project->user_id);
}
//projects is now with user_email field
$this->load->view()...
} else {
//no data recieved
redirect(); //redirect to default_controller
}
Try join this -
1) Model
function get_projects()
{
$this->db->select('*');
$this->db->from('project_table');
$this->db->join('user_table', 'project_table.user_id = user_table.id');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
}
2) Controller
$data['user_project'] = $this->project_model->get_projects();
$this->load->view($view, $data);
3) View
foreach($user_project as $row)
{
echo "<p>" .$row->pro_name. "</p>";
echo "<p>" .$row->duration. "</p>";
echo "<p>" .$row->budget. "</p>";
echo "<p>" .$row->email. "</p>";
}
I am creating a search function that will allow a user to search for a house in my database by postcode initially. The function can be seen below, when the function is executed and finds a true statement I get no errors however when I execute the search and I get a no fields been returned I am left with this error:
No Records Found
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/undergradpad/search.php on line 26
I want it to display No Records Found however I don't know how I should correct the above error.
search.php:
<table width="500" border="1" cellpadding="5">
<tr>
<th width="16" scope="row">id</th>
<td width="95">bedrooms</td>
<td width="140">description</td>
<td width="104">roadname</td>
<td width="71">postcode</td>
</tr>
<?php
require("classes/class.House.inc");
$obj = new House();
$obj->search($_POST['term']);
foreach($obj->data as $val){
extract($val);
?>
<tr>
<td scope="row"><?php echo $id; ?></td>
<td><?php echo $bedrooms; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $roadname; ?></td>
<td><?php echo $postcode; ?></td>
</tr>
<?php
}
?>
</table>
classes/class.House.inc:
<?php
include("connect/class.Database.inc");
class House extends Database {
public function read(){
$query = "SELECT * FROM houses";
$result = $this->mysqli->query($query);
$num_result = $result->num_rows;
if($num_result > 0){
while($rows =$result->fetch_assoc()){
$this->data[]=$rows;
//print_r($rows);
}
return $this->data;
}
}
public function search ($term){
$query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
$result = $this->mysqli->query($query);
$num_result = $result->num_rows;
if($num_result > 0){
while($rows =$result->fetch_assoc()){
$this->data[]=$rows;
//print_r($rows);
}
return $this->data;
} else{
echo 'No Records Found';
}
}
}
?>
in this variable ($obj->data) you just get null data.
First check if not empty and than use foreach and don't have error if yout method don't return null data
just check if (!empty($obj->data)
{
foreach code
}
$obj is a House object. It has no $data property, even if you use it. The search method sets this property, but only if records are found. If no records are found, the method echoes a value.
I would change it like this: Instead of echoing an error, make the method return false:
public function search ($term){
$query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
$result = $this->mysqli->query($query);
$data = false;
$num_result = $result->num_rows;
while($row =$result->fetch_assoc()){
$data[]=$row;
}
return $data;
}
Now, the function return an array of false if there is no data. You can now use it like this:
$obj = new House();
if ($data = $obj->search($_POST['term']))
{
foreach($obj->data as $val){
extract($val);
}
}
The changes I made:
- No longer set data as a property, since you also return it. You can still do that, if you like, but I think it's confusing to do both.
- Return false if there's no data.
- Change the variable rows to row, since it only contains one row.
if(is_array($obj->data)){
foreach code
}
else{
no record
}
i am using codeigniter. i have a table to be displayed in a view file. so i have a model where i fire a query to get the data from table.
function my_active_requests()
{
$user_id = $this->session->userdata('user_id');
$this->db->select('id,request_date,required_by_date');
$this->db->where('requested_by',$user_id);
return $this->db->get('requests');
///also tried with
//$query = $this->db->get('requests');
///$number_of_rows = $query->num_rows;
//return $query;
//return $number_of_rows;
//but no result
}
this is the model function.
this is my controller
function my_active_req()
{
$this->bloodline_model->my_active_requests();
//also tried without this//
$query = $this->db->get('requests');
//and this//
$number_of_rows = $query->num_rows;
$this->load->view('my_active_req');
}
and this is my view
<?php foreach ($query->result() as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->request_date; ?></td>
<td><?php echo $row->required_by_date; } ?></td>
</tr>
</table>
but the error is undefined variable $query.
i think , i am not able to return the $query and other data from my model to controller and controller to my view.
Please provide me with the solution.
`
Change this line:
<?php
$this->load->view('my_active_req', array(
'query' => $query
));
The second parameter of the view() loader function allows you to pass variables into the view.
However, you're largely circumventing the purpose of MVC separation. You want the controller to "ask" for information from the model (not just straight from the database; otherwise, what's the point of a model?), and then "pass" it to the view. The controller knows what the view needs to function.
Model
function my_active_requests()
{
$user_id = $this->session->userdata('user_id');
$this->db->select('id,request_date,required_by_date');
$this->db->where('requested_by',$user_id);
$query = $this->db->get('requests');
if($query->num_rows) {
return $this->db->result();
}
return false;
}
Controller
function my_active_req()
{
$results = $this->bloodline_model->my_active_requests();
$this->load->view('my_active_req', array('user_data' => $results));
}
View
<?php
if($user_data) {
foreach ($user_data as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->request_date; ?></td>
<td><?php echo $row->required_by_date; } ?></td>
</tr>
</table>