Show image in slider Codeigniter - php

Hi i use codeigniter to simple listing script. I have a multi images upload for each listing 5 images...
But i have a problem with showing in listing details page....
i have two tables properties and media In media has 3 column id property_id (same with properties.id and photo (this is image name + extension)
Controller: Properties.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Properties extends My_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Property');
}
public function details($slug){
$this->data['listing'] = $this->Property->find_by_slug($slug);
$this->load_theme('properties/details');
}
}
Model: Property.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Property extends CI_Model{
var $table = 'properties';
function find_by_slug($slug){
$this->db->select('properties.*,users.*,media.photo');
$this->db->join('users', 'users.id = properties.user_id');
$this->db->join('media', 'media.property_id = properties.id');
$this->db->where('slug',$slug);
return $this->db->get($this->table,1)->row_array();
}
}
View: details.php
<div class="carousel-inner">
<div class="item active">
<?php $x = 0; ?>
<?php if($listing):?>
<?php foreach ($listing as $m):?>
<?php $x++ ; ?>
<div class="item <?php if ($x == 1) echo 'active' ?>">
<img src="<?php echo site_url('files/listing/'.$m['photo'])?>" class="thumb-preview" alt="<?php echo $m['alt'];?>">
</div>
<?php endforeach;?>
<?php endif;?>
</div>
I can not understand why it does not show the images in the slider...

If in config, site_url() can have index.php at the end. That will break your image url.
If you need to access your resources, always use base_url().
Change:
site_url('files/listing/'.$m['photo']);
To:
base_url('files/listing/'.$m['photo']);

I think the problem is that it can not find the appropriate id

Related

How to generate PDF Programmatically with my company logo using PHP codeigniter

I am trying to create a PDF with my company logo using the dompdf library but I have a PDF without my logo. I can try the following code.
Welcome Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('pdf');
}
public function index()
{
$html = $this->load->view('welcome_message', [], true);
$this->pdf->createPDF($html, 'mypdf', false);
}
}
Welcome_view:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<img src="<?php echo base_url() ?>/img/un6.png'">
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
and also included dompdf files application/libraries folder:
PDF.php file for dompdf file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');
class Pdf
{
function createPDF($html, $filename='', $download=TRUE, $paper='A4', $orientation='portrait'){
$dompdf = new Dompdf\DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper($paper, $orientation);
$dompdf->render();
if($download)
$dompdf->stream($filename.'.pdf', array('Attachment' => 1));
else
$dompdf->stream($filename.'.pdf', array('Attachment' => 0));
}
}
?>
How can I do this
The problem is here:
<img src="<?php echo base_url() ?>/img/un6.png'">
DomPDF needs all external resources to be referenced as file paths, not URLs.
You could, for instance try this:
<img src="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/img/un6.png'">
or also something like:
<img src="<?php echo FCPATH; ?>/img/un6.png'">
(FCPATH is Codeigniter's front controller path... the server path to the main index.php file that orchestrates everything in CI)
DomPDF requires base64 encoded images in order to display it properly in PDF format.
Convert the image to base64 using this tool https://www.base64-image.de/
It will give a html tag, just use it in your HTML PDF code

Delete file a file on the server(not local) with CodeIgniter(3)

I have a view named home_view, where there are several lists, for example libri(books).The user can upload and delete the files.
This is a snippet of home_view.php
<?php
echo "<table>";
echo "<tbody>";
echo "</br>";
foreach ($libri as $row):
?>
<tr>
<td>
<div>
</br>
<img src="<?php echo base_url('Immagini/book.png'); ?>" />
<a class="pdf" data-fancybox-type="iframe" rel="group" href="<?php echo base_url($row['Url_suffix']) ?>"><?php echo $row['Nome']; ?> </a>
<a class="deleteUser" href="javascript:void(0)" rel="<?php echo site_url('libro/elimina/'.$row['ID']) ?>"><img src="<?php echo base_url('Immagini/button_close.png'); ?>"/></a>
</div>
</td>
</tr>
<?php
endforeach;
?>
libri is a table of Mysql db and have different columns, Url_suffix is a Varchar(255) column where there's the folder/filename.pdf. IN the second anchor I delete with success, the row from the DB, but not the file. I tried to do something like this
<a class="deleteUser" rel="<?php unlink($row['Url_suffix']); ?>"><img src="<?php echo base_url('Immagini/button_close.png'); ?>"/></a>
but without success. What I'm wrong?
Update:
controller libro.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Libro extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper("file");
}
public function elimina($libri_id) {
//$libri_ID = $this->libri->get_one($libri_id);
$result=$this->libri->did_delete_row($libri_id);
redirect(site_url('admin/dashboard'), 'refresh');
}
}
?>
You can't use "unlink" in that way, basically you need to process which you want to delete on your controller, with some help of a model, example:
public function delete_file($route, $file){
unlink($route . "/" . $file);
}
In a model so you can use it anywhere, the $route parameter will be the path to your directory where the file is stored, for example:
htdocs/website/static/books/user/
And the $file parameter will be the name with extension that you want to delete, like:
myfirstbook.pdf
then you will have the full route to the file and it will be deleted, you only have to call that funcion from the models in your controller where the link was given by that anchor, like
Delete file
And your controller will have something like
public function delete_file($user, $file){
$this->load->model('YourModel', 'yourmodel');
$path = FCPATH . "static/books/user/".$user;
$this->yourmodel->delete_file($path, $file);
}
And its done.
Hope it helps.

