View doesn't load - codeigniter

I want to ask about view
here I have controller like this: (api.php)
public function export_excel() {
$date = new DateTime($this->input->post('date_fil'));
$curr_date = $date->format('Y-m-d ');
$user = $this->input->post('sales_name');
$checked = $this->input->post('cb_month');
$month = date('m');
if ((int) $checked == 1) {
$this->form_validation->set_rules('sales_name', 'Sales Name', 'required');
if ($this->form_validation->run() == false) {
$this->output->set_output(json_encode([
'result' => 0,
'error' => $this->form_validation->error_array()
]));
return false;
}
$this->db->select('t1.act_id, t2.login, t1.cust_name, t1.act_type, t1.act_detail, t1.date_added, t1.date_modified, t1.act_notes')
->from('activity as t1')
->join('user as t2', 't1.user_id = t2.user_id', 'LEFT')
->where('t2.login', $user)
->where('MONTH(t1.date_added)', $month);
$query = $this->db->get();
$result = $query->result_array();
$data = array('title' => 'Sales Report',
'user' => $result);
$this->load->view('report/vw_excel', $data);
// Selecting data by Date ----------------------------------------------
} else {
$this->form_validation->set_rules('sales_name', 'Sales Name', 'required');
$this->form_validation->set_rules('date_fil', 'Date', 'required');
if ($this->form_validation->run() == false) {
$this->output->set_output(json_encode([
'result' => 0,
'error' => $this->form_validation->error_array()
]));
return false;
}
$this->db->select('t1.act_id, t2.login, t1.cust_name, t1.act_type, t1.act_detail, t1.date_added, t1.date_modified, t1.act_notes')
->from('activity as t1')
->join('user as t2', 't1.user_id = t2.user_id', 'LEFT')
->where('t2.login', $user)
->where('DATE(t1.date_added)', $curr_date);
$query = $this->db->get();
$result = $query->result_array();
$data = array('title' => 'Sales Report',
'user' => $result);
$this->load->view('report/vw_excel', $data);
}
and here's the view I want to load it's under folder (report/vw_excel)
<body>
<main>
<h1>Excel Report</h1>
<p>Export to Excel</p>
<table border="1" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Sales Name</th>
<th>Customer Name</th>
<th>Activity</th>
<th>Detail</th>
<th>Start Time</th>
<th>Finish Time</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<?php $i=1; foreach($user as $xuser) { ?>
<tr>
<td><?php echo $xuser['act_id']; ?></td>
<td><?php echo $xuser['login']; ?></td>
<td><?php echo $xuser['cust_name']; ?></td>
<td><?php echo $xuser['act_type']; ?></td>
<td><?php echo $xuser['act_detail']; ?></td>
<td><?php echo $xuser['date_added']; ?></td>
<td><?php echo $xuser['date_modified']; ?></td>
<td><?php echo $xuser['act_notes']; ?></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</main>
when I call it from other view folder(admin/admin_view)
onsubmit = 'api/export_excel',
this "$this->load->view('report/vw_excel', $data);" wont load the view.
the result are showing in the browser's network, but the view doesn't load.
network view
any idea? thanks.

Do a right click on the screen - inspect element. In the elements panel, select the table that was supposed to be there on screen. Check in the Style panel if the same is not turned off from being displayed for some reason.
You may also check its position (if in case it is visible) on screen for you to be able to diagnose whether the element is actually in screen dimensions.

Related

fetch email from id and send mail to that particular user in codeigniter

i have one view candidates form in my application ..in that i display all the candidates details and i display user_id also..i gave on button called send in the view page..
so what i am trying is when i click on the button i need to fetch the user email and send mail to that user..
view code:
<section class="content">
<div class="row">
<div class="col-xs-12">
<!-- /.box-header -->
<div class="box-body">
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th></th>
<th>Vendor</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Mobile Number</th>
<th>experience</th>
<th>CTC</th>
<th>Expected Ctc</th>
<th>role</th>
<th>Current Location</th>
<th>Desired Location</th>
<th>Notice Period</th>
<th>Resume</th>
<th>Actions</th>
</tr>
</thead>
<?php
foreach ($view_candidates as $idata)
{
?>
<tbody>
<tr id="domain<?php echo $idata->user_id;?>">
<td class="cell checkbox">
<input type="checkbox" class="selectedId" name="selectedId" />
</td>
<td><?php echo $idata->user_id;?></td>
<td><?php echo $idata->first_name;?></td>
<td><?php echo $idata->last_name;?></td>
<td><?php echo $idata->email;?></td>
<td><?php echo $idata->mobile_number;?></td>
<td><?php echo $idata->experience;?></td>
<td><?php echo $idata->ctc;?></td>
<td><?php echo $idata->expectedctc;?></td>
<td><?php echo $idata->role_name;?></td>
<td><?php echo $idata->current_location;?></td>
<td><?php echo $idata->desired_location;?></td>
<td><?php echo $idata->notice_period;?></td>
<td><?php echo $idata->resume;?></td>
<td><button id="<?php echo $idata->candidate_id; ?>" name="button" onClick="CallFunction(this.id)" class="btn btn-info">send</button></td>
</tr>
<?php
}
?>
</tbody>
Controller:
public function change_status()
{
$candidate_id = $this->input->post('candidate_id');
$this->CandidateModel->update_status($candidate_id);
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3'
);
$this->load->library('email',$config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to('someone#gmail.com');
$this->email->subject('Request for contact info');
$link = 'Click on this link - Click Here';
$this->email->message($link);
if($this->email->send())
{
echo "email send";
}
else
{
echo "failed";
}
// echo true;
// exit;
}
Model:
function update_status($candidate_id)
{
$this->db->select('*');
$this->db->from('candidates_details');
$status = $this->db->query("update candidates_details set status='1' where candidate_id ='$candidate_id'");
$data=array('status'=>$status);
$this->db->where('candidate_id',$candidate_id);
//$this->db->update('candidates_details',$data);
//$query=$this->db->get();
echo $this->db->last_query();
//return $query->result();
}
Can anyone help me how to send mail to that user..
Thanks in advance..
Do something like below:
<script>
function CallFunction(id){
var url = '127.0.0.1/job_portal/index.php/Candidate/change_status/'‌​;;
$.ajax({
url: url,
type: 'POST',
data: {
candidate_id: id
},
dataType: 'JSON',
success: function(data) {
if(data == 1) {
$('.button').text('FINISHED');
} else {
$('.button').text('TRY AGAIN!');
}
}
});
}
</script>
Controller and Model:
<?php
// controller
function change_status()
{
$data = $_POST;
$d = $this->user_model->send_email($data['candidate_id']);
echo json_encode($d);
}
// model
function send_email($candidate_id)
{
$q = $this->db->query("SELECT u.email_id as email_id FROM tbl_candidate as c, tbl_user as u WHERE c.candidate_id = $candidate_id AND c.user_id = u.user_id");
if($q->num_rows() > 0) {
$d = $q->row_array();
$to = $d['email_id'];
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3'
);
$this->load->library('email',$config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to($to);
$this->email->subject('Request for contact info');
$link = 'Click on this link - Click Here';
$this->email->message($link);
if($this->email->send())
{
return 1;
}
else
{
return 0;
}
} else {
return 0;
}
}
?>

A PHP Error was encountered Severity: Notice Message: Undefined property: Site::$site_model

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Site::$site_model
Filename: controllers/site.php
Line Number: 13
Site Controller site.php
Fatal error: Call to a member function delete_row() on a non-object in
C:\xampp\htdocs\login\application\controllers\site.php on line 13
My controller->Site.php
<?php
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
}
function delete(){
$this->site_model->delete_row();
$this->index();
}
function members_area(){
$this->load->model('patient_model');
$data['records'] = $this->patient_model->getAll();
$this->load->view('members_area', $data);
}
function add(){
$data['main_content'] = 'patient_form';
$this->load->view('includes/template',$data);
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true){
echo 'Your don\'t have permission to access this page. Login';
die();
}
}
}
My model -> site_model.php
class Site_model extends CI_Model{
function get_records(){
$query = $this->db->get('patient');
return $query->result();
}
function delete_row(){
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('patient');
}
function create_member(){
$new_member_insert_data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'contact' => $this->input->post('contact'),
'remarks' => $this->input->post('remarks')
);
$insert = $this->db->insert('patient', $new_member_insert_data);
return $insert;
}
}
My view-> members_area.php
<?php $this->load->view('includes/header');?>
<div id="main">
Welcome Back, <u><?php echo $this->session->userdata('username'); ?>!</u>
<h3>Vision Eye Medical Center Patient Information</h3>
<div id="overflow">
<table class="features-table">
<thead>
<tr>
<td>Patient Name</td>
<td>Email</td>
<td>Address</td>
<td>Contact</td>
<td>Remarks</td>
<td>Action</td>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tfoot>
<tbody>
<?php foreach($records as $row): ?>
<tr>
<td><?php echo $row->fname;?>
<?php echo $row->lname;?>
</td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->address;?></td>
<td><?php echo $row->contact;?></td>
<td><?php echo $row->remarks;?></td>
<td>View|<?php echo anchor("site/delete/$row->id",'delete');?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php echo form_open();?>
<?php echo anchor('patient/add', 'Add new Data'); ?><?php echo anchor('login/logout', 'Logout'); ?><br>
<?php echo form_close();?>
</div>
<?php $this->load->view('includes/footer');?>
It seems you are not loading your "site model" anywhere in code you can do following:
autoload it in autoload file.
load your model either in constructor of controller, or load it whenever needed by using this line $this->load->model('site_model');
More information can be found here.
You need to load model in your controller.
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
$this->load->model('site_model'); //Load site_model
........
In somecase it happens when you don't extract your form data to the model. Try extract($_POST) in your model and don't forget to load the model properly.
P.s.:this was the solution for my error

Image upload to specific folder in codeigniter

I've been trying to upload images to specific folder my scenario is I've upload controller in which I've do_upload function
public function do_upload($field_name) {
$field_name = 'image_title';
$page_id = $this->input->post('page_id');
$config = array(
'allowed_types' => '*',
'max_size' => '1024',
'max_width' => '1024',
'max_height' => '768',
'upload_path' => './uploads/'. $page_id
);
$this->load->library('upload');
$this->upload->initialize($config);
if (!is_dir('uploads'))
{
mkdir('./uploads/', 0777, true);
}
$dir_exist = true; // flag for checking the directory exist or not
if (!is_dir('uploads/' . $page_id))
{
mkdir('./uploads/' . $page_id, 0777, true);
$dir_exist = false; // dir not exist
}
else{
}
if (!$this->upload->do_upload($field_name)) {
if(!$dir_exist)
rmdir('./uploads/' . $page_id);
$this->data['error'] = array('error' => $this->upload->display_errors());
} else {
$fInfo = $this->upload->data($field_name);
$this->_createThumbnail($fInfo['file_name']);
return $fInfo;
}
}
/**********************************************************************************************/
function _createThumbnail($filename)
{
$config['image_library'] = "gd2";
$config['source_image'] = "uploads/" .$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = "80";
$config['height'] = "80";
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
I've index view file given as
<section>
<?php echo validation_errors(); ?>
<?php //echo form_open_multipart('admin/upload/index/' . ((isset($page->id)) ? $page->id : '' )); ?>
<?php echo form_open_multipart('admin/upload/index/'); ?>
<tr>
<td><h3>Upload Images</h3></td>
</tr>
<table class="table table-striped">
<tr>
<thead>
<tr>
<th>Image Name</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php if (count($images)): foreach ($images as $image): ?>
<tr>
<td><?php echo anchor('admin/upload/index/' . $image->id, $image->image_title); ?></td>
<td><?php echo btn_edit('admin/upload/index/' . $image->id); ?></td>
<td><?php echo btn_delete('admin/upload/index/' . $image->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">There is no Image to display</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</section>
<section>
<table class="table table-striped">
<tbody>
<tr>
<td><h5>Select Page</h5></td>
<td><?php echo form_dropdown('page_id', $get_with_images, $this->input->post('page_id')); ?></td>
</tr>
<tr>
<td><h5>Select Image</h5></td>
<td>
<?php echo form_upload('image_title', set_value('image_title', $image->image_title)); ?>
</td>
</tr>
<tr>
<td><?php echo form_submit('submit', 'Upload', 'class="btn btn-success"'); ?></td>
</tr>
</tbody>
</table>
<?php echo form_close(); ?>
</section>
dropdown is populating from page controller what actually I needed is I want to upload image to the page selected from dropdown for example if "Contact" page is selected from dropdown I want to upload my image to "Contact" page and on the backend I want to create directory with the same name as selected in dropdown in my case I want "uploads/home/abc.jpg" and same for the other pages please advice the basic idea I will modify it with my own
You're already sending your pageid value as post. Use it at uploading config.
...
$config = array(
...
'upload_path' => './upload/' . $this->input->post('page_id')
)
You need to check if directory exists first, use isdir() PHP function, and if false, mkdir(), also PHP, with folder name and permission.
I'm not familiar anymore with CodeIgniter to tell you if there is some way to get written value of a <option> tag, but I would suggest to create a array with page ids and their respective name and look for the array's index by post value:
$page_name = [
0 => 'contact',
1 => 'home',
2 => 'user',
...
]
...
$config = array(
...
'upload_path' => './upload/' . $page_name[$this->input->post('page_id')]
)
EDIT: User had problems with thumb creation
With your update, you forgot to update also path of the thumbnail. So you must send the folder and filename:
function _createThumbnail($folder, $filename)
{
$config['image_library'] = "gd2";
$config['source_image'] = "./uploads/" $folder . "/" .$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = "80";
$config['height'] = "80";
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
Then you should call it as you were, replacing the parameters correctly:
$this->_createThumbnail($page_id, $fInfo['file_name']);

how to get result of query in view in codeigniter

I have use two query to get result on the view.
To get the result of resultq1 i use foreach but how can i get result of resultq2 in view.
For each row of resultq1 i get record "reccount" in resultq2.
//controller
//Query 1
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
$i=0;
foreach($resultq1 as $rec)
{
//query 2
$id = $rec['id'];
$q1 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where('spft.forumid',$id)->get();
$resultq2[$i] = $q1->row_object();
$i++;
}
$this->load->view('forumview', $data, true);
//view file
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >---- </td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
Array print in view for resultq2.
Resultq1 have 4 rows so that i get 4 value in resultq2.
Array ( [0] => stdClass Object ( [reccount] => 0 ) [1] => stdClass Object ( [reccount] => 0 ) [2] => stdClass Object ( [reccount] => 0 ) [3] => stdClass Object ( [reccount] => 2 ) ) 0002
You need to pass $resultq2 to the view as well.
In the controller, just before calling the view
$data['resultq2'] = $resultq2
You can now use $resultq2 in your view.
PS - SQL queries should be in models, not controllers.
Just Write '$data['resultq2'] = $resultq2' before loading the view,
$data['resultq2'] = $resultq2
$this->load->view('forumview', $data, true);
And you can retrieve it in the view by $result2 variable.
Just do var_dump($result2); in your view file, You will get the complete data of $result2 varaible.
And xbonez is right, do write your DB queries in the model, controller is only for login, and coordinating between (models, views, libraries, helper etc...)
I optimized your second query a bit, but logic is pretty much the same.
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$ids = array();
foreach($resultq1 as $result)
$ids[] = $result['id'];
$resultq2 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where_in('spft.forumid',$ids)->get();
foreach( $resultq2->result_array() as $res )
$newArr[$res['forumid']] = $res;
$data['resultq2'] = $newArr;
After resultq2 is indexed by original id, its easy to use id from resultq1 loop to show correct data
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >
<?php
print_r( $resultq2[$row['id']]);
?>
</td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
This should fix it.
Modify the controller - see comments also
<?php
//Query 1
$q = $this->db->select(array(
'spf.id as id',
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1 = $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
foreach ($resultq1 as $rec) {
//query 2
//echo $id = $rec['id']; //Test and See if all IDs echo out correctly
$data["id"] = $id; //Add this for the ID
$q1 = $this->db->select("count('id') as reccount, spft.forumid")->from('sp_forum_topic spft')->where('spft.forumid', $id)->get();
$resultq2[$id] = $q1->result(); //Change this line
$data["resultq2"][$id] = $resultq2[$id]; //Add this line
}
//echo "<pre>";
//$export = var_export($data, true);// Test the returned value...
//echo "</pre>";
$this->load->view('forumview', $data, true);
?>
In Your view
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php //foreach ($resultq1 as $row): ?>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td>
<?php
//$resultq2 is now array
//print_r($resultq2); //Do this so you can see the depth of the array
foreach ($resultq2 as $counts) {
foreach ($counts as $count) { //The second foreach might be necessary - please test
echo $count->reccount; //This should print out the count result
//echo "<br />";
}
}
?>
</td>
<td><?php echo $row['added_by']; ?></td>
<td ><?php echo $row['added_on']; ?></td>
<td >
</td>
</tr>
<?php
endforeach;
exit;
?>
</table>
I haven't had time to test it but it should work. Let me know if you find any issues:

i want to send email with multiple database values

if($records->result()>0)
{
foreach ($records->result() as $user)
{
$username= ('first name='.$user->u_first_name.'<br/>'.'Last name='.$user->u_last_name.'<br/>'.'Email='.$user->u_email.'<br/>'.'Property Id='.$user->propertyid);
$username.="<br/>";
$username.="-------------------------";
$username.="<br/>";
$email_template = file_get_contents($this->config->item('base_url').'assets/email/email.html');
$email_template = str_replace("[[EMAIL_HEADING]]", $mail_content->subject, $email_template);
$email_template = str_replace("[[EMAIL_CONTENT]]", $username, $email_template);
$email_template = str_replace("[[SITEROOT]]", $this->config->item('base_url'), $email_template);
$email_template = str_replace("[[LOGO]]",$this->config->item('base_url')."assets", $email_template);
$this->email->message(html_entity_decode($email_template));
$this->email->send();
print_r($email_template);
this is my code
/* UPDATE */
You can use a view for your template like normal (passing in values), setting the third parameter as TRUE to return the html.
To send one email with all database records, just pass the entire result object into the view, the process it in the view using your standard foreach loops, etc..
E.g
if($records->result()>0) {
$email_template = $this->load->view('email_template', array('heading' => 'My Email Report', 'records' => $records->result(), TRUE);
$this->email->message($email_template);
$this->email->send();
print_r($email_template);
}
Then the view (/view/email_template) would be something like;
<h1><?php echo $heading; ?>
<p> Records;</p>
<table>
<?php
foreach ($records as $r) {
?>
<tr>
<td><?php echo $r->u_first_name; ?></td>
<td><?php echo $r->u_last_name; ?></td>
<td><?php echo $r->u_email; ?></td>
<td><?php echo $r->propertyid; ?></td>
</tr>
<?php
}
?>
</table>

Resources