Codeigniter inserts partial CSV data into MYSQL only id and date - php

Hi i am trying to insert data into mysql table by uploading the CSV. But i am getting an error. the upload shows me error and inserts only the id and date. I dont know the possible reasons for it. Firstly i exported the users table from MYSQL to csv format. Now i upload same using my code but does not work Below is my code :
I did Print_r($file_data);
so got this
Array ( [file_name] => users_(2)3.csv [file_type] => text/plain [file_path] => /var/www/Test/uploads/ [full_path] => /var/www/Test/uploads/users_(2)3.csv [raw_name] => users_(2)3 [orig_name] => users_(2).csv [client_name] => users (2).csv [file_ext] => .csv [file_size] => 3.83 [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => )
The Controller :
function importcsv() {
$data['users'] = $this->csv_m->get_users();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
print_r($_FILES);
$this->load->view('csv', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path))
{
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row)
{
$insert_data = array(
'id'=>$row['id'],
'sip_id'=>$row['sip_id'],
'sip_pass'=>$row['sip_pass'],
'key'=>$row['key'],
'name'=>$row['name'],
'status'=>$row['status'],
'email'=>$row['email'],
'password'=>$row['password'],
'phone'=>$row['phone'],
'balance'=>$row['balance'],
'created'=>$row['created'],
'modified'=>$row['modified'],
'date_inactive'=>$row['date_inactive'],
'reset_date'=>$row['reset_date'],
);
print_r($file_data);
$this->csv_m->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csv', $data);
}
}
The Model :
<?php
class Csv_m extends CI_Model {
function __construct() {
parent::__construct();
}
function get_users() {
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
echo"Nothing To Show";
return FALSE;
}
}
function insert_csv($data) {
$this->db->insert('users', $data);
}
}
The View :
<?php if (isset($error)): ?>
<div class="alert alert-error"><?php echo $error; ?></div>
<?php endif; ?>
<h2>CI Addressbook Import</h2>
<form method="post" action="<?php echo base_url() ?>csv/importcsv" enctype="multipart/form-data">
<input type="file" name="userfile" ><br><br>
<input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
</form>
<br><br>
<table class="table table-striped table-hover table-bordered">
<caption>Address Book List</caption>
<thead>
<tr>
<th>Id</th>
<th>SIP Id</th>
<th>SIP Password</th>
<th>Key</th>
<th>Name</th>
<th>Status</th>
<th>Email</th>
<th>Password</th>
<th>Balance</th>
<th>Created</th>
<th>Modified</th>
<th>Date_Inactive</th>
<th>Date Reset</th>
</tr>
</thead>
<tbody>
<?php if ($users == FALSE): ?>
<tr><td colspan="4">There are currently Users</td></tr>
<?php else: ?>
<?php foreach ($users as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
Please Review my code and help me solve this issue. When i upload the csv i get an error :
Message: Undefined index: id
Message: Undefined index: sip_id
Message: Undefined index: sip_pass
and all other fields. But some how id and date get inserted in table and other fields dont!
The Error I face :

If your model return false the user $users array is empty. So in view you have you check empty
<?php if (!empty($users)): ?>// check empty array
<tr><td colspan="4">There are currently Users</td></tr>
<?php else: ?>
<?php foreach ($users as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>

Message: Undefined index: id Message: Undefined index: sip_id Message: Undefined index: sip_pass
this error occurs when variable is not define or no data related with it
Make sure your $users array names are with this names
$row['id'];
$row['sip_id'];
$row['sip_pass'];
$row['key'];
$row['name'];
$row['status'];
$row['email'];
$row['password'];
$row['phone'];
$row['balance'];
$row['created'];
$row['modified'];
$row['date_inactive'];
$row['reset_date'];
and in view table should be
<table class="table table-striped table-hover table-bordered">
<caption>Address Book List</caption>
<thead>
<tr>
<th>Id</th>
<th>SIP Id</th>
<th>SIP Password</th>
<th>Key</th>
<th>Name</th>
<th>Status</th>
<th>Email</th>
<th>Password</th>
<th>Balance</th>
<th>Created</th>
<th>Modified</th>
<th>Date_Inactive</th>
<th>Date Reset</th>
</tr>
</thead>
<tbody>
<?php if (empty($users)) //use empty()
{
?>
<tr><td colspan = "4" > There are currently Users </td ></tr>
<?php
}
else
{
foreach ($users as $row)
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
and in model
function get_users() {
$query = $this->db->get('users');
$result = $query->result_array();
$count = count($result);
if (empty($count))
{
return $result;
}
else
{
}
}

Related

infusionsoft dsfind().Divide data into 3 chuncks in different pages

data i am fetching from infusion using tag id and dsfind() function. and that data i have to divide into 3 pages. (data used be unique)
<?php
require("isdk.php");
$app = new iSDK;
if ($app->cfgCon("infusionid"))
{
$returnFields = array('Id','Email');
$contacts = $app->dsFind('Contact',14,0,'Groups',7406,$returnFields);
echo '<pre>', print_r($contacts), '</pre>';
}
?>
I want to print this series
<tr>
<td><?php echo $contacts[0]['Id'] ?></td>
<td><?php echo $contacts[0]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[4]['Id'] ?></td>
<td><?php echo $contacts[4]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[7]['Id'] ?></td>
<td><?php echo $contacts[7]['Email']; ?></td>
</tr>
<tr>
<td><?php echo $contacts[11]['Id'] ?></td>
<td><?php echo $contacts[11]['Email']; ?></td>
</tr>
You can use foreach() function e.g:
echo "<table>";
foreach ( $contacts as $var) {
echo "<tr><td>ID: ", $var['Id']."</td>";
echo "<td>Email: ", $var['Email']."</td></tr>";
}
echo "</table>";

Is windows.setTimeout function getting executed in following code?

I want 3 elements of array to be displayed before setTimeout(30000) function and 3 elements after it;so that there is gap of 30 seconds between both the loops.But I am not getting the desired result.Following is the code:
<?php
$info=array(array("name"=>"abc",
"phone"=>"12345"),
array("name"=>"pqr",
"phone"=>"23456"),
array("name"=>"def",
"phone"=>"34567"),
array("name"=>"asd",
"phone"=>"45678"),
array("name"=>"ghj",
"phone"=>"56789"),
array("name"=>"jkl",
"phone"=>"67566"),
);
echo ( $info[0]["name"]);
?>
<body>
<table>
<tr>
<th>id</th>
<th>Name</th>
<th>phone<th>
</tr>
<?php
$i=1;
foreach ($info as $data) {
if($i==4)
{
break;
}
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $data["name"]; ?></td>
<td><?php echo $data["phone"]; ?></td>
</tr>
<?php $i++;}?>
<script>
window.setTimeout(30000);
</script>
<?php
for($j=3;$j<6;$j++)
{
?>
<tr>
<td><?php echo $j+1; ?></td>
<td><?php echo $info[$j]["name"]; ?></td>
<td><?php echo $info[$j]["phone"]; ?></td>
</tr>
<?php } ?>
</table>

Failed to export to .xlsx format

I tried to export my table to .xlsx format. But it is failed to do that. So far I did this:
header('Content-type: application/octet-stream');
header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //should be a correct mimetype
header('Content-Disposition:attachment; filename="contoh-1.xlsx"');
Below is the error:
Here is how I generate the data.
Controller:
public function kategori_excel(){
$paramKategori = $this->input->get('CategoryCode');
$get_url_kategori = $this->ws_url->GetUrl('CategoryRetrieve?CategoryCode='.$paramKategori);
$get_json_kategori = json_decode(file_get_contents($get_url_kategori), true);
$data['itemKategoriToExcel'] = $get_json_kategori['Kategoris'];
$this->load->view('kategori_excel', $data);
}
And here is the view:
<table border='1' width="100%">
<tr>
<th>Kode</th>
<th>Nama</th>
<th>Definisi</th>
<th>Unsur Abstrak</th>
<th>Alias</th>
<th>Sumber Definisi</th>
<th>Tanggal</th>
<th>Tipe Tanggal</th>
<th>Edisi</th>
<th>Role</th>
<th>Other Citation Detail</th>
<th>Nama Katalog</th>
<th>Nama Organisasi</th>
</tr>
<?php foreach($itemKategoriToExcel as $item): ?>
<tr>
<td><?php echo $item['KodeKategori'] ?></td>
<td><?php echo $item['Nama'] ?></td>
<td><?php echo $item['Definisi'] ?></td>
<td><?php echo $item['UnsurAbstrak'] ?></td>
<td><?php echo $item['Alias'] ?></td>
<td><?php echo $item['SumberDefinisi'] ?></td>
<td><?php echo $item['Tanggal'] ?></td>
<td><?php echo $item['TipeTanggal'] ?></td>
<td><?php echo $item['Edisi'] ?></td>
<td><?php echo $item['Role'] ?></td>
<td><?php echo $item['OtherCitationDetail'] ?></td>
<td><?php echo $item['NamaKatalog'] ?></td>
<td><?php echo $item['NamaOrganisasi'] ?></td>
</tr>
<?php endforeach; ?>
</table>
How can I fix this thing(s)?

why Cannot use object of type stdClass as array in

I am trying do pagination,which is working but dont know why my edit and delte functionality stopped working.
This is My Controller
function view($page=0){
$config = array();
$config["base_url"] = base_url() . "index.php/employee/view";
$config["total_rows"] = $this->employee_model->getTotalEmployeeCount();
$config["per_page"] =5;
$this->pagination->initialize($config);
$this->data["results"] = $this->employee_model->getEmployee($config["per_page"], $page);
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('employee_view', $this->data);
}
This is My Model
function getTotalEmployeeCount() {
return $this->db->count_all('user');
}
function getEmployee($limit, $start) {
$this->db->limit($limit, $start);
$this->db->select('id,firstname,lastname,presentadress,contactno,dateofjoining,email');
$qry= $this->db->get('user');
return $qry->result_array();
}
This is my view
<?php
foreach ($results as $m){
?>
<tr style="text-align:center;">
<td><?php echo $m['id'] ?></td>
<td><?php echo $m['firstname'] ?></td>
<td><?php echo $m['lastname'] ?></td>
<td><?php echo $m['dateofjoining'] ?></td>
<td><?php echo $m['presentadress'] ?></td>
<td><?php echo $m['contactno'] ?></td>
<td><?php echo $m['email'] ?></td>
<td>Edit</td>
<td>
<?php
echo anchor('employee/delete_employee/'.$m->id, 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
?>
</td>
<?php
}
?>
<?php echo $this->pagination->create_links()?>
In my model if change result_array to result then it gives error of "Cannot use object of type stdClass as array in".Thats why i used result_array,But now delete and edit functionality not working.I am getting the error of "Trying to get property of non object".
Help me to end this error
If you change result from result_array in model, in view you should access those elements like this.
<td><?php echo $m->id; ?></td>
<td><?php echo $m->firstname; ?></td>
<td><?php echo $m->lastname; ?></td>
Just try like this
<td><?php echo $m->id; ?></td>
<td><?php echo $m->firstname; ?></td>
<td><?php echo $m->lastname; ?></td>
change single line in your view :
<?php
foreach ($results as $m){
?>
<tr style="text-align:center;">
<td><?php echo $m['id'] ?></td>
<td><?php echo $m['firstname'] ?></td>
<td><?php echo $m['lastname'] ?></td>
<td><?php echo $m['dateofjoining'] ?></td>
<td><?php echo $m['presentadress'] ?></td>
<td><?php echo $m['contactno'] ?></td>
<td><?php echo $m['email'] ?></td>
<td>Edit</td>
<td>
<?php
//change here ///
echo anchor('employee/delete_employee/'.$m['id'], 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
?>
</td>
<?php
}
?>
<?php echo $this->pagination->create_links()?>

cant fetch row data from database after search in search form using codeigniter

hey guys i am new in codeigniter and i am working on an application in which i have database and many records in database..i make a search form in which i make a text field in which user can fill any field value and search records.but here is a small problem..when i fill a name in textbox and click on search button no record display on view . . pls help me . . thanks ..
my controller is:
function search()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('inputsearch', 'Search Your Text Here','required');
if ($this->form_validation->run() == TRUE)
{
$this->load->helper('form');
$this->load->helper('html');
$search_this=$this->input->post('inputsearch');
$this->load->model('mod_user');
$searchmember=$this->mod_user->search_member($search_this);
$data['searchmember'] = $searchmember;
var_dump($searchmember);
$this->load->view('view_home',$data);
$this->load->view('view_homemember',$data);
}
else
{
redirect('ctl_home');
}
}
view is:
<form class="userinfo" action="" method="post">
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td width="5%;"> </td>
<td width="5%;"> </td>
<td width="15%;">Name</td>
<td width="15%;">Moderator Name</td>
<td width="20%;">KCC Branch</td>
<td width="15%;">Father/Husband Name</td>
<td width="15%;">Address</td>
<td width="10%;">Date</td>
</tr>
<?php foreach ($rows as $row):?>
<tr>
<td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
<td>Delete</td>
<td><?php echo $row->member_name; ?></td>
<td><?php echo $row->moderator_name; ?></td>
<td><?php echo $row->branch_name; ?></td>
<td><?php echo $row->father_name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php endforeach; ?>
</table>
</form>
my model is:
function search_member($search_this)
{
$query=$this->db->query("SELECT tbl_members.* , tbl_moderators.member_id AS moderator_id, tbl_moderators.member_name AS moderator_name, tbl_moderators.member_no AS moderator_no, tbl_branches.branch_name FROM tbl_members, tbl_members AS tbl_moderators, tbl_branches WHERE tbl_members.moderator_id = tbl_moderators.member_id AND tbl_branches.branch_id = tbl_members.kcc_branch
AND CONCAT(tbl_members.member_name, ' ', tbl_moderators.member_name, ' ',tbl_members.moderator_id, ' ', tbl_moderators.member_id, ' ',tbl_branches.branch_name, ' ',tbl_members.father_name, ' ',tbl_members.address, ' ',tbl_moderators.address, ' ',tbl_members.date, ' ',tbl_moderators.date) like '%".$search_this."%'");
if ($query->num_rows >= 0)
{
foreach($query->result_array() as $row)
{
$data[]=$row;
}
return $data;
}
}
Your loop should have been like this
<?php foreach ($searchmember as $row):?>
<tr>
<td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
<td>Delete</td>
<td><?php echo $row->member_name; ?></td>
<td><?php echo $row->moderator_name; ?></td>
<td><?php echo $row->branch_name; ?></td>
<td><?php echo $row->father_name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php endforeach; ?>
It is $searchmember as $row instead of $rows as $row in foreach
If $data['searchmember'] contains value in controller,
you can access the varibale in view as $searchmember

Categories