Array into function (export xls file) - php

I do not understand how to pass all the ARRAY within all the columns in order to export them as an xls file.
I was thinking about something like this:
<a href='".site_url('main_page/EXPORT')
Take a look:
I am fetching all the different rows from a table and they are displayed in the screen, check it out (it does work) - this part is inside page_view (VIEW folder):
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 align="center">TABLE: USERS</h2>
<table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="table">
<thead>
<th>id</th>
<th>user</th>
<th>name</th>
<th>lastname</th>
<th>type</th>
<th>status</th>
<th>date</th>
</thead>
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
Take a look at this (this is probably wrong)
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
<a href='".site_url('main_page/EXPORT')."/$record->id'> //THIS IS POBABLY WRONG
<button type='button' class='btn btn-primary'>EXPORT</button></a>
</tr>";
</tbody>
Then, I want to pass that information inside this function:
public function EXPORT(){
$data['records']=$this->MODEL->EXPORT_XLS();
$this->load->view('page_view',$data);
Could you please help me?

Related

How can I display values from a table that correspond to their matching id

So in this query below im joining two tables through order_id and displaying the all the values from the user_orders table.
As per the image below I am trying to display only the order_Id rows that match the order_manager table.
public function getUserOrder(){
$sql = "SELECT user_orders.order_id,
user_orders.title, user_orders.price,
user_orders.quantity
FROM order_manager
JOIN user_orders ON order_manager.order_id = user_orders.order_id;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
while ($result = $stmt->fetchAll()){
return $result;
}
}
I have attempted to use an if statement that appears to do something however it gives me the values that don't match order id in reverse.
<div class="container mt-5">
<?php $artworks = new Artworks(); ?>
<div class="row">
<div class="col-lg-12">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Order ID</th>
<th scope="col">Full Name</th>
<th scope="col">Phone</th>
<th scope="col">Address</th>
<th scope="col">Orders</th>
</tr>
</thead>
<?php
$artworks->getOrder();
foreach ($artworks->getOrder() as $art) {
echo "<tbody>
<tr>
<td>$art[order_id]</td>
<td> $art[full_name]</td>
<td> $art[phone] </td>
<td>$art[address]</td>
<td>
<table class= 'tale text-center table-dark'>
<thead>
<tr>
<th scope='col'>Order ID</th>
<th scope='col'>title</th>
<th scope='col'>price</th>
<th scope='col'>Quantity</th>
</tr>
<thead>
<tbody>
<tr>";
$artworks->getUserOrder();
foreach ($artworks->getUserOrder() as $order) {
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
}
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>";
}
echo "
</tbody>
</table>
</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
Here is an image to help explain the desired output
Solved moving the td items into the if statement. Guess it was mostly correct.
$artworks->getUserOrder();
foreach($artworks->getUserOrder() as $order)
{
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>
";
}
}

How to retrieve data from database using Ajax and OOP php

I am going to develop web page with ajax and php. but there is problem in my get_data_in_table function. it will show data in a table structure. but it shows error when i run this code. others are working properly. it shows error in while loop. but i am unable to find any issue.
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
?>
I guess I found it:
class get_data()
{
public function get_data_in_table($query){
$output='';
$result= $this->Getdata($query);
$output.='
<table class="table table-bordered table-striped">
<tr>
<th>Countty</th>
<th>Airline Name</th>
<th>Arrival Time</th>
<th>Status</th>
<th>Comment</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output .='
<tr>
<td>'.$row->country.'</td>
<td>'.$row->air_line.'</td>
<td>'.$row->arrival_time.'</td>
<td>'.$row->status.'</td>
<td>'.$row->comment.'</td>
</tr>';
}
$output.='</table>';
return $output;
}
} // This was missing
?>

Issue trying to fetch data codeigniter

