Display data to the view - php

I'm having trouble displaying data in my view. I need to display teacher's remark in the view.
I placed this in my controller <pre>';print_r($data);die
and $data['exam_result'] outputs the teacher's remark. However, I'm still having issues displaying it in the view.
Controller:
public function view($id)
{
$data['exam_result'] = $this->examgroupstudent_model->searchStudentExams($student['student_session_id'], true, true);
$this->load->view('layout/header', $data);
$this->load->view('student/studentShow', $data);
$this->load->view('layout/footer', $data);
}
Model:
public function searchStudentExams($student_session_id, $is_active = false, $is_publish = false) {
$inner_sql = "";
if ($is_active) {
$inner_sql = "and exam_group_class_batch_exams.is_active=1 ";
}
if ($is_publish) {
$inner_sql .= "and exam_group_class_batch_exams.is_publish=1 ";
}
$sql = "SELECT exam_group_class_batch_exam_students.*,exam_group_class_batch_exams.exam_group_id,exam_group_class_batch_exams.exam,exam_group_class_batch_exams.date_from,exam_group_class_batch_exams.date_to,exam_group_class_batch_exams.description,exam_groups.name,exam_groups.exam_type FROM `exam_group_class_batch_exam_students` INNER JOIN exam_group_class_batch_exams on exam_group_class_batch_exams.id=exam_group_class_batch_exam_students.exam_group_class_batch_exam_id INNER JOIN exam_groups on exam_groups.id=exam_group_class_batch_exams.exam_group_id WHERE student_session_id=" . $this->db->escape($student_session_id) . $inner_sql . " ORDER BY id asc";
$query = $this->db->query($sql);
$student_exam = $query->result();
if (!empty($student_exam)) {
foreach ($student_exam as $student_exam_key => $student_exam_value) {
$student_exam_value->exam_result = $this->examresult_model->getStudentExamResults($student_exam_value->exam_group_class_batch_exam_id, $student_exam_value->exam_group_id, $student_exam_value->id, $student_exam_value->student_id);
}
}
return $student_exam;
}
View:
Teacher's Remark: <?php echo $student->teacher_remark; ?>;

This is what I did in my view
Teacher's Remark: <?php echo $exam_value->teacher_remark;?>

Related

how to query two tables from database and show the result in a single row using codeigniter

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

Pass mysql_query results to foreach

I used the following code to to get list of users facebook friends and ccheck it against users in an app database. This code would return the users of the app, who are Facebook friends of the user.
$friends_set = '(';
foreach($friends["data"] as $value) {
$friends_set .= $value['id'].',';
}
$new_set = preg_replace('/,$/',')',$friends_set);
$res = mysql_query("SELECT * from user AS u, upload AS up WHERE u.fb_id IN $new_set AND u.fb_id=up.user_id") or die(mysql_error());
while($row = mysql_fetch_array($res)) {
echo $row['fb_id']. "". $row['first_name'];
echo "<br>";
}
$data['top_friends']=$res;
$this->load->view('myview');
This code works. It is in a controller of my codeigniter application and it successfully echos the correct data onto the page.
However now I need to print the result of the query in a for each statement in my view like this:
<?php foreach ($top_friends as $me) : ?>
<div >
<p><?php echo $me['first_name']; ?></p>
<?php endforeach; ?>
However when I try getting the query results in the view using the for each it doesn't work.
How do i fix this?
You could try it the codeignitor way, Create a model function say get_top_friends and i assume that you are passing a comma separated string as argument like $fb_id = '45,65,78,89'. Say facebook_model is the name of the model then :
class Facebook_model extends CI_Model{
//other functions and constrcutor here
//function to get the top friends
function get_top_friends($fb_id){
$fbId = explode(",",$fb_id)
$this->db->select('*');
$this->db->where_in('fb_id',$fbId);
$this->db->order_by('points','desc');
$this->db->limit(10);
$query = $this->db->get('user');
if ($query->num_rows() < 1) return FALSE;
return $query->result_array();
}
}
And make change in your code as below:
$friends_set = '';
foreach($friends["data"] as $value) {
$friends_set .= $value['id'].',';
}
$new_set = preg_replace('/,$/',')',$friends_set);
$res = $this->facebook_model->get_top_friends($new_set);
$data['top_friends']=$res;
$this->load->view('myview',$data);
And now in view you can try
foreach ($top_friends as $me){
echo $me['first_name'];
}
[Updated for user ]
If you want to do it as in your question : then try,
$result = array();
while($row = mysql_fetch_array($res)) {
$result[] = $row;
}
$data['top_friends']=$result;
$this->load->view('myview',$data);//pass data to view

Dropdown using codeigniter is not dropping any data

In the below codeigniter code i have placed the controller ,model and view part.My aim is to drop exam name from exam table.In my case it is drop the exam name from exam table.Pls help me o do this .
Controller
public function index()
{
//echo "inside form upload";
$data = array();
//$college_name = $this->session->userdata('college_name');
if($query = $this->import_model->get_exam_data())
{
$data['exam_data'] = $query;
}
//$this->load->view('student_view', $data);
$this->load->view('form_upload');
}
model
function get_exam_data()
{
//$this->db->distinct();
$this->db->select("CONCAT(exam_name, ' ', month,' ',year) AS fullexamname", FALSE);//this will concat the value
//$this->db->where('college_name',$college_name);
$query = $this->db->get('exam_table');
return $query->result();
}
view
<?php
$data = array();
$data["Select Exam Name"] = "Select Exam Name";
foreach ($exam_data as $row)
{
$data[$row->fullexamname] = $row->fullexamname;
}
echo form_dropdown('exam_name', $data, 'small', 'class="dropdown_class" id="exam_name_id" ');
?>
*
You have to pass $data to your view file from controller
$this->load->view('form_upload',$data);

Foreach loop, and array building

I have an admin area where they can delete multiple users at a time. This is part of the code that handles the deletion. Basically it goes through the user ids and deletes each one that was marked checked.
if ($_POST['doDelete'] == 'Delete') {
if (!empty($_POST['u'])) {
foreach ($_POST['u'] as $uid) {
$id = escape($uid);
$delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
if (!$delete) {
$msg->add('e', QUERY_ERROR);
redirect('users.php');
exit();
}
}
}
/* we need a way to iterate over users deleted */
$msg = new Messages();
$msg->add('s', QUERY_DELETE_SUCCESS);
redirect('users.php');
exit();
}
function get_user_name_from_id($user_id)
{
if ($_SESSION['user_level'] == ADMIN_LEVEL) {
$viewUserMod = 1;
} else {
$config = Nemesis::select("usr_view_cm", "config");
$row_config = $config->fetch_assoc();
$viewUserMod = $row_config['usr_view_cm'];
}
if (is_numeric($user_id) && $viewUserMod == 1) {
$sql = Nemesis::select("full_name", "users", "id = {$user_id}");
if ($sql->num_rows > 0) {
$user_name = $sql->fetch_assoc();
return $user_name['full_name'];
} else {
// user name cannot be matched with db, either error, or most likely user was deleted
return 'User ' . $user_id;
}
} else {
return $user_id;
}
}
Where it says QUERY_DELETE_SUCCESS I would like to output something like "Deleted Bob, Jack, Tim" .etc I have a function that uses the users id and gets their names. The issue is that once the iteration is complete. Obviously those users no longer exist in the database and I cannot get their names. Is there a way of running this function during the loop, and building a string or array. That can be outputted in place of the message?
You should just be able to do this:
if ($_POST['doDelete'] == 'Delete') {
$deleted = array();
if (!empty($_POST['u'])) {
foreach ($_POST['u'] as $uid) {
$id = escape($uid);
$username = get_user_name_from_id($uid);
$delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
if (!$delete) {
$msg->add('e', QUERY_ERROR);
redirect('users.php');
exit();
}
$deleted[] = $username // push name to array after deletion is successful
}
}
/* The $deleted array now holds the names of the deleted users.
* Do with it what you want.
*/
$names = implode(",", $deleted)
$msg = new Messages();
$msg->add('s', QUERY_DELETE_SUCCESS . " Deleted: $names");
redirect('users.php');
exit();
}
There are several improvements that can be made here, including efficiency (combining many small single queries into a few larger ones) and error handling (don't redirect on the first error - instead redirect after all processing is complete to a page with a list of successes and errors), but this is the basic idea.
Here is a quick change that will do all of the operations, even if one of them errors:
if ($_POST['doDelete'] == 'Delete') {
$deleted = array();
$errored = array();
if (!empty($_POST['u'])) {
foreach ($_POST['u'] as $uid) {
$id = escape($uid);
$username = get_user_name_from_id($uid);
$delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
if (!$delete) {
$errored[] = $username;
} else {
$deleted[] = $username // push name to array after deletion is successful
}
}
}
/* The $deleted array now holds the names of the deleted users.
* The $errored array now holds the names of users who were not deleted due to errors.
* Do with them what you want.
*/
$msg = new Messages();
$names_deleted = implode(",", $deleted)
$msg->add('s', QUERY_DELETE_SUCCESS . " Deleted: $names_deleted");
if (count($errored) > 0) {
$names_errored = implode(",", $errored)
$msg->add('e', QUERY_ERROR . " Did not delete: $names_errored");
}
redirect('users.php');
exit();
}
You could add the names to an array as you are looping over the uids with something like this:
$names = array();
if (!empty($_POST['u'])) {
foreach ($_POST['u'] as $uid) {
$names[] = get_user_name_from_id($uid);
$id = escape($uid);
$delete = Nemesis::query("DELETE FROM users WHERE id = '{$id}' AND id <> '{$_SESSION[user_id]}'");
if (!$delete) {
$msg->add('e', QUERY_ERROR);
redirect('users.php');
exit();
}
}
}
Then when you want to output the confirmation message, you could turn that array into a comma separated string with something like this:
$names = implode(', ',$names);
$message = "Deleted $names";

How to display more number of array results in a single array?

I have two tables: loginandfollow`.
Table name: login
Fields: id,email,username,imageurl
Table name: follow
Fields: id:user_id:follow_id
It's like a Twitter follower's concept. I want to get the details of myfollower name and also myfollower's following person's name.
For that I have written the coding as like below.
public function follw ()
{
if( $this->input->get("userid") )
{
extract($this->input->get());
$followers_list = array();
$follower = array();
$query = $this->db->query('select follow_id from follow where user_id = '.$userid.'')->result();
foreach($query as $row)
{
$follower['follower_id'] = $row->follow_id;
if($follower['follower_id'] == "")
{
echo "hi";
}
else
{
$query3 = $this->db->query('select username from login where id = '.$follower['follower_id'].'')->result();
foreach($query3 as $row3)
{
$follower['followuser'] = $row3->username;
}
$query1 = $this->db->query('select follow_id from follow where user_id = '.$follower['follower_id'].'')->result();
foreach($query1 as $row1)
{
$follower['follow_id'] = $row1->follow_id;
if($follower['follow_id'] == "")
{
echo "jeeva";
}
else
{
$query2 = $this->db->query('select username from login where id = '.$follower['follow_id'].'')->result();
foreach($query2 as $row2)
{
$follower['username'] = $row2->username;
}
}//second for each in else loop
}//first foreach in else loop
}//main else
$followers_list[] = $follower;
}
$str = json_encode($followers_list);
echo stripslashes($str);
}
else
{
echo '[{"status":"Failure - Error Occured - Not Enough Details provided"}]';
}
}
I get the output like this:
[{"follower_id":"12","followuser":"janmejoy","follow_id":"24","username":"sarvana"},{"follower_id":"10","followuser":"jeeva","follow_id":"23","username":"selva"},{"follower_id":"6","followuser":"raj","follow_id":"17","username":"jeeva"},{"follower_id":"23","followuser":"selva","follow_id":"22","username":"guru"}]
This output displays myfollower's name and myfollower's following person name, but the problem is it displays only one member of myfollower's following person name.
However, I want to the output like this:
[{"follower_id":"12","followuser":"janmejoy",{"follow_id":"24","username":"sarvana",follow_id":"13","username":"jai",follow_id":"9","username":"raj"}},{"follower_id":"10","followuser":"jeeva","follow_id":"23","username":"selva"},{"follower_id":"6","followuser":"raj","follow_id":"17","username":"jeeva"},{"follower_id":"23","followuser":"selva","follow_id":"22","username":"guru"}]
[{"follower_id":"12","followuser":"janmejoy",{"follow_id":"24","username":"sarvana",follow_id":"13","username":"jai",follow_id":"9","username":"raj"}},{"follower_id":"10","followuser":"jeeva","follow_id":"23","username":"selva"},{"follower_id":"6","followuser":"raj","follow_id":"17","username":"jeeva"},{"follower_id":"23","followuser":"selva","follow_id":"22","username":"guru"}]
This code is invalid JSON variable. i can't solve this question for you. Please update your question.

Categories