upload image from rest client (android) to database with codeigniter - codeigniter

i want to upload the image profile and the user id from my android application to database. i tried this method and it work in local but when i test it with (POSTMAN) a rest client it doesn't work. i think because i call a view.
this is my code please help me.
my controller
class Users extends REST_Controller {
function __construct()
{ parent::__construct();
$this->load->model('Users_model','',TRUE);}public function index_get()
{
$this->load->view('users');
}
public function save_image_post()
{
$id = $_POST["id"];
$url = $this->do_upload();
$this->Users_model->save_image($id, $url);
//$this->Users_model->save_image($url);
}
private function do_upload()
{
$type = explode('.', $_FILES["pic"]["name"]);
$type = strtolower($type[count($type)-1]);
$url = "./images/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg", "jpeg", "gif", "png")))
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
return $url;
return "";
}
}
this is my model:
public function save_image($id,$url)
{
$this->db->set('id', $id);
$this->db->set('image_user', $url);
$this->db->insert('users');
}
this is my view:
<!DOCTYPE html><html lang="en"><head></head><body>
<?php echo form_open_multipart('api/users/save_image/'); ?>
<table class="table">
<tr>
<td>Id</td>
<td><?php echo form_input('id'); ?></td>
</tr>
<tr>
<td>Image</td>
<td><?php echo form_upload('pic'); ?></td>
</tr>
<tr><td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table></body></html>

Related

its showing variable undefined

This is My Controller page
class Cart extends CI_Controller{
public function index()
{
$getALlCat = $this->Product_model-> getAllCategory();
$data = array();
//Retrieve cart Data
$data['cartItems'] = $this->cart->contents();
//print_r($data);exit;
//$this->load->view('cart/index' ,$data);
///$this->load->helper('url');
/ /$this->load->view('cart/header',['getAllCate'=>$getALlCat], $data); //including header and
footer
$this->load->view('cart/cartbody',['getAllCate'=>$getALlCat], $data);
$this->load->view('common/footer'); //including header and footer
}
}
**This is View page**
<tbody>
<?php if($this->cart->total_items() > 0){ foreach($cartItems as $item){ ?>
<tr>
<td><img src="<?php echo $item['product_image']; ?>" width="50"/></td>
<td><?php echo $item['product_name']; ?></td>
<td><?php echo '$'.$item["product_price"].' USD'; ?></td>
<td><input type="number" class="form-control text-center"></td>
<td><?php echo '$'.$item["subtotal"].' USD'; ?></td>
<td>Are you sue</td>
</tr>
<?php } }else{ ?>
<td colspan="6"><p>Your cart is empty.....</p></td>
<?php } ?>
</tbody>
**Error Showing like this**
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cartItems
Filename: cart/cartbody.php
Line Number: 217
Backtrace:
File: C:\xampp\htdocs\shop\application\views\cart\cartbody.php
Line: 217
Function: _error_handler
File: C:\xampp\htdocs\shop\application\controllers\Cart.php
Line: 33
Function: view
File: C:\xampp\htdocs\shop\index.php
Line: 315
Function: require_once
You can store multiple values as an array key of $data and then send only $data to view and use those key to retrieve values. I've rectified your code below and it should work as your needs. I've mentioned comments wherever necessary.
public function index() {
$data = array();
$data['getALlCate'] = $this->Product_model-> getAllCategory(); // store it in an array instead (I've changed it to getALlCate from getALlCat, so you don't have to change anything in your view)
//Retrieve cart Data
$data['cartItems'] = $this->cart->contents();
//$this->load->view('cart/header', $data); // including header and footer
$this->load->view('cart/cartbody', $data);
$this->load->view('common/footer'); //including header and footer
}
}
// you can retrieve data in view by accessing $cartItems and $getALlCate variables.
Hope it helps you.
Try following
$this->load->view('cart/cartbody',['getAllCate'=>$getALlCat, 'cartItems'=> $this->cart->contents()]);

