how to access the value of blob column(image) in controller laravel - php

I make live search in laravel by ajax jquery
i send html code from controller to view but i can't display the value of blob column
function search (Request $request) {
if($request->ajax())
{ $output='';
$data=Club::all();
foreach($data as $record)
{
$output.='
<tr>
<td > '.$record->name.'</td>
<td > '.$record->country.'</td>
<td ><img src="data:image/png;charset=utf8;base64,{{base64_encode('.$record-
>logo.')}}" ></img></td> //this is uncorrect what is the correct syntax here???
</tr>
';
}
echo json_encode($output);
}
}

Try to return the data rather than display them using echo
json_encode() shall receive object or array, not HTML, return data from your API then format it in front-end.
if($request->ajax())
{ $output='';
$data=Club::all();
return json_encode($data); \\ return rather than display
}

if($request->ajax())
{
$output='';
$data=Club::all();
$path = $record->logo;
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
foreach($data as $record)
{
$output.='
<tr>
<td > '.$record->name.'</td>
<td > '.$record->country.'</td>
<td ><img src="'.$base64.'" ></img></td> //this is uncorrect what is the correct syntax here???
</tr>
';
}
echo json_encode($output);
}

Related

How to convert json value database to html

I have notification table on my database where the value is using like json.
Here's my table
id | touserid | data
1 2 a:1:{i:0;s:10:"INV-000001";}
2 2 a:1:{i:0;s:10:"INV-000003";}
3 2 a:1:{i:0;s:15:"The Mej Hotel";}
4 1 a:5:{i:0;s:28:"Total Goalsi:1;s:7:"6250000";}
5 1 a:1:{i:0;s:10:"INV-000007";}
I want to use that value in html table, but I don't know how to convert the value to html table in codeigniter
Here's my view code
<table class="table table-dark">
<tbody>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Data</th>
<
</tr>
</thead>
<tbody>
<?php foreach($notifications as $notif){ ?>
<tr>
<td><?php echo $notif['id'] ?></td>
<td><?php echo $notif['data'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Here's controller code
$this->db->limit($this->misc_model->get_notifications_limit(), $offset);
$this->db->where('touserid', get_staff_user_id());
$this->db->order_by('date', 'desc');
$data['notifications'] = $this->db->get(db_prefix() . 'notifications')->result_array();
$this->load->view('admin/sales/sales', $data);
But I don't see the data value get into html table like I want, in table it's show the error message " not_goal_message_failedArray"
I'm trying to encode the json, but I still don't know how to pass the json encode in controller to view in codeigniter
Here's the json encode
$page = $this->input->post('page');
$offset = ($page * $this->misc_model->get_notifications_limit());
$this->db->limit($this->misc_model->get_notifications_limit(), $offset);
$this->db->where('touserid', get_staff_user_id());
$this->db->order_by('date', 'desc');
$notifications = $this->db->get(db_prefix() . 'notifications')->result_array();
$i = 0;
foreach ($notifications as $notification) {
if (($notification['fromcompany'] == null && $notification['fromuserid'] != 0) || ($notification['fromcompany'] == null && $notification['fromclientid'] != 0)) {
if ($notification['fromuserid'] != 0) {
$notifications[$i]['profile_image'] = '<a href="' . admin_url('staff/profile/' . $notification['fromuserid']) . '">' . staff_profile_image($notification['fromuserid'], [
'staff-profile-image-small',
'img-circle',
'pull-left',
]) . '</a>';
} else {
$notifications[$i]['profile_image'] = '<a href="' . admin_url('clients/client/' . $notification['fromclientid']) . '">
<img class="client-profile-image-small img-circle pull-left" src="' . contact_profile_image_url($notification['fromclientid']) . '"></a>';
}
} else {
$notifications[$i]['profile_image'] = '';
$notifications[$i]['full_name'] = '';
}
$data = '';
if (!empty($notification['data'])) {
$data = unserialize($notification['data']);
$x = 0;
foreach ($data as $dt) {
if (strpos($dt, '<lang>') !== false) {
$lang = get_string_between($dt, '<lang>', '</lang>');
$temp = _l($lang);
if (strpos($temp, 'project_status_') !== false) {
$status = get_project_status_by_id(strafter($temp, 'project_status_'));
$temp = $status['name'];
}
$dt[$x] = $temp;
}
$x++;
}
}
$notifications[$i]['description'] = _l($notification['description'], $dt);
$notifications[$i]['date'] = time_ago($notification['date']);
$notifications[$i]['full_date'] = $notification['date'];
$i++;
}
echo json_encode($notifications);
Do you know where's my error when tried convert the json value in the table to html table code ?
Thank you
Your data in your table is looking like a serialised array
You will not get the data via echo, should use
$this->load->view("notification_view", $notifications);
instead of
echo json_encode($notifications);

Updating Table or div Data using Ajax Codeigniter

I am trying to create a page where it shows the current chats done by clients.
Now I am using Openfire as Server, and PHP Ajax for scripting.
Openfire dumps data into MySQL Table.
I retrive all the records using Codeigniter-PHP:
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
$this->data['messages'] = $result;
$this->data['subview'] = 'dashboard/test';
$this->load->view('_layout_main', $this->data);
}
Now on my view I have table :
<table class="table table-striped table-bordered table-hover" >
<thead>
<th>From</th>
<th>To</th>
<th>Message</th>
<th>Time</th>
</thead>
<tbody>
<?php if(count($messages)): foreach($messages as $key => $message): ?>
<tr>
<td><?php $users = explode("__", $message->fromJID); echo $users[0];?></td>
<td><?php $tousers = explode("__", $message->toJID); echo $tousers[0]; ?></td>
<td><i class="material-icons">play_circle_filled</i>Message</td>
<td><?php echo $date->format('d-m-y H:i:s'); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
Now I don't want to reload the whole page instead just refresh the table contents every 5 seconds.
I am using DataTables.
I also thought I could pass json to my view, but I don't know how to update it every 5 seconds.
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
if (count($result)) {
$response = json_encode($result);
}
else
{
$response = false;
}
echo $response;
}
Also this is going to send new and older messages.
So I only want to append news messages to table old message should be not repeated
Your backend function :
public function get_chats()
{
$this->db->select('*');
$this->db->from('ofMessageArchive');
$query = $this->db->get();
$result = $query->result();
if (count($result)>0) {
$response['status']= true;
$response['messages']= $result;
$response['date']= date("d-m-y H:i:s", strtotime($your-date-here));
}
else
{
$response['status']=false;
}
echo json_encode($response);
}
You have to make javascript function like this :
<script>
function myAJAXfunction(){
$.ajax({
url:'your url here',
type:'GET',
dataType:'json',
success:function(response){
console.log(response);
var trHTML='';
if(response.status){
for(var i=0;i<response.messages.length;i++){
users =response.messages[i]fromJID.slice('--');
touser=response.messages[i]toJID.slice('--');
trHTML=trHTML+
'<tr>'+
'<td>'+users[0]+'</td>'+
'<td>'+touser[0]+'</td>'+
'<td><i class="material-icons">play_circle_filled</i>Message</td>'+
'<td>'+response.date+'</td>'+
</tr>';
}
$('tbody').empty();
$('tbody').append(trHTML);
}else{
trHTML='<tr><td colspan="4">NO DATA AVAILABLE</td></tr>';
$('tbody').empty();
$('tbody').append(trHTML);
}
},
error:function(err){
alert("ERROR LOADING DATA");
}
});
}
// calling above ajax function at every 5 seconds
setInterval(myAJAXfunction,5000);
</script>

extract a protion of html file [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I have used dompdf to create pdf file, I have used a portion of the html file ie between to generate pdf . (cut & pasted manual way)
since I have a valid pdf out put now, I want to further automate the process,
I want to copy all contents between tables
<table> </table>
to a file, would like to know what would be possible options in php.
any suggestion is highly appreciated
Don't use regex, instead use DomDocument.
The following class will extract out the content between any element. So load your html from your file, or just pass it the contents of ob_get_contents()
<?php
class DOMExtract extends DOMDocument
{
private $source;
private $dom;
public function __construct()
{
libxml_use_internal_errors(true);
$this->preserveWhiteSpace = false;
$this->strictErrorChecking = false;
$this->formatOutput = true;
}
public function setSource($source)
{
$this->source = $source;
return $this;
}
public function getInnerHTML($tag, $id=null, $nodeValue = false)
{
if (empty($this->source)) {
throw new Exception('Error: Missing $this->source, use setSource() first');
}
$this->loadHTML($this->source);
$tmp = $this->getElementsByTagName($tag);
$ret = null;
foreach ($tmp as $v) {
if ($id !== null) {
$attr = explode('=', $id);
if ($v->getAttribute($attr[0]) == $attr[1]) {
if ($nodeValue == true) {
$ret .= trim($v->nodeValue);
} else {
$ret .= $this->innerHTML($v);
}
}
} else {
if ($nodeValue == true) {
$ret .= trim($v->nodeValue);
} else{
$ret .= $this->innerHTML($v);
}
}
}
return $ret;
}
protected function innerHTML($dom)
{
$ret = "";
foreach ($dom->childNodes as $v) {
$tmp = new DOMDocument();
$tmp->appendChild($tmp->importNode($v, true));
$ret .= trim($tmp->saveHTML());
}
return $ret;
}
}
$html = '
<h3>HTML Table Example</h3>
<div>
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
</div>';
$dom = new DOMExtract();
$dom->setSource($html);
echo '
<table cellspacing="0" cellpadding="3" border="0" width="100%">',
//match and return only tables inner content with id=customers
$dom->getInnerHTML('table', 'id=customers'),
//match all tables inner content
//$dom->getInnerHTML('table'),
'</table>';
https://3v4l.org/OkbQW
<table cellspacing="0" cellpadding="3" border="0" width="100%"><tr><th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr><tr><td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr><tr><td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr><tr><td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr><tr><td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr><tr><td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr><tr><td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr></table>
Try This
To Extract Data between tags try this code
Here $source will be your complete html code. And $match will be the data extracted between tags.
Code:
preg_match("'<table>(.*?)</table>'si", $source, $match);
if($match) echo "result=".$match[1];
Reference: Preg match text in php between html tags

Remove image from folder after deleting in codeigniter

I want to delete the image not only in database, but in folder too.
this is my model
public function delete($id)
{
if ($this->db->delete("np_gallery", "id = ".$id))
{
return true;
}
}
this is my controller
public function delete_image($id)
{
$this->np_gallery_model->delete($id);
$query = $this->db->get("np_gallery");
$data['records'] = $query->result();
$this->load->view('admin/gallery/gallery_listing',$data);
}
this is my view
<table class="table table-bordered">
<thead>
<tr>
<td>Sl No</td>
<td>Tag</td>
<td>Image</td>
<td>Action</td>
</tr>
</thead>
<?php
$SlNo=1;
foreach($records as $r)
{
?>
<tbody>
<tr>
<?php $image_path = base_url().'uploads';?>
<td><?php echo $SlNo++ ; ?></td>
<td><?php echo $r->tag; ?></td>
<td><img src="<?php echo $image_path; ?>/images/gallery/<?php echo $r->picture;?>" style=" width:35%; height:100px;"/></td>
<td>
</td>
</tr>
</tbody>
<?php } ?>
</table>
I succeed in deleting the data in the database, but the image in the folder are not also be deleted.
Add some extra code in your controller:
public function delete_image($id)
{
$image_path = base_url().'uploads/images/gallery/'; // your image path
// get db record from image to be deleted
$query_get_image = $this->db->get_where('np_gallery', array('id' => $id));
foreach ($query_get_image->result() as $record)
{
// delete file, if exists...
$filename = $image_path . $record->picture;
if (file_exists($filename))
{
unlink($filename);
}
// ...and continue with your code
$this->np_gallery_model->delete($id);
$query = $this->db->get("np_gallery");
$data['records'] = $query->result();
$this->load->view('admin/gallery/gallery_listing',$data);
}
}
Note: alternativelly, you can do it inside your model delete() method instead. Consider where it better fits your applicaction needs.
Try these
foreach ($query_get_image->result() as $record)
{
// delete file, if exists...
$filename = $image_path . $record->picture;
if (file_exists($filename))
{
unlink($filename);
}
// ...and continue with your code
$this->np_gallery_model->delete($id);
$query = $this->db->get("np_gallery");
$data['records'] = $query->result();
$this->load->view('admin/gallery/gallery_listing',$data);
}

codeigniter - html2pdf (dompdf library) does not show the database results in the generated pdf

I am working with html2pdf (dompdf library), to create a PDF invoice with data from the database.
The pdf is generated with all the formatting css perfectly.
The generated pdf does not show the results of the invoice from the database, in the view I put an "else" statement to see if it was printing actually post something. it is printed empty rows.
The data in the database are there.
Where am I wrong?
Sorry for the bad English.
Thank you.
My Controller:
public function index()
{
//Load the library
$this->load->library('html2pdf');
$this->load->model('model_fatture2');
//Set folder to save PDF to
$this->html2pdf->folder('./assets/pdfs/');
// change name file
$nome_file = "pippo";
//Set the filename to save/download as
$this->html2pdf->filename($nome_file.'.pdf');
//Set the paper defaults
$this->html2pdf->paper('a4', 'portrait');
$data['result_fatture'] = $this->model_fatture2->getFattureUtente();
// test view
$data = array(
'title' => 'PDF Created',
'message' => 'Hello World!'
);
//Load html view
$this->html2pdf->html($this->load->view('pdf', $data, true));
if($this->html2pdf->create('download')) {
//PDF was successfully saved or downloaded
echo 'PDF saved';
}
//$this->output->enable_profiler(TRUE);
}
My Model:
function getFattureUtente() {
$userId = $this->session->userdata('id');
$ProgettoId = $this->session->userdata('id_progetto');
$data = array();
$this->db->select('*');
$this->db->from('fatture');
$this->db->join('users', 'fatture.id_progetto = users.id_progetto');
$this->db->where('id_user',$userId);
$this->db->where('fatture.id_progetto', $ProgettoId);
$query = $this->db->get();
if($query->num_rows() > 0){
foreach ($query->result_array() as $row) {
$data[] = $row;
}
$query->result();
return $data;
}
}
My View
<?php if (!empty($result_fatture)): ?>
<?php foreach ($result_fatture as $row): ?>
<?php if ($this->session->userdata('id') === $row['id']): ?>
<table height="20" border="0" cellpadding="0" cellspacing="0" class="table_riga">
<tr>
<td class="col-dx"><?=$row['id_fatture']?></td>
<td class="col-dx"><?=$row['id_user']?></td>
<td class="col-dx"><?=$row['id_progetto']?></td>
<td class="row-bottom"><?=$row['n_progetto']?></td>
<td class="col-sx"><?=$row['data_emissione']?></td>
<td class="col-sx"><?=$row['n_fattura']?></td>
<td class="col-sx"><?=$row['prezzo']?></td>
</tr>
</table>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<?php echo "rows empty"; ?>
<?php endif; ?>
SOLVED! I'M NOT HAD DECLARED THE SESSIONS, SO THE VIEW session-> userdata ('id') === $ row ['id']):?> RETURN FALSE. If it can help someone. thanks

Categories