How to flush output using Codeigniter 3?

I'm writing a CodeIgniter 3 application. My goal is to have a view, which is constantly (as the code goes along) flushed with the output content. I read some answers in stackoverflow, but I am not sure, how to do this.
I have a Controller wich renders the view, in the update method.
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class Dataupdate extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->render('dataupdate_list_view');
}
public function update() {
$this->render('dataupdate_update_view');
}
}
?>
Here is the class "MY_Controller".
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
$this->data['page_title'] = 'Team Portal';
$this->data['before_head'] = '';
$this->data['before_body'] = '';
}
/**
* Render method is used to render a view using a template.
* The given template delivers the HTML header and footer.
* The view contains the actual page content.
*
* #param string $the_view The view to be rendered
* #param string $template The template to render the view
*/
protected function render($the_view = NULL, $template = 'master') {
if ($template == 'json' || $this->input->is_ajax_request()) {
header('Content-Type: application/json');
echo json_encode($this->data);
} else {
// Get current user from database
$this->load->model('user_model');
$user = $this->user_model->get_record_by_name($_SERVER['REMOTE_USER']);
// Data to pass to view
$this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
$this->data['user'] = $user;
// Load view with data
$this->load->view('templates/' . $template . '_view', $this->data);
}
}
}
?>
Then I have a view, which outputs the content.
<div>
<!-- PAGE TITLE -->
<div class="page-title">
<h3>Daten Update</h3>
<div class="title_right">
</div>
</div>
<div class="clearfix"></div>
<!-- ALERTS -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Fehler<small>Kleiner Text</small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content bs-example-popovers">
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<strong>Strong text.</strong>What to do now?
</div>
</div>
</div>
</div>
</div>
<!-- CONTENT -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Neues Daten Update ausführen</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<?php echo form_open('dataupdate/update'); ?>
<input type="text" name="test" />
<button>Ausführen</button>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
This works fine. So far.
Now, what I try to achieve is the following:
I click on the "Ausführen" (Execute) button, which submits the form to the same url as this page
Then I want a php script to execute, which continously outputs content to the page. Not all content at once, but adds output after output to the page.
I hope I made myself clear.
Any suggestions or tutorials, on how to do that?
You'll need to force the output by calling ->_display method on the output class
For example for JSON output you'll do something like this:
$response = array('status' => 'OK');
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
Same thing goes when you load a view, if you're 100% sure you won't need anything else, You may call _display manually to get the output; just be sure to call exit after that otherwise you'll end-up with a duplicated output (one from your manual call & the other from the automatic call).
Have a look at the user's guide for output class: http://www.codeigniter.com/user_guide/libraries/output.html

OpenCart: How to make a global variable?

I am trying to make two global variables within OpenCart. I basically want to be able to declare them in any of my .tpl files
<?php echo $global1; ?>
I have tried editing, library/system.php and also config.php by adding $global1="test" inside my files. However calling that in .tpl files is not working?
Example, look at the file below, I want to be able to call these variables anytime.. do I have to edit config.php or what?? The example shows the $header call which is used on every .tpl file.
not_found.tpl
<?=$header?>
<div class="breadcrumb">
<? foreach ($breadcrumbs as $breadcrumb) { ?>
<? $breadcrumb['separator']; ?><?=$breadcrumb['text']?>
<? } ?>
</div>
<div id="content">
<?=$global1?>
<img src="/catalog/view/theme/default/image/error.png"/>
</div>
<?=$footer?>
Updated
/catalog/controller/common/header.php
<?php
class ControllerCommonHeader extends Controller {
protected function index() {
// NEW GLOBAL VARS
$cdnDefault="//www.gorgeouscouturedev.com/catalog/view/theme/";
$currentUseLang = $this->language->get('code');
And now in /catalog/view/theme/default/template/common/home.tpl
<?=$header?>
<?=$column_left?>
<?=$column_right?>
<div id="content">
<? echo $cdnDefault ?>
<? echo $currentUseLang ?>
<?=$content_top?>
<div class="flexslider">
<ul class="slides">
<li><img src="catalog/view/theme/default/image/desktop.png"/></li>
<li><img src="catalog/view/theme/default/image/blogger.png"/></li>
</ul>
</div>
<?=$content_bottom?>
</div>
<?=$footer?>
And the errors:
Notice: Undefined variable: cdnDefault in /catalog/view/theme/default/template/common/home.tpl on line 6
Notice: Undefined variable: currentUseLang in /catalog/view/theme/default/template/common/home.tpl on line 7
If you are just wanting to use a static value, you can just use a constant. Simply create one in your config.php file(s) such as
define('CDN_URL', 'http://cdn.someurl.com/');
You can then use
<?php echo CDN_URL; ?>
anywhere in your application. If you want to code it like you have in your edited question, th fundamental flaw with your code is that you are using $cdnDefault instead of $this->data['cdnDefault'] in your controller file, causing the undefined issue. Note however that this variable is not global in the slightest, it's merely been coded as it should be
In my opinion, the right way to do this would be to create a new setting value for you to edit in SYSTEM > SETTINGS in your administration area, and then call it using $this->config->get('config_value_here') rather than take what is considered more of a quick hack method
you can use $GLOBALS super global array
for example declare it first in controller/common/header.php
$GLOBALS["1"] = "test";
then use it in any tpl file like
<?php echo $GLOBALS["1"]; ?>
regarding that header thing, that header and five other files are actually declared in every controller file (corresponding to every tpl file ) like this
$this->children = array(
'common/column_left',
'common/column_right',
'common/content_top',
'common/content_bottom',
'common/footer',
'common/header'
);
Answer to updated question
/catalog/controller/common/header.php
<?php
class ControllerCommonHeader extends Controller {
protected function index() {
// NEW GLOBAL VARS
$GLOBALS["cdnDefault"]="//www.gorgeouscouturedev.com/catalog/view/theme/";
$GLOBALS["currentUseLang"] = $this->language->get('code');
And now in /catalog/view/theme/default/template/common/home.tpl
<?=$header?>
<?=$column_left?>
<?=$column_right?>
<div id="content">
<? echo $GLOBALS["cdnDefault"]; ?>
<? echo $GLOBALS["currentUseLang"]; ?>
<?=$content_top?>
<div class="flexslider">
<ul class="slides">
<li><img src="catalog/view/theme/default/image/desktop.png"/></li>
<li><img src="catalog/view/theme/default/image/blogger.png"/></li>
</ul>
</div>
<?=$content_bottom?>
</div>
<?=$footer?>

Display two views at once and change url on change

I have a main view with a menu which helps me display another view. It's similar to this:
<div id="page">
<div id="menu">
Page1
Page2
</div>
<div id="content">
<!-- Page1 or Page2 are displayed here -->
</div>
</div>
I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2
in main view, instead of include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
UPDATE:
protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking
protected/views/layouts/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
protected/controllers/ControllerController.php:
class ControllerController extends Controller {
/**
* #var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
UPDATE2:
If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>
using ajax the preferred way is using hash like you mentioned.

Categories