how to retrive data from db to the table using getAll()

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Unit extends CI_Controller {
/*Load supplier default*/
public function index($id='')
{
$this->load->model('Unit_model');
($id!='') ? $data["unit_details"]=$this->Unit_model->get_unit($id) :'';
$this->load->view('includes/header');
$this->load->view('includes/left_menu');
$this->load->view('unit/unit_manage', ($id!='') ? $data : '');
$this->load->view('includes/footer');
/*Submit supplier on add/edit */
}
public function post_unit()
{
$this->load->model('Unit_model');
$this->db->trans_begin();
// transaction begin statement
try{ //Use try catch statement here
$this ->db-> trans_complete();
if ($this->input->is_ajax_request()){
$case = $this->input->post('case');
//for both case use insert_customer function
if($case=='edit')
$insert_unit=$this->Unit_model->update_unit();
else
$insert_unit=$this->Unit_model->insert_unit();
$data['insert_unit']=$insert_unit;
//place commit statement
$this ->db-> trans_commit();
}
else
{
// through new exception statement
}
}
catch(Exception $e){ //statement here
// Place roll back statement
$this->db-> trans_rollback();
}
$this->sendData($data);
}
} ?>
This is my controller part.
class Unit_model extends CI_Model
{
protected $strTableName = 'suc_unit';
function __construct()
{
parent::__construct();
$CI = &get_instance();
$this->db->from($this->strTableName);
}
/*Load all details of table*/
function get_all()
{
$query = $this->db->get($this->strTableName);
return $query->result();
}
}
The above is my model... i want disply data in my page using the getAll() declared in the model. The insertion, updation and view should take place in the same page
<table id="tblListOfUnits" class="table table-bordered table-striped">
<tr>
<th style="width: 10px">Sl No</th>
<th>Unit Name</th>
<th>Unit Symbol</th>
<th style="width: 40px">No.Of Decimal</th>
</tr>
<?php
if(isset($units)){
$count = 1;
foreach ($units as $key => $unit) {
?>
<tr id="tr_<?php echo $unit->pk_int_unit_id; ?>">
<td id="tdUntSlNo_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $count; ?>.</a></td>
<td id="tdUntNme_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_name; ?></a></td>
<td id="tdUntSymbl_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_symbol; ?></a></td>
<td id="tdUntDecml_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->int_no_decimal; ?></a></td>
</tr>
<?php
$count ++;
}
}?>
</table>
this is my view page i want to display data in that table using getAll() statement i wrote in the model. The page contain the add & view page together. please help me to complete my work

codeigniter 3.1.3 pagination shows same record

i don't understand why this is happening but same row on every page reload shows. I'm new to codeigniter and know basics of php and programming. Thank you for help in advance.
model:
class Clanovi_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_clanovi($limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get('clan');
return $query->result();
}
public function broji_clanove() {
return $this->db->count_all('clan');
}
}
controller:
class Clanovi extends CI_Controller {
public function index() {
$this->load->model('Clanovi_model');
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = 'http://localhost/codeigniter5/index.php/clanovi';
$config['total_rows'] = $this->Clanovi_model->broji_clanove();
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['clanovi'] = $this->Clanovi_model->get_clanovi($config['per_page'], $this->uri->segment(3));
$data['paginacija'] = $this->pagination->create_links();
$this->load->view('zaglavlje');
$this->load->view('clanovi', $data);
$this->load->view('podnozje');
}
}
view:
<h2>Članovi</h2>
<hr>
<table class="table table-striped">
<tr>
<th>Ime</th>
<th>Prezime</th>
<th>Adresa</th>
<th>Korisničko ime</th>
<th>Lozinka</th>
</tr>
<?php foreach ($clanovi as $clan): ?>
<tr>
<td><?php echo $clan->ime; ?></td>
<td><?php echo $clan->prezime; ?></td>
<td><?php echo $clan->adresa; ?></td>
<td><?php echo $clan->korisnicko_ime; ?></td>
<td><?php echo $clan->lozinka; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $paginacija; ?>
routes:
$route['clanovi/(:num)'] = 'clanovi/index/$1';
$route['clanovi'] = 'clanovi';
screenshot1
screenshot2
Found solution by looking what $this->uri->segment(3); does, and changed value to 2.
$data['clanovi'] = $this->Clanovi_model->get_clanovi($config['per_page'], $this->uri->segment(2));

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

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