I've a problem about this illegal string offset
This is the controller named formlembur.php
function download($lid)
{
$form_lembur = $this->form_lembur->read(array('lid' => $lid));
$data['form_lembur'] = $form_lembur;
//echo $data;die;(until this statement,it can load the data on database.
//it actually showing the data.
$this->load->view('exp-lembur', $data);
}
This is the model named form_lembur.php
public function read($where = FALSE)
{
if ($where === FALSE)
{
$query = $this->db->get('form_lembur');
return $query->result_array();
}
$query = $this->db->get_where('form_lembur', $where);
return $query->row_array();
}
and this is the view before i download
<td> <input type="submit" name="download" value="Download"></td>
and this is the view where i want the data viewed on excel named exp-lembur.php
<?php
header("Content-type: application/vnd.openxmlformats xlsx");
header("Content-disposition: attachment; filename=\"Lembur.xls\"");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=ProgId content=Excel.Sheet>
<title>Reports</title>
<style>
table.member
{
border:.5pt solid #000;
border-collapse:collapse;
font:14px tahoma;
margin:0 auto;
width:50%
}
table.member tr th{background-color:#999;border:.5pt solid #000}
table.member tr.head th{color:#fff}
table.member tr.data td{border:.5pt solid #000; vertical-align: top}
</style>
</head>
<body>
<?php
//print 'Generated On ' .date("j M y, g:i a",time()). '<br>';
?>
<table class="member">
<thead>
<tr class="head">
<th>Tanggal</th>
<th>Nama</th>
<th>Jabatan</th>
<th>Jam Pulang</th>
<th>Keperluan</th>
<th>Instruktur</th>
<th>Status</th>
<th>Uang Lembur</th>
</tr>
</thead>
<tbody>
<?php
if (isset($form_lembur)){
foreach($form_lembur as $k){
print <<<h
<tr class="data">
<td>{$k['date']}</td>
<td>{$k['nama']}</td>
<td>{$k['jabatan']}</td>
<td>{$k['jampulang']}</td>
<td>{$k['keperluan']}</td>
<td>{$k['instruktur']}</td>
<td>{$k['status']}</td>
<td>{$k['uanglembur']}</td>
</tr>
h;
}
}
?>
</tbody>
</table>
</body>
</html>
but the problem is, it said that its an illegal string offset.
what is the problem ?
Please help me.
this is the error message ( showed on excel document because i try to download it into an excel document )
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'date'
Filename: views/exp-lembur.php
Line Number: 50
The problem is in the model. It sometimes returns
$query->result_array()
and other time returns
$query->row_array()
depends on $where value. But result_array() gives this format:
$row[0]['date'];
when row_array() gives this format:
$row['date'];
That's why you have "Illegal string offset 'date'".
Related
The datatable cant display the searching and sorting, may I know why?
The problem is, the searching, sorting didn't display. But when I use HTML code 100% then it will display, but when I use data that fetch from MySQL, it didn't display the searching and sorting but it displays the table. I'm new to this and still a student. This is where the js and css link I take
datatable
Table for datatype:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Content filter for IHSR</title>
<!--Link from datatable for sort,paging,search-->
<link href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
</head>
<body >
<?php
include('connection.php');
// select column from table ihsr_facebook_content
$query = "SELECT * FROM customer ORDER BY Customer_Name ASC";//later change to DESC
// get results from database
$result = mysqli_query($connect, $query);
// display data in table
if(mysqli_num_rows($result)>0)
{
$output = '<div>
<table id="example" class="display" style="width:100%">
<thead class="table-dark">
<tr>
<th style="padding-left: 10px; padding-right: 20px;" >No.</th>
<th style="padding-left: 10px; padding-right: 20px;" colspan="2">Name</th>
<th style="padding-left: 10px; padding-right: 20px;" >Email</th>
<th style="padding-left: 10px; padding-right: 20px;" >Contact</th>
<th style="padding-left: 10px; padding-right: 20px;" >Address</th>
<th width="7%">Edit</th>
<th width="9%">Delete</th>
</tr>
</thead>
<tbody>';
$num = mysqli_num_rows($result);
$count= 1 ;
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
$Customer_ID = $row['Customer_Id'];
$Customer_Name = $row['Customer_Name'];
$Customer_Email = $row['Customer_Email'];
$Customer_Contact = $row['Customer_Contact'];
$Customer_Address = $row['Customer_Address'];
// echo out the contents of each row into a table
$output.='<tr>
<td>'.$count.'</td>
<td colspan="2">'.$Customer_Name.'</td>
<td>'.$Customer_Email.'</td>
<td>'.$Customer_Contact.'</td>
<td>'.$Customer_Address.'</td>
<td><a class="btn-1" href="product_edit.php?GetEdit='.$Customer_ID.'" id="myBtn">Edit</a></td>
<td><button type="button" class="delButton btn-1" data-toggle="modal" id="'.$Customer_ID.'" data-target="#myModal" >Delete</button></td>
</tr>';
$count++;
}
}
// close table>
echo $output.'</tbody></table></div>';
?>
<script>
$(document).ready(function() {
$('#example').DataTable();
} );
</script>
</body>
</html>
Error message:
jquery error jquery-3.5.1.js:4046 jQuery.Deferred exception: Cannot set properties of undefined (setting '_DT_CellIndex') TypeError: Cannot set properties of undefined (setting '_DT_CellIndex')
Since I am completely new in Laravel, I have a problem pretty complicated for me.
I have controller file ApiHandlerController like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Client;
class ApiHandlerController extends Controller
{
//
public function index()
{
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'http://jsonplaceholder.typicode.com/todos',
]);
$response = $client->request('GET', '/todos');
//get status code using $response->getStatusCode();
$body = $response->getBody();
$arr_body = json_decode($body);
return view('myview', $arr_body);
}
}
?>
myview.blade.php as a view file:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Todo</title>
<link rel="shortcut icon" type="image/x-icon" href="icon.ico">
<meta name="description" content="HTML, PHP, JSON, REST API">
<style>table, th, td {border: 1px solid black;}</style>
</head>
<body>
<table style="width:100%"; class="data-table">
<tr>
<th>User ID:</th>
<th>ID:</th>
<th>Title:</th>
<th>Completed</th>
</tr>
<?php foreach ($arr_body as $value) { ?>
<tr>
<td><?php echo $value -> userId ?> </td>
<td><?php echo $value -> id ?> </td>
<td><?php echo $value -> title ?> </td>
<td><?php echo $value -> completed ?> </td>
</tr>
<?php } ?>
</table>
And web.php file with route:
Route::get('myview', function () {
return view('myview');
});
My question is:
How to show result of todos from jsonplaceholder.typicode.com in this blade (view) file?
Currently, it's not possible, and every time I try this is an error:
ErrorException
Undefined variable: arr_body (View: C:\xampp\htdocs\TodoList\resources\views\myview.blade.php)
Thank you guys in advance!
To show array type data in blade...
First edit your controller code.
From:
return view('myview', $arr_body);
To:
return view('myview')->with('arr_body', $arr_body);
Below is core php syntex.
<td><?php echo $value['userId'] ?> </td>
Laravel syntex
<td>{{$value['userId']}}</td>
Hope this will be useful.
Am working on a Laravel API application whereby I have some dynamic data. I am looping through the data (which is an array) inside the html. The html is embedded in a PHP variable using herodoc format.
After populating the dynamic variables in the html , am storing the content in a variable and convert it to base64 format and return as a response.
The problem is the PHP variables inside the HTML arent being iterated correctly and are throwing errors: ErrorException
Array to string conversion
My PHP Function
public function showPDF($data){
$names = $data[0];
$amount = $data[1];
//Embedded HTMl whereby am populating the above variables
$my_var = <<<EOD
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title> PDF Output| </title>
<style type="text/css">
body{
font-size: 13px;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Benefits</th>
<th> Benefit Limit</th>
<th> Cover</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($names as $name){ ?>
<td> <?php echo $name ?> </td>
<?php } ?>
<td> 14-05-2019 </td>
</tr>
</tbody>
</table>
</body>
</html>
EOD;
dd($my_var);
//Convert Final PDF to base64
$b64Doc = chunk_split(base64_encode(file_get_contents($my_var)));
return $b64Doc;
}
If your data is a linked list you can`t call it as array first check which data type is in
names = data[0]
Okay, so I am exporting the same information in two different ways...'html' and 'xlsx'. In order to get up and down arrows in my xlsx I did the following:
$upArrow = html_entity_decode('↑',ENT_QUOTES,'UTF-8');
$downArrow = html_entity_decode('↓',ENT_QUOTES,'UTF-8');
This works great. However, this does not work for the HTML export so i did the following:
$upArrow = ($_REQUEST['t'] == 'html') ? '↑' : html_entity_decode('↑',ENT_QUOTES,'UTF-8');
$downArrow = ($_REQUEST['t'] == 'html') ? '↓' : html_entity_decode('↓',ENT_QUOTES,'UTF-8');
However, now I am getting the literal string ↓ instead of ↓.
The HTML Writer will treat strings for worksheet cells as strings, not as html entities, because that's what it's expecting from PHPExcel.... use the UTF-8 up and down arrow characters, and that should work for all writers
EDIT
include 'PHPExcel.php';
$phpe = new PHPExcel();
$sheet = $phpe->getActiveSheet();
// Write a UTF-8 up-arrow character to cell A1
$sheet->setCellValue(
'A1',
html_entity_decode('↑',ENT_QUOTES,'UTF-8') . ' up arrow'
);
// Write a UTF-8 down-arrow character to cell A2
$sheet->setCellValue(
'A2',
html_entity_decode('↓',ENT_QUOTES,'UTF-8') . ' down arrow'
);
// Save as an OpenOfficeXML .xlsx file
$writer = new PHPExcel_Writer_Excel2007($phpe);
$writer->save('./test.xlsx');
// Save as a BIFF-8 .xls file
$writer = new PHPExcel_Writer_HTML($phpe);
$writer->save('./test.html');
// Save as an HTML file
$writer = new PHPExcel_Writer_Excel5($phpe);
$writer->save('./test.xls');
EDIT 2
The Markup generated is:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Generated by PHPExcel - http://www.phpexcel.net -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Spreadsheet</title>
<meta name="author" content="Unknown Creator" />
<meta name="title" content="Untitled Spreadsheet" />
<meta name="company" content="Microsoft Corporation" />
<style type="text/css">
html { font-family:Calibri, Arial, Helvetica, sans-serif; font-size:11pt; background-color:white }
table { border-collapse:collapse; page-break-after:always }
.gridlines td { border:1px dotted black }
.b { text-align:center }
.e { text-align:center }
.f { text-align:right }
.inlineStr { text-align:left }
.n { text-align:right }
.s { text-align:left }
td.style0 { vertical-align:bottom; border-bottom:none #000000; border-top:none #000000; border-left:none #000000; border-right:none #000000; color:#000000; font-family:'Calibri'; font-size:11pt; background-color:white }
table.sheet0 col.col0 { width:42pt }
table.sheet0 tr { height:15pt }
table.sheet0 tr.row0 { height:15pt }
table.sheet0 tr.row1 { height:15pt }
</style>
</head>
<body>
<style>
#page { left-margin: 0.7in; right-margin: 0.7in; top-margin: 0.75in; bottom-margin: 0.75in; }
body { left-margin: 0.7in; right-margin: 0.7in; top-margin: 0.75in; bottom-margin: 0.75in; }
</style>
<table border="0" cellpadding="0" cellspacing="0" id="sheet0" class="sheet0 gridlines">
<col class="col0">
<tbody>
<tr class="row0">
<td class="column0 style0 s">↑ up arrow</td>
</tr>
<tr class="row1">
<td class="column0 style0 s">↓ down arrow</td>
</tr>
</tbody>
</table>
</body>
</html>
The purpose of this script:
Write a script that counts from 1 to 10 in steps of 1. For each number, display whether that
number is an odd or even number, and also display a message if the number is a prime number.
Display this information within an HTML table.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Exercise 1</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
th { text-align: left; background-color: #999; }
th, td { padding: 0.4em; }
tr.alt td { background: #ddd; }
</style>
</head>
<body>
<h2>Exercise 1</h2>
<table cellspacing="0" border="0" style="width: 20em; border: 1px solid #666;">
<tr>
<th>Number</th>
<th>Parity</th>
<th>Primality</th>
</tr>
<?php
$n=10;
for ($i=1;$i<=$n;$i++){
echo ($i%2 != 0)? '<tr class = "alt">':'<tr>'; ?>
<td><?php echo $i; ?></td>
<td><?php echo ($i%2 != 0)? "Odd":"Even";?></td>
<td><?php
$k=0;
for ($j=1;$j<=$i;$j++){
if ($i%$j=0) $k++; //Where the error occurs
}
echo ($k=2 || $k=1)?"Prime":"Composite";?>
</td></tr>
<?php
}
?>
</table>
</body>
</html>
$i%$j=0
Assigns $j as 0 and then tries to do $i % 0;
You want
($i % $j) == 0
There's another mistake in the same way with the echo.
Consider also formatting your code with spaces to make it more readable.
if ($i%$j=0) should probably be if ($i%$j==0)