Getting CodeIgniter HelloWorld example to work - php

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>

Related

why laravel view is showing blank page?

when I open "http://localhost/app/public/"
the default laravel home page works perfectly which is a welcome page where in the routes file there is this:
Route::get('/', function () {
return view('welcome');
});
but I'm learning so I just changed the "welcome" to "home" and created a new file resources/views/home.blade.php with the following simple code:
<?php
echo 'hello world of laravel';
?>
but when I open "http://localhost/app/public/"
it shows a blank page.
what am i missing?
You should setup your web server so it point to public directory and then access the app through http://localhost/
Try to change your home.blade.php to just html, remove this
<?php
echo 'hello world of laravel';
?>
and try something like:
<h1> Hello, world! </h1>
and see if works :)
This is the solution open .env file make sure no space in your app name.The app name should be one word for example AppOne not App One

How to use qr-code in my cakephp project

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, ...

Symfony + PHP not working

I am trying to create a website with Symfony 2.0 and I am running into an issue where I see extend("...") ?> from <?php $this->extend("...") ?>.
The site is viewable at http://symfony.toxic-productions.com/install/web/poshpaws/hello.
The code for the controller:
<?php
namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
//return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
// render a PHP template instead
return $this->render('AcmeHelloBundle:Hello:index.html.php', array('name' => $name));
}
}
?>
The code for the frontend page (index.html.php)
<html>
<head>
<title>Poshpaws</title>
<?php
$view->extend('::base.html.php');
echo($head);
?>
</head>
<body>
<?php echo($body); ?>
<h1>This is just a page to say: Hello <?php echo $view->escape($name) ?>!</h1>
</body>
</html>
I just had the same problem. It turned out that I didn't have php templating engine enabled in my configuration file.
Make sure you check the first step from this site:
http://symfony.com/doc/current/cookbook/templating/PHP.html
Your .htaccess file either isn't working or isn't in the right place. Make sure it's in the web folder. If it is, then make sure your webhost allows you to override Apache settings. If it does, then run php app/console router:debug over SSH and make sure your route is properly defined. Also, you really should just set "web" as the root of your directory or you expose certain security risks needlessly.
On a side note...why PHP templates? To me, it doesn't enforce separation of concerns quite as well and they're generally harder to read (for me, anyway).

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 :)

Zend MVC - I echo some test debug output but it seems to be suppresed and does not show

I am developing a Zend MVC application.
Sometimes I like to echo some text just for quick and dirty debugging. e.g
<?php
class MyClass {}
echo('hello world - yes this script is included');
I can echo 'debug-text' in my bootstrap> But, when text is echoed in my model or controllers it is NOT being rendered in the output.
I am using Zend 1.9.5 and using Zend Layout with MVC
Is there a setting somewhere that is suppressing all ''non-view script rendered'' output?
You can also use die() instead of echo :) it will work. Or use Zend_Log and output writer like this:
$writer = new Zend_Log_Writer_Stream('php://output');
Output buffering may be the culprit. Try:
echo 'my debug output';
flush();
If viewRendering is turned on, it's probably looking for the view/scripts/ to render and possibly not displaying your echo statements.
You could try to turn viewRendering off the given controller and see if that helps.
viewRenderer->setNoRender()
I am answering this question because I came back to the same code , 10 months later , and it confused me again.
I was in the context of a Default Module, Index Controller, and Index Action.
I wanted to echo some simple text as a quick way to see what was happening in the code:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
//home page
echo '<h1>hello world</h1>';
//set the special home page layout
$this->_helper->layout()->setLayout('home-page');
}
Trouble was - the echoed text was just not showing up.
It worked in other controllers , but not this one. It was driving me crazy.
Now - I've worked out the problem.
I was using a special layout for this particular control action... a 'home page layout'.
In my home page layout I was not rendering any view script. All the content and design was in the layout. Because the home page is a special , unique page, there was no need to separate into a two step view. For this reason I had no reason to need a view script. But, I did create a 'views\scripts\index\index.phtml' to keep ZF from complaining. I did not however, render it in my layout - cos it was not needed.
Any echoed output is captured into the layout()->content (automatically assigned by the layout from the 'default' response segment). Because I was not echoing out that segment, it behaved as if the echoed text was being suppressed.
To solve the problem , i simply had to ensure that i was echoing out the content segment.
i.e.
<html>
<head>
...
</head>
<body>
... my home page html...
<?php echo $this->layout()->content ?>
... my home page html...
</body>
</html>
which, with my debug text being echoed too... would render as:
... my home page html...
<h1>hello world</h1>
... my home page html...

Categories