I got this errors in the view file (and so many more about the indexes id,username, name, lastname, password, type, status, date):
https://i.gyazo.com/d8bda30b1fafce47ed2125d590c5b4e4.png
I gotta show ONLY the rows that contain "simple" users ("type = 1") which are stored inside the table "usuarios"
Table "usuarios".
"usuarios" includes:(id,username,name,lastname,password,type,status,date).
When i enter the system as an ADMIN the program must show a table with all the "simple" users stored in the table "usuarios".
I need something like this:
https://i.gyazo.com/36f11b368a339964f1b734cea0177734.png
This is my code:
My view file ("user_view"):
<table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
<thead>
<th>id</th>
<th>User</th>
<th>Name</th>
<th>Lastname</th>
<th>Password</th>
<th>Type</th>
<th>Status</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$records['id']."</td>
<td>".$records['username']."</td>
<td>".$records['name']."</td>
<td>".$records['lastname']."</td>
<td>".$records['password']."</td>
<td>".$records['type']."</td>
<td>".$records['status']."</td>
<td>".$records['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My model function:
public function getINFOUSER(){
$query = $this->db->get_where('usuarios',array('type'=>1));
if ($query->num_rows() > 0 ) {
$result = $query->result_array();
}
return $result;
}
Do not know what to do now :S
It is record, not records inside foreach:
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['password']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
Hope this will work.
There is some error in your code. you have written wrong variable name. I just corrected it.
<?php
if (count($records) > 0 && $records != false)
{
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['username']."</td>
<td>".$record['name']."</td>
<td>".$record['lastname']."</td>
<td>".$record['password']."</td>
<td>".$record['type']."</td>
<td>".$record['status']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>

bootstrap display data from mysql in table

I am trying to display my mysql rows to html bootstrap table. Database connection is working, displaying data is working, but it is not fancy as i want. I'd like to maybe save in arrayData maybe and then print this arrayData in html tag. Please any suggestions much appreciated. I want to do this easiest way and most convenient for editing later on. php code to display data :
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["lastName"]. "<br>"; /* and so on..*/
}
} else {
echo "0 results";
}
and this is my html code for bootstrap
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>ales</td>
<td>king</td>
<td></td>
<td></td>
</tr>
<tr>
<th scope="row">2</th>
<td>love</td>
<td>2</td>
<td>code</td>
<td></td>
</tr>
</tbody>
</div>
</table>
EDIT: i want this, but loaded from database not manually typed :)!
You can use a php loop this way
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<?php
....
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' . $row["id"]. '</td>
<td>' . $row["name"] .'</td>
<td> '.$row["lastName"] .'</td>
</tr>';
}
} else {
echo "0 results";
}
?>
</tbody>
</div>
</table>
I would do something like this:
( but you should really read some basic PHP )
<?php
echo "<table>";
// table header
echo "<tr><th>id</th><th>Name</th><th>Lastname</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["lastName"]."</td></tr>";
}
// table footer
echo "</table>";
?>

laravel-excel not working to export xls file using blade with image

Excuse me,
I want to use laravel-excel to export my report.
This is the controller:
public function getExcelfile(){
$users = UserService::findAllPublic();
$total = UserService::count();
$total_with_photo = UserService::countWithPhoto();
Excel::create('excelfile', function($excel) use ($users, $total, $total_with_photo) {
$excel->sheet('Excel', function($sheet) use ($users, $total, $total_with_photo) {
$sheet->loadView('report.excel')->with("users", $users)->with("total", $total)->with("total_with_photo", $total_with_photo);
});
})->export('xls');
}
This is the excel.blade.php located at report folder:
<html> <body> Total Registered User : {{$total}} Total Registered User With Photo : {{$total_with_photo}} <h1>Data User</h1> #if(!empty($users)) <table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Photo</th>
<th>Nama</th>
<th>Struck</th>
<th>Email</th>
<th>Phone</th>
<th>FB ID</th>
<th>Timestamp</th>
<th>Publish</th>
</tr>
</thead>
#foreach ($users as $row)
<tr>
<td>{{$row->id}}</td>
<td><img src="{{URL::asset('assets/images/upload/' . $row->photo)}}" width="200px"/></td>
<td>{{$row->nama}}</td>
<td>{{$row->struck}}</td>
<td>{{$row->email}}</td>
<td>{{$row->phone}}</td>
<td>{{$row->fbid}}</td>
<td>{{$row->timestamp}}</td>
<td>{{$row->publish}}</td>
</tr>
#endforeach </table> #endif </body> </html>
Then this error appears:
PHPExcel_Exception
File http://myserver.com/projectname/public/assets/images/upload/DSCN1164.jpg not found!
That file is exist when I try to access it at browser.
Instead of:
<img src="{{URL::asset('assets/images/upload/' . $row->photo)}}"
you should use:
<img src="assets/images/upload/{{{ $row->photo }}}"
Of course assuming it's correct path

Categories