How to use qr-code in my cakephp project - php

I found a tutorial on how to generate qr codes with php [here] http://www.terragon.de/index.php?id=273 . I worked through the tutorial and now I want to use it in my cakephp project, but I cannot seem to get it to work.
I added the folder called qr-code to app/lib. Within the folder is another folder called php that contains qr_img.php which contains the essential php code.(app/Lib/qr-cpde/php/qr_img.php)
In the tutorial, I created a file called index.php that contained this code:
<?php
echo "<img src='qr_img.php?d=Cara-Drye' >";
?>
in my code I replaced it with:
<?php
echo "<img src='app/Lib/qr-code/php/qr_img.php?d=Cara-Drye' >";
?>
but I only gt that little image box and no qr code.
Here is my controller
<?php
App::import('Lib','qr-code.php.qr_img.php');
class QrCodesController extends AppController {
//put your code here
public $name = 'QrCodes';
public function index(){
}
}
?>

once I put the source into the images folder in the webroot and changed my code to:
<?php
echo $this->Html->image('/img/qr-code/php/qr_img.php?d=Cara-Drye');
?>
everything worked fine. The thing about cakephp is the built in architecture, if you are wanting to use an image, it needs to be in the image folder.

If you dont mind using google, you can use
https://github.com/dereuromark/cakephp-tools/blob/cake2/View/Helper/QrCodeHelper.php
echo $this->QrCode->image($text);
and others like url, sms, tel, ...

Related

Php include not executed

I'm learning Php
And i made a main index.php page
And at some point it contains the line
<?php
include './BaseTemplate.php';
?>
BaseTemplate.php contains a lot of common plain html that is equal between all pages.
It looks like (but these are only a few lines) :
echo '<script src = "../assets/js/intention.js"></script>';
echo '<script src = "../assets/js/context.js"></script>';
echo '<head><body>';
echo '<table>';
But these echo commands dont get executed how should i resolve this ?
When you are including any file into your page the code for that file will come into the page.
So if the included file and the page where you are including it are in not in same path will create a problem.
It seems that you are getting that only. So please make the path in BaseTemplate.php according to the index.php, and it will work.
I solved it. I was thinking that include would put all code in a page from an external php source file. That's not the case, include files are libraries with functions which can be called.
So to resolve it, I rewrote it the BaseTemplate.php like this
<?php
Function WritePageBase() { // now i put it all inside a callable function
echo '<script src = "../assets/js/intention.js"></script>';
echo '<script src = "../assets/js/context.js"></script>';
echo '<head><body>';
echo '<table>';
}
?>
Now my other pages can call it like
<?php
include './BaseTemplate.php'; // include the file with functions
WritePageBase(); // call the function to echo all html code in page
?>
which reduces a lot of html code which now can be edited by a single file (and all other pages using it will change too).

Generating PDFs from template files using wkhtmltopdf

I need to generate PDF files from HTML templates and plan on using wkhtmltopdf to do that. Inside the HTML templates, I need to be able to use PHP logic to adjust what the template will render. Take this HTML template for example:
<p>Dear <?php echo $firstname; ?>,</p>
<p>Thanks for signing up. You've invited these people along with you:</p>
<ul>
<?php foreach ($invitees as $invitee): ?>
<li><?php echo $invitee; ?></li>
<?php endforeach; ?>
</ul>
<p>Regards,</p>
<p>Chris White</p>
I have no problem being able to pass a HTML template file to wkhtmltopdf but I don't know how to get the PHP logic inside it to run correctly and be able to return the resulting template. I came across this blog post while Googling but the author uses Smarty as a template language: https://davejamesmiller.com/blog/php-html-pdf-conversion-using-wkhtmltopdf
Using Smarty would solve my problem but I don't want to bring in a library to do this when I can just use plain old PHP. Basically, I need a way to pass in variables to the HTML template (in this case $firstname and $invitees), have it execute the PHP code inside the template and then return the resulting template after the PHP has been executed.
Any ideas?
Just save your file as php (for example template.php) and implement there the logic you need.
I did the same also with wkhtmltopdf and it worked great.
I also passed some variables over GET to the template to really get the correct report as pdf.
To save the file I used the PHP session id and saved the file to a folder with write permissions for www-data (Linux) and started the download automatically via Javascript.
I had the same need and I did it like that. Don t really know if my code will help you but why not.
First I used composer to get https://github.com/mikehaertl/phpwkhtmltopdf.
lets say you have a php file " content.php "
<?php
echo "<html>";
echo "<h1>test</h1>";
echo "<html>";
?>
your index.php will be :
<?php
require "vendor/autoload.php";
ob_start();
require('content.php'); //The php file
$content = ob_get_clean();
$pdf = new \mikehaertl\wkhtmlto\Pdf($content);
if (!$pdf->send()) {
throw new Exception('OMG WHY : '.$pdf->getError());
}
If you're not using any template engine, can't u just call your template.php file with some params ?
Something like
$wkhtml2pdf->html2pdf('template.php?firstname=Foo');
(I have no idea how wkhtml2pdf works, this code is just for you to understand the logic)
and in your template.php file :
<p>Dear <?php echo $_GET['firstname']; ?>,</p>

Zend Framework CSS issue

I am new to ZF but have been looking into one specific issue I am having. I am currently trying to append a stylesheet to my site through the layout.phtml file. It works fine when the URL remains at the index action of a controller however if I go to site/controller/method the stylesheet presents incorrectly. I am doing everything right I believe. I have used the following code on my site to append the stylesheet:
<?php
$this->headLink()->appendStylesheet($this->baseUrl('stylesheets/style.css'))
->appendStylesheet($this->baseUrl('stylesheets/jplayer.css'))
->appendStylesheet($this->baseUrl('stylesheets/contact.css'));
echo $this->headLink();
$this->headScript()->appendFile("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
echo $this->headScript();
?>
I have also tried doing this from the controller action minus the echo part which ends up breaking the page all together.
EDIT: I got the css to show up now but the font wont display the correct one. Has anyone experienced this and could this be a part of my css directing to the incorrect path or the font breaking somewhere.
Keep stylesheets folder in public direcory of your project
<?php
$this->headLink()->appendStylesheet('stylesheets/style.css');
$this->headLink()->appendStylesheet('stylesheets/jplayer.css');
$this->headLink()->appendStylesheet('stylesheets/contact.css');
echo $this->headLink();
$this->headScript()->appendFile("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
echo $this->headScript();
?>
Hopes it will solves your problem

CodeIgniter and Javascript/Jquery Library

As title said, I'm trying to figure out how to use javascript and jquery libraries on CI.
Following instruction in the docs, I load the library in my controller:
$this->load->library('javascript');
Then, I define the location of the jQuery file (jquery.min.js) in the config.php:
$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');
After that, I open the view file and put in these two lines:
<?php echo $library_src;?>
<?php echo $script_head;?>
First error comes up here: Undefined variable $library_src and $script_head (don't understand where I have to set them)
Anyway, I've commented these lines and continue with jquery lib, by loading it in my controller with:
$this->load->library('jquery');
Next error: Unable to load the requested class: jquery. (it seems that it can't find the lib, what i messed up?)
Checking on system folder it looks all files are in place:
system/libraries/Javascript.php
system/libraries/javascript/Jquery.php
Thanks in advance for your help!
Put the code in the config.php like this:
$config['javascript_location'] = 'js/jquery/jquery.js';
$config['javascript_ajax_img'] = 'images/ajax-loader.gif';
In your controller file (e.g. controllers/sample.php) type this codes:
function __construct()
{
parent::__construct();
$this->load->library('javascript');
}
function index()
{
$data['library_src'] = $this->jquery->script();
$data['script_head'] = $this->jquery->_compile();
$this->load->view('sampleview', $data);
}
In your view file (e.g. views/sampleview.php) type this codes:
<?php echo $library_src;?>
<?php echo $script_head;?>
This works for me. I hope it works for you too. XD
It is important to note that this Driver is marked as experimental so I wouldn't rely on it.
Also, personally I think it's asking for confusion and headaches to try and directly mix server-side portions of your applications with client side portions.
To use javascript in your views, I would just start out by loading them like this...
<script type="text/javascript" src="<?= base_url() ?>path/to/jquery.js"></script>
Because this driver is experimental the documentation isn't quite there yet. But I was able to find a solution.
First, the documentation has an error in it. Unless you change the core Javascript library (not suggested) the reference variable is not $script_head but in fact$script_foot.
Second, once you've finished making your calls, it seems you need to run
$this->javascript->external();
and
$this->javascript->compile();
These functions set the $library_src and $script_foot variables.
To put it all together, in your controller you would have:
class Some_Controller extends CI_Controller {
public function index()
{
$this->javascript->click('#button', "alert('Hello!');");
$this->javascript->external();
$this->javascript->compile();
$this->load->view('index');
}
}
In your view you would have
<html>
<head>
<?php echo $library_src; ?>
<?php echo $script_foot; ?>
Although CI first looks for system folder, you can also try putting your libs in the these folders:
application/libraries/Javascript.php
application/libraries/javascript/Jquery.php
Not sure why you have to load your js file via controller and then echoing it in your view. You can just load ur js file using HTML tags directly in your view. Passing data from controller and echoing it in view is mostly used when your variable's value is dynamic/ needs to be loaded from databases etc.
As we know on the user guide, first, it was an experimental but working. Thr first step is open your config file under application/config/config.php .
Put the following line :
// path to JS directory you want to use, I recommended to put the .js file under 'root app/js/yourfile.js'
$config['javascript_location'] = 'js/yourfile.js';
Second step is open your controller, within constructor method, put the following code :
// Load javascript class
$this->load->library('javascript');
$this->load->library('javascript/jquery'); // if u want to use jquery
Then, still in controller file within index method :
$data['library_src'] = $this->jquery->script(); // Because I refer to use jquery I don't test to use $this->javascript->script().
Then open your view file and put the following code within tag head :
<?php echo $library_src; ?>
That way is working for me, let's try it.
Use:
$this->load->library('javascript/jquery');
instead of:
$this->load->library('jquery');
This will load your jQuery library.
I had the same problem, and found :
<?php $this->load->library('javascript'); ?>
<?php $this->load->library('javascript/jquery'); ?>
on https://ellislab.com/forums/viewthread/181742/#860506
Because jquery is in javascript folder.
Or
$autoload['libraries'] = array('database', 'session', 'javascript', 'javascript/jquery');
In the autoload.php file.
That solved the problem of loading the librairy.
You can try this, it's work to me.
in config.php
$config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';
in Controller
public function __construct() {
parent::__construct();
$this->load->library('javascript');
$this->load->library('javascript/jquery');
}
public function index(){
$d['library_src'] = $this->jquery->script();
$d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');");
$this->load->view('main',$d);
}
in view (head)
<?php echo $library_src; ?>
in view content (body)
<div class="alert alert-dismissable alert-info" id="logalert">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
</div>
in javascript view
<script type="javascript">
$(document).ready(function(){
<?php echo $logalert; ?>
};
</script>
happy coding :)

Getting CodeIgniter HelloWorld example to work

Guys I'm a newbie to code igniter.. I do not understanding how to use this framework. Its just opening the user guide. Can anyone tell me the steps I need to follow to execute the "hello world" program on code igniter?
Make a file called hello.php in your system/application/controllers folder. In the file, put this code:
<?php
class Hello extends Controller
{
function index()
{
echo 'Hello world!';
}
}
?>
Then go to http://localhost/codeigniter/index.php/hello , and you should see the hello world. (You might've put it in a different directory than codeigniter, so change the url as needed).
Then you can change the code to:
<?php
class Hello extends Controller
{
function index()
{
echo 'Hello world!';
}
function test()
{
echo 'Second hello world!';
}
}
?>
and going to http://localhost/codeigniter/index.php/hello/test would run the 'test' function from the class.
Using .htaccess and mod_rewrite you can remove the 'index.php' from your url, so you would only visit http://localhost/codeigniter/hello or http://localhost/codeigniter/hello/test.
Dont mistake code igniter with an IDE.
code igniter is a frame work, not an itegrated development enviroment (IDE). An example of an IDE is ECLIPSe. An IDE will have a text editor, mabee some syntax highlighting/bug checking/and even compiling capabilities..
A frame work on the otherhand is a set of functions/classes/scripts that contains code that you can reuse to make your life simpler, or to give it order.
So assuming you have downloaded code igniter and have it on your webserver, you can use the previous users instructions.
My advice is to watch/listen to the video tutorials avaliable here: http://codeigniter.com/tutorials/ and http://video.derekallard.com/ and make notes on them. Should show you how codeigniter works.
<html>
<head>
<title>Codeigniter</title>
</head>
<body>
<?php
include "index.php";
class hello extends CI_Controller
{
function index()
{
echo "Hello world!";
}
}
echo "head<br>";
$t = new hello();
$t->index();
?>
</body>
</html>

Categories