Cannot upload csv file using codeigniter - php

I try to import my.csv file but I cannot upload file and find out my problem.
No error show, if I print my code see all empty value don't show anything.
Here is my code:
public function load_data(){
$result = $this->web->select();
// print_r($result);exit;
$output = '
<h3 align="center">Imported User Details from CSV File</h3>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Sr. No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email Address</th>
</tr>
';
$count = 0;
if($result->num_rows() > 0)
{
foreach($result->result() as $row)
{
$count = $count + 1;
$output .= '
<tr>
<td>'.$count.'</td>
<td>'.$row->first_name.'</td>
<td>'.$row->last_name.'</td>
<td>'.$row->phone.'</td>
<td>'.$row->email.'</td>
</tr>
';
}
}
else
{
$output .= '
<tr>
<td colspan="5" align="center">Data not Available</td>
</tr>
';
}
$output .= '</table></div>';
echo $output;
}
public function import() {
$file_data = $this->csvimport->get_array($_FILES["csv_file"]["tmp_name"]);
foreach($file_data as $row){
$data[] = array(
'first_name' => $row["First Name"],
'last_name' => $row["Last Name"],
'phone' => $row["Phone"],
'email' => $row["Email"]
);
}
$this->web->insert($data);
}
How can I upload csv and xlsx file by using codeigniter in easy and simple way?

Related

how to fetch data from single table and show as multiple datatables using codeignator?

I'm fetching the data from the database. Want to show the data into multiple dataTables separated by column name (certification_scope).
This is the labels table in the database
Here is my Controller Clients.php
public function labels_data(){
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$query = $this->db->select('*')->where('clientid',get_client_user_id())->order_by('labelid','desc')->get(db_prefix().'labels');
$data = [];
foreach($query->result() as $r) {
$data[] = array(
$r->certification_scope,
$r->test_evidence,
$r->c_scope_desc,
$r->total_quantity,
$r->current_balance,
$r->project_name,
$r->serial_no_from,
$r->serial_no_to,
$r->used_quantity
);
}
$result = array(
"draw" => $draw,
"recordsTotal" => $query->num_rows(),
"recordsFiltered" => $query->num_rows(),
"data" => $data
);
echo json_encode($result);
exit();
}
This is the view labels.php
<div class="table-wrap">
<table id="item-list" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Certification Scope</th>
<th>Test Evidence</th>
<th>Scope Description</th>
<th>Total Quantity</th>
<th>Current Balance</th>
<th>Project Name</th>
<th>Serial No From</th>
<th>Serial No To</th>
<th>Used Quanity</th>
</tr>
</thead>
<tbody> </tbody>
</table>
</div>
This is the dataTable Ajax
$('#item-list').DataTable({
"ajax": {
url : "<?php echo base_url('clients/labels_data'); ?>",
type : 'GET'
}
});
Result for the above code is
Want to show the DataTables like this
Anyone can please help me to fix this.

How do I display image in the right column in an sql table?

I am inserting values to an sql table and displaying it through AJAX. The values get displayed properly but the image does not come in the right column, instead it comes on top. Why does this happen?
Although the image is shown in the webpage, it is however aligned in different position.
<?php
...
...
...
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>Time</th>
<th>Img</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["Name"].'</td>
<td>'.$row["Time"].'</td>
<td>' ?> <img src="<?php echo $row['Img']; ?>" alt="my picture" height="100" width="100" /> <?php '</td> //Here is the problem where I am trying to display the image
</tr>
';
}
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>Time</th>
<th>Img</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["Name"].'</td>
<td>'.$row["Time"].'</td>
<td> <img src="' .$row["Img"] . '" alt="my picture" height="100" width="100" /> </td> //Here is the problem where I am trying to display the image
</tr>';
}
$output .= '</table></div>';
echo $output;
}
else
{
echo 'Data Not Found';
}
try this way.

Exporting data from MySQL to Excel with PHP

