I use download helper in codeigneter,, when i download file jpeg and open it,, it becomes invalid...
What should i do..
Thank you...
Controller
function downloadSuratElektronik($file)
{
$this->load->helper('download');
$file_data = file_get_contents(base_url()."data/".$file);
$file_name = $file;
force_download($file_name, $file_data);
}
Model
function getSuratKeluar(){
$data = $this->db->get('surat_keluar');
if ($data->num_rows() > 0){
foreach ($data->result() as $row){
$hasil[]=$row;
}
return $hasil;
}
}
View
<table>
<tr>
<td>File Upload</td>
</tr>
<?php
foreach ($hasil as $data) :
?>
<tr>
<td>/<?=$data->link_upload?></td>
</tr>
<?php endforeach; ?>
</table>
I got the same problem, i fixed it using other method... if u still want it
$file_name = "picture.jpg"
$file_path = "your path"
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$file_name");
ob_clean(); flush();
readfile($file_path);
Related
my model
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class User_model extends CI_Model{
public function get_user($offset,$limit,$q=''){
$sql = "SELECT * FROM users WHERE 1=1 ";
if($q!=''){
$sql .=" AND name LIKE '%{$q}%' ";
}
$result['count'] = $this->db->query($sql)->num_rows();
$sql .=" LIMIT {$offset},{$limit} ";
$result['data'] = $this->db->query($sql)->result();
return $result ;
}
}
<?php
class User extends CI_Controller{
public function __construct(){
parent::__construct();
//load the model
// $this->load->model('user_model');
}
//load user view
public function index(){
$this->load->view('user_view');
}
//get data for datagrid
public function get_user(){
/*Default request pager params dari jeasyUI*/
$offset = isset($_POST['page']) ? intval($_POST['page']) : 1;
$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$search = isset($_POST['search']) ? $_POST['search'] : '';
$offset = ($offset-1)*$limit;
$data = $this->user_model->get_user($offset,$limit,$search);
$i = 0;
$rows = array();
foreach ($data ['data'] as $r) {
//array keys ini = attribute 'field' di view nya
$rows[$i]['first_name'] = $r->first_name;
$rows[$i]['last_name'] = $r->last_name;
$rows[$i]['phone'] = $r->phone;
$rows[$i]['email'] = $r->email;
$i++;
}
//keys total & rows wajib bagi jEasyUI
$result = array('total'=>$data['count'],'rows'=>$rows);
echo json_encode($result); //return nya json
}
}
<?php $url_data=base_url().'user/get_user';?>
<?php echo $url_data;?>
<table id="dg" title="Product" class="easyui-datagrid"
style ="width:auto;height:400px"
url ='<?php echo $url_data;?>' toolbar="#toolbar"
pagination="true"
rownumbers="false"
fitColumns="true" singleSelect="true"
checkBox ="true" striped="true"
remoteSort="false"
nowrap ="false">
<thead>
<tr>
<th field="ck" checkbox="true"></th>
<th field="first_name" width="200" sortable="true">
<b>First Name</b>
</th>
<th field="last_name" width="700" sortable="true">
<b>Last Name</b>
</th>
<th field="phone" width="100" sortable="true">
<b>Phone</b>
</th>
<th field="email" width="100" sortable="true">
<b>email</b>
</th>
</tr>
</thead>
</table>
I'm not that much into OOP so I may be wrong.
I think you forgot to encode the result of your SQL query in JSON before returning it :
echo json_encode($result);
Hope this will help.
On my view I need to be able to get the hidden banner_image_id and use that in my $this->db->where('banner_image_id', $id) on my model function
When I update the image. It does not update to the correct row. Instead it update all rows with the same filename.
Question: On my model how can I make sure that my $this->db->where('banner_image_id', $id) updates correct rows.
I have done a vardump($id) and result is string(2) "63" which is correct but still updates every row the same. I tried update_batch also no luck.
Model Function
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
if (isset($banner_image_id)) {
foreach ($banner_image_id as $id) {
//var_dump($id);
//exit;
$data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);
$this->db->where('banner_image_id', $id);
//$this->db->where_in('banner_image_id', $id); // Tried No Luck
//$this->db->or_where_in('banner_image_id', $id); // Tried No Luck
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
}
View Table
<table>
<tbody>
<?php foreach ($banner_images as $img) { ?>
<tr>
<td>
<?php echo $img['banner_image_id'];?>
<input type="hidden" name="banner_image_id[]" value="<?php echo $img['banner_image_id'];?>">
</td>
<td>
<input type="file" name="banner_image[]" multiple size="20">
</td>
<td>
<img src="<?php echo base_url() . 'uploads/' . $img['banner_image'];?>" />
<input type="hidden" name="banner_image" value="<?php echo $img['banner_image'];?>">
</td>
</tr>
<?php }?>
<tbody>
</table>
Try to concatenate the id numbers in foreach loop and pass it where function at once.
Assuming that your id numbers are array(1,2,5,6).
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
if (isset($banner_image_id)) {
$bid = 'IN(';
foreach ($banner_image_id as $id) {
$bid .= $id . ',';
}
$bid = substr($bid, 0, -1) . ')'; //remove last comma and add closing paranthesis.
//The content of the variable $bid would be like
//IN(1,2,5,6)
$data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);
$this->db->where('banner_image_id', $id);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
Another Suggestion:
Convert $banner_image_id to a normal array in case of it is an associative array.
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
$bid = array();
if (isset($banner_image_id)) {
foreach ($banner_image_id as $id) {
$bid[] = $id;
}
$data = array('banner_id' => $this->uri->segment(4),
'banner_image' => $file_name);
$this->db->where_in('banner_image_id', $bid);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
I suggest you to make convertion in the controller section, not in model. The lines ending with // should be in the controller. You can easily pass $bid to edit_banner_image($file_name, $bid) function then.
public function edit_banner_image($file_name, $bid) {
$banner_image_id = $this->input->post('banner_image_id'); //
$bid = array();//
if (isset($banner_image_id)) {//
foreach ($banner_image_id as $id) { //
$bid[] = $id;//
}
$data = array('banner_id' => $this->uri->segment(4),
'banner_image' => $file_name);
$this->db->where_in('banner_image_id', $bid);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
Thus, you will have more readable code.
My model is:
function get_all_pages(){
$query= $this->db->query("SELECT Title FROM news");
return $query->result_array();
}
My view is:
<table cellpadding="0" cellspacing="0">
<tr>
<th>Title</th>
</tr>
<?php
$count=1;
if(!empty($content)) {
foreach ($content as $content1)
{
?>
<tr>
<td align="left"><?php echo $content1['Title']; ?></td>
</tr>
<?php
}
} else{?>
<tr>
<td align="center" colspan="4"><?php echo "No Record Added Yet!!!";?></td>
</tr>
<?php }?>
</table>
My controller is:
public function manage_news()
{
$this->load->model('users_model');
$result_nb = $this->users_model->get_all_pages();
$data['content'] = $result_nb;
$data['page_title']="Manage Pages";
$this->load->view("manage_news",$data);
}
I am using codeigniter. my exact error is:
Call to undefined method users_model::get_all_pages()
how can i fix this error? i want the title row data to appear in a table (from the db)
Q: use Correct naming structure for CI? Which version do u use?
Filename
models/user_model.php
Examples (for demo purpose)
<?php
class User_model extends CI_Model {
// example one
function get_all_pages() {
$query = $this->db->query("SELECT Title FROM news")->result_array();
return ( is_array($query) && sizeof($query) > 0 ? $query : FALSE);
}
// example second
function get_all_pages_second() {
$data = FALSE;
$query = $this->db->query("SELECT Title FROM news");
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
// example third
function get_all_pages_third() {
$data = FALSE;
$query = $this->db->select('Title')->get('news');
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
}
I'm new to codeigniter and followed the guides but seem to be missing something. I have a simple database with customer records in it. My first goal in codeigniter is to simple list all my customers.
here is my controller:
public function index()
{
$this->load->model('HomeModel'); //loads the HomeModel model
$data = $this->HomeModel->function1(); //loads the function (function1) from the model
$this->load->view('index', $data);
}
Here is my model:
public function function1()
{
$query = $this->db->get('work_orders');
return $query->result();
}
here is my view:
<table class="table table-bordered table-striped table-highlight" id="invoice-details">
<thead>
<tr>
<th>Work Order ID</th>
<th>Status</th>
<th>Description</th>
</tr>
</thead>
<?php
foreach ($data->result() as $row);?>
<tr class="even gradeC">
<td><a href="/WorkOrders/viewWo/<?php echo $row['id']; ?>">
<?php echo $row['id']; ?></a></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo Logic\System\Lib\Helper::trunc(htmlentities($row['description']), 8); ?></td>
</tr>
<?php endforeach; ?>
</table>
Change on model :
public function function1()
{
$query = $this->db->get('work_orders');
//return $query->result();
return $query->result_array();
}
Change on controller :
public function index()
{
$this->load->model('HomeModel');
//$data = $this->HomeModel->function1();
$data['result'] = $this->HomeModel->function1();
$this->load->view('index', $data);
}
Change on view :
//foreach ($data->result() as $row);
if(is_array($result)&&!empty($result))
foreach ($result as $row);
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Views : Codeigniter User Guide
And Since you are using array on your view like $row['id'], you got to use result_array on model to return result set array:
This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Generating Query Results : Codeigniter
You can use $this->db->join();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
Produces:
SELECT * FROM blogs
JOIN comments ON comments.id = blogs.id
Go through active record class guide .
Active Record Class
In ur controller,u must pass the values as subarray of data array$data['result'].After which u can simply call it in view as $result .
Controller
$data['result'] = $this->HomeModel->function1();
And In view,
foreach ($result as $row){
<?php echo $row->id; ?>
..
}
Hello I have a problem with my search in CI. In the first page the results are displayed ok, but on second page it shows a 1064 SQL error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND cpt_echip.cpt_echip_nr_inventar LIKE '%%' LIMIT 10 , 10' at line 1
Here is the code (functions that are used):
MODEL:
function search($start, $limit){
$match = $this->input->post('search');
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$q = $this->db->query("SELECT * FROM (cpt_echip)LEFT OUTER JOIN cpt_utl ON cpt_utl.cpt_utl_marca = cpt_echip.cpt_utl_marca WHERE cpt_echip.cpt_tip_echip_id = $tip_echip AND cpt_echip.cpt_echip_nr_inventar LIKE '%$match%' LIMIT $start , $limit");
$rezultat = $q->result();
return $rezultat;
}
function num_filter(){
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$this->db->where('cpt_tip_echip_id', $tip_echip);
$q = $this->db->get('cpt_echip');
$number = $q->num_rows();
return $number;
}
CONTROLLER:
function search(){
$data = $this->helpdesk_model->general();
$start_row = $this->uri->segment(3);
$per_page = 10;
if(trim($start_row) == ""){
$start_row = 0;
}
$config['base_url'] = base_url() . '/index.php/helpdesk/search/';
$config['total_rows'] = $this->helpdesk_model->num_filter();
$config['per_page'] = $per_page;
$config['num_links'] = 10;
$config['first_link'] = 'Prima pagina';
$config['last_link'] = 'Ultima pagina';
$this->pagination->initialize($config);
$data['pagini'] = $this->pagination->create_links();
$data['query'] = $this->helpdesk_model->search($start_row, $per_page);
$this->load->view('rezultat', $data);
}
VIEW:
<table border="1">
<tr>
<th>Nr. Inventar</th>
<th>Nume</th>
<th>Utilizator</th>
</tr>
<?php foreach($query as $row): ?>
<tr>
<td><?php echo anchor('helpdesk/detalii_echipament/'.$row->cpt_echip_nr_inventar, $row->cpt_echip_nr_inventar, array('data-fancybox-type'=>'iframe', 'class'=>'fancybox')); ?></td>
<td><?php echo $row->cpt_echip_nume; ?></td>
<td><?php echo $row->cpt_utl_nume; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $pagini; ?>
Without the search and filter, the pagination works fine.
It means the $_POST array is empty. There will not be anything posted when you navigate using links to the second page. You can pass the search data by url or store in session at first submission and use it.
Just check using print_r($_POST) to see what is there $_POST array.