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);
Related
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;?>
I am working in CakePHP. I want to save posted data into my database.So I have created function in controller that looks like below :
function add_news()
{
$this->_checkSession();
$this->layout = "admin_inner";
if(!empty($this->params['form']))
{
$tit = $this->params['form']['title'];
$con = $this->params['form']['content'];
$query = "insert into news(news_title,news_content) values('".$tit."','".$con."')";
echo $query;
$this->News->query($query);
}
Configure::write('debug', '2');
}
I print query and execute in database then it works fine.But here it does not execute.I do not have News model.
Note : I am working in CakePHP 1.3.13
first you need to know how to insert data in cakephp ....link for this is :- http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html
then you can try this code to insert data in table
if(!empty($this->params['form']))
{
$tit = $this->params['form']['title'];
$con = $this->params['form']['content'];
$data = array(
"news_title"=>$tit,
"news_content"=>$con
);
$this->News->save($data);
}
function add_news()
{
$this->_checkSession();
$this->layout = "admin_inner";
if(!empty($this->params['form']))
{
$tit = $this->params['form']['title'];
$con = $this->params['form']['content'];
$data = array(
"news_title"=>$tit,
"news_content"=>$con
);
$this->News->save($data);
}
Configure::write('debug', '2');
}
I have fetched the record in my model against each user from database as follows:
public function counter_records() {
$table = 'usersearchs';
$this->db->select("domain, time");
$this->db->from($table);
$this->db->where("user_id", $this->session->userdata("user_id"));
$res = $this->db->get();
$num_of_records = $res->num_rows();
return $num_of_records;
}
Now I want to show these number of records into a view file for each user account, they have to know how many records they have.
Use this function for display all records against user_id in your Model:
public function userData()
{
$results = array();
$table = 'usersearchs';
$this->db->select("domain, time");
$this->db->from($table);
$this->db->where("user_id",$this->session->userdata("user_id"));
$query = $this->db->get();
$num_of_records = $query->num_rows();
if($num_of_records > 0){
$results = $query->result_array();
}
return $results;
}
In Controller:
call this function in your controller file like.
$this->load->model('model_yourmodel');
$data['records'] = $this->model_yourmodel->userData();
And pass this data in your view from controller as like:
$this->load_view("yourviewpath" , $data);
In HTML View File:
Use loop for display this data like that:
<?
$total_records = count($records);
if($total_records <= 3){
echo (4-$total_records). " records remaining in your account";
}
else{
echo "You have reached the maximum record";
}
if($total_records > 0){ // $records that you have pass from your controller.
foreach($records as $value){
?>
// Your HTML Code
Domain <?=$value['domain']?> - Time <?=$value['time']?> <br/>
// Your HTML Code
<?
}
}
else{
// your HTML
echo "No record found";
// your HTML
}
?>
Result should be like that in HTML:
domain1 - time1
domain2 - time2
domain3 - time3
domain4 - time4
domain5 - time5
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 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