I want to export the data in the gotten from det SQL to a spreadsheet, using PHP.
$sql_export = "SELECT id_employee, firstname, email FROM employees WHERE id_employee = 1";
$export_result = $db->query($sql_export);
This code working for me.Try this code and make some changes as you needed.
**excl_1.php**
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
?>
</table>
<br />
<form method="post" action="export.php">
<input type="submit" name="export" class="btn btn-success" value="Export" />
</form
**export.php**
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if(isset($_POST["export"]))
{
$query = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
?>
Test Result
There are a couple of libraries for that, first and foremost https://packagist.org/packages/phpoffice/phpspreadsheet

Trying to divide a string

I am pulling information from my table. It is setup as a varchar. I can't change it because it 0's everything out when I do. I need to be able to divide the result by 12. It is the MY-TotalPrem that needs to be divided.
I tried
<td>'.$row["MS-TotalPrem" / 12].'
</td>, <td>'.$row["MS-TotalPrem"] / 12.'</td>,
<td>'.$row[int("MS-TotalPrem") / 12].
'</td>
and none work.
function fetch_customer_data($connect)
{
$query = "SELECT * FROM mytable";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>Address</th>
<th>Policy Number</th>
<th>Total Monthly</th>
<th>Expiration Date</th>
</tr>
';
foreach($result as $row)
{
$output .= '
<tr>
<td>'.$row["AC-AcctName"].'</td>
<td>'.$row["PM-PropertyAddr1"].'</td>
<td>'.$row["PO-Addr1"].'</td>
<td>'.$row["PO-PrefixPolNum"].'</td>
<td>'.$row["MS-TotalPrem"].'</td>
<td>'.$row["PO-ExpDate"].'</td>
</tr>
';
}
$output .= '
</table>
</div>
';
return $output;
}
I just want the output to be a division of 12, or any other number I put in. Thank you.
function fetch_customer_data($connect)
{
$query = "SELECT * FROM mytable";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '
<div class="table-responsive">
<table class="table table-striped table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>Address</th>
<th>Policy Number</th>
<th>Total Monthly</th>
<th>Expiration Date</th>
</tr>
';
foreach($result as $row)
$output .= '
<tr>
<td>'.$row["AC-AcctName"].'</td>
<td>'.$row["PM-PropertyAddr1"].'</td>
<td>'.$row["PO-Addr1"].'</td>
<td>'.$row["PO-PrefixPolNum"].'</td>
<td>'.($row["MS-TotalPrem"] / 12).'</td>
<td>'.$row["PO-ExpDate"].'</td>
</tr>
';
$output .= '
</table>
</div>
';
return $output;
}

how to call a php function when we open the page

/*the function which fetches data from database */public function allClients()
{
try
{
$stmt = $this->conn->query("SELECT * FROM client");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
This is the next page where the function is called.I have used a fetch button:'btn-signup' but I want to display this data in a table without using any button how do I do that.
$client2 = new client2();
if (isset($_POST['btn-signup']))
{
try
{
$results=$client2->allClients();
foreach(array ($results) as $row)
{
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
echo $id."\t\t";
echo $name."\t\t";
echo $email;
echo "\n\n";
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
The table in which i need to fetch the values.THANK you
<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<tr>
<td>10238</td>
<td>eduardo#pingpong.com</td>
<td>14.10.2013</td>
</tr>
<tr>
<td>10243</td>
<td>eduardo#pingpong.com</td>
<td>19.10.2013</td>
</tr></tr>
</tbody>
</table>
<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<?php
foreach( array($results) as $row )
{
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
You do the foreach inside your table printing the vars with the proper table markup
So why don't you built your table generic with PHP? As much as I can see you are not doing a AJAX-Request or something like that.
something like:
function table()
{
$client2 = new client2();
if (isset($_POST['btn-signup']))
{
try
{
$results=$client2->allClients();
foreach(array ($results) as $row)
{
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '</tr>';
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
/* Set this line where your static rows now are */
<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<?php table(); ?>
</tbody>
</table>
change
echo $id."\t\t";
echo $name."\t\t";
echo $email;
echo "\n\n";
to
echo "<tr><td>$id</td><td>$name</td><td>$email</td></tr>;
And call the function before the close of the table.
And also, you have an extra < /tr> before closing the body tag
</tr></tbody>
The problem was with the function,this is the correct code.It's now working perfectly
public function allClients()//function
{
try
{
$stmt = $this->conn->query("SELECT * FROM client");
$stmt->execute();
$results = $stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
Function call
<table id="data-table-command" class="table table-striped table-vmiddle">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received" data-order="desc">Received</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
<tbody>
<?php
$results=$client2->allClients();
foreach($results as $row){
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['whatphn']."</td>";
// echo "<td>".$row['email']."</td></tr>";
}
?>
</tbody>
</table>

Categories