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']);
Related
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.
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;
}
}
?>
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:
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>
I am trying to display information from database. I have set $config['per_page'] to 2, in my view file I can see the information I want but when I click on the next button it doesn't change anything. The database values remain same and the current page remains the first page too.
Would you please kindly help me figure out the problem?
Thanks in Advance :)
Controller:
function index($id){
$this->load->library('pagination');
$config['base_url'] = site_url().'Student_fee_status/index/'.$id;
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$query = $this->db->get('');
$numrows=$query->num_rows();
$config['total_rows'] = $numrows;
$config['per_page'] = 2;
$config['uri_segment'] = '2';
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('Mod_student_fee_status');
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
$data['main_content']='view_student_fee_status';
$this->load->view('includes/template',$data);
}
My Model :
function fee_status($id,$perPage,$uri_segment) {
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$getData = $this->db->get('', $perPage, $uri_segment);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
EDIT
When the page first loads the link looks like this- http://localhost/sundial/Student_fee_status/index/1006/
but when I click on the next page it looks like this- http://localhost/sundial/Student_fee_status/index/1006/2
My View File:
<h1>Payment Status</h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>S.N</th>
<th>Invoice ID</th>
<th>Transaction Description</th>
<th>Received Date</th>
<th>Debit</th>
<th>Credit</th>
<th>Balance</th>
</tr>
</thead>
<?php $i = $this->uri->segment(2) + 0; foreach ($records as $row){ $i++; ?>
<tbody>
<?php
$mydate= $row['period'];
$month = date("F",strtotime($mydate));
$year = date("Y",strtotime($mydate));
?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td><a href="<?php echo base_url(); ?>student_fee_status/fee_types/<?php echo $row['paymentid']; ?>" rel="1" class="newWindow" >Total Fee For <?php echo $month ;?>, <?php echo $year ;?> </a></td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo $row['totalamount'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td>Payment Received </td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['amountpaid'];?></td>
<td>
<?php
$balance=$row['totalamount']-$row['amountpaid'];
if($balance>0){
echo "<font color=\"red\">$balance</font>";
}
else {
echo $balance;
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
You are telling the pagination library to use $config['uri_segment'] = '2'; - the second segment of your uri.
When this is your url: http://localhost/sundial/Student_fee_status/index/1006/ I am guessing this is your base_url: http://localhost/sundial/
In this case your segments are:
Student_fee_status - your controller
index - the controllers method you are calling
1006 - the argument you are calling the controllers method with
this should be the argument for pagination
Try this
$config['uri_segment'] = '4';
instead of
$config['uri_segment'] = '2';
edit:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
I think this line contains another error.
You pass your model the information which uri_segment is used by the pagination library. That should be 4 now. However, your model uses this value to specify an offset in your query. This means you always put an offset of 4 into your query. But I think what you really want to do is, pass the model the VALUE of the 4th uri_segment.
I would try this instead:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$this->uri->segment($config['uri_segment']));