Header not appearing & Footer not correct - codeigniter

I'm following the instructions in the CodeIgniter Tutorial on the Static Page but the Header does not appear although the tab is labelled "CodeIgniter Tutorial" and the content of the Footer appears on the same line as the content of the Page.
This is the content of my Pages.php - application/controllers/Pages.php
<?php
class Pages extends CI_Controller
{
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
This is the content of my header.php - application/views/templates/header.php
<html>
<head>
<title>CodeIgniter Tutorial</title>
</head>
<body>
<h1><?php echo $title ?></h1>
This is the content of my footer.php - application/views/templates/footer.php
<em>© 2014</em>
</body>
</html>
This is the content of my routes.php - application/config/routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
This is one URL http://localhost/MyProject/index.php/pages/view
And this is the result;
Home
Hello World! © 2014
And this is the other URL http://localhost/MyProject/index.php/pages/view/about
And this is the result;
About
Hello World! © 2014
I've checked every page numerous times, tried re-booting, and tried another browser, and tried CodeIgniter Forum, all to no avail.
Can somebody explain where I'm going wrong?
Edited
In the header.php file I've changed
<h1><?php echo $title ?></h1>
to
<h3><?php echo $title ?></h3>
which makes the word "Home" decreases in size. The only file I can find the word "home" is Pages.php
public function view($page = 'home')
If I change that word to "CodeIgniter Tutorial" I get a 404 error.
The IE tab is labelled "CodeIgniter Tutorial"
I'm unsure if my expectations are correct, but this is the result that I'm expecting (but with "© 2014" at the bottom of the page);
CodeIgniter Tutorial
Hello World!
© 2014

It seems like the files views/pages/about.php and views/pages/home.php are empty. Try writing something in them, for example:
views/pages/about.php
<i>This is the About page</i>
If the file exist, but is empty, you should get the result you're seeing, because this line will not render anything:
$this->load->view('pages/'.$page, $data);
If the files don't exist, you should get some 404 output - so that's a bit weird. It could happen if the file application/errors/error_404.php is empty.

I tested your project on my localhost and your pages controller is fine.
It just seems to be you need to either use p tags or div etc and then
use css style sheets etc.
But instead of
<em>© 2014</em>
</body>
</html>
Try
<p>© 2014</p>
</body>
</html>
Proof

I created a file at application/views/pages/CodeIgniter Tutorial.php
and changed
public function view($page = 'home')
to
public function view($page = 'CodeIgniter Tutorial')
in Pages.php
and changed
<em>© 2014</em>
to
<p><br><br><em>© 2014</em></br></br></p>
in footer.php
I can achieve close to my expectations and now realise my expectations were far too much for a simple piece of coding.

Related

Site URL CodeIgniter Not Working

I have Controller on CI like this
class Testing extends CI_Controller {
//put your code here
public function xx() {
$this->load->helper('url');
$this->load->view('testing');
}
public function linkURL() {
$this->load->helper('url');
$data['test'] = "testing123";
$this->load->view('xxx_view', $data);
}
}
I'm running on function linkURL and call view xxx_view, the code on view like this
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$segment = array('Testing', 'xx');
?>
Link
</body>
view call a href and using helper site_url to call Controller Testing and function xx. But the link is not working. I am already capture on firebug and link looks like weird. The Link on href contain *http://::1*. How to solved that link
You can print_r($_SERVER) in your controller and check it. or You can use
$config['base_url'] = 'http://localhost:8081/your-project/'
In my opinion, seem to you sent data test to view xxx_view but not using this variable. try echo $test and I see in url using xxx not xx
You need to autoload URL helper in your config/autoload.php.This time you are loading URl helper in function. That will not work for your view.

how to open pdf file to another tab in browser using codeigniter

im making currently making my thesis about a record management of our university secretary.. in which all papers inside the office will be scanned and uploaded in the system.. i am using codeigniter..one of the feature in my system is to view the pdf file in other window of the browser. but my problem is, when i click the title. only blank page will be displayed in the other tab.. can you help me solve this one?? here is my code
controller:
function viewMinutesFile(){
if(isset($_GET['id'])){
$id = $_GET['id'];
$file = $this->minutes_model->getFile($id);
$fp= fopen($file->path, "r");
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$file->filename."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' .filesize($file->path));
ob_clean();
flush();
while (!feof($fp)){
$buff = fread($fp,1024);
print $buff;
}
exit;
}
}
code to open the file: this is my syntax to be clicked by the user so that pdf file will be open in the new tab
File
index.php/admin/viewMinutesFile?
id=" target="_tab">
try this one with a static url. no need any extra words for that.
Show My Pdf
New Update
if its work for you then fetch pdf name from database and put the name in the view like
Show My Pdf
now in the controller
$this->load->helper('download');
if($this->uri->segment(3))
{
$data = file_get_contents('./file_path/'.$this->uri->segment(3));
}
$name = $this->uri->segment(3);
force_download($name, $data);
well, you could add a link to file with target="_blank", like
<a href="<?php echo base_url(). 'your_controller/viewMinutesFile'; ?>" target="_blank">
View Pdf
</a>
and in controller function:
function viewMinutesFile(){
....
$file = $this->minutes_model->getFile($id);
$this->output
->set_content_type('application/pdf')
->set_output(file_get_contents($your_pdf_file));
}
you can try this on your view :
Filename
and on your controller, you can try this, because this is works for me :
function viewfile(){
$fname = $this->uri->segment(3);
$tofile= realpath("uploaddir/".$fname);
header('Content-Type: application/pdf');
readfile($tofile);
}
hope this might help you...
Just create a link to a blank page and use this code in your controller:
public function myPdfPage(){
$url = base_url('assets/your.pdf');
$html = '<iframe src="'.$url.'" style="border:none; width: 100%; height: 100%"></iframe>';
echo $html;
}
Enjoy!
There is no any problem with your code you can open easily on next tab, like other pages only difference you have to change header description and it is make sure on your browser pdf reader add-ons are available, otherwise it will give you option to download.
You may just follow this.
<?php echo form_open_multipart('your_controller/your_function','target="_blank"') ;?>
//other input fields
<?php form_close();?>

Laravel 4 blade templates causing FatalErrorException?

I'm getting an unexplained FatalErrorException when trying to implement a simple page layout using blade templating. I'm not sure if it's something I'm doing wrong or Laravel is. I'm following the tutorial on L4's documentation about Templating and my code seems to follow it. Here's my code.
app/routes.php:
<?php
Route::get('/', 'HomeController#showWelcome');
app/views/home/welcome.blade.php:
#extends('layouts.default')
#section('content')
<h1>Hello World!</h1>
#stop
app/views/layouts/default.blade.php:
<!doctype html>
<html>
<head>
<title>The Big Bad Barn (2013)</title>
</head>
<body>
<div>
#yield('content')
</div>
</body>
</html>
app/controllers/HomeController.php:
<?php
class HomeController extends BaseController {
protected $layout = 'layouts.default';
public function showWelcome()
{
$this->layout->content = View::make('home.welcome');
}
}
Laravel just throws a FatalErrorException. The output error page says "syntax error, unexpected '?'". The file blade is generating inside the storage/views directory has PHP where the
<?php echo $__env->make('layouts.default')
<?php $__env->startSection('content', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>; ?>
<h1>Hello World!</h1>
<?php $__env->stopSection(); ?>
Yesterday I encountered the same problem you did, however the other answers didn't fix my problem. You've probably figured it out already, but maybe this post prevents others from spending as much time as I did figuring out what is wrong while it's such a small (but frustrating!) thing.
I'm using Notepad++ as text-editor and for some strange reason it had decided to use "MAC format" as the End-Of-Line (EOL) format. Apparently the Blade framework can't cope with that. Use the conversion function (in notepad++ : Edit -> EOL Conversion) to convert to Windows Format and it will work just fine..
Your controller should be just returning View::make("home.welcome") instead of attaching it to the layout.
The welcome view then calls the layout so the controller is only concerned about the body template in this case.
Edit for showing example controller:
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('home.welcome');
}
}
Eric already gave the right answer, I just wanted to add that if you want to use
protected $layout = 'layouts.default';
in your HomeController, then leave showWelcome action intact and remove this line
#extends('layouts.default')
from welcome.blade.php file. That should work too.
Regards,
Vlad

Redirecting to another page. How do I display a countdown?

I'm just wondering if the following is possible:
I don't know anything about JQuery and I don't know how to make my own javascripts yet.
When a user logs into my website, they'll get a page that says "login completed".
Under that sentence, I would like to get a sentence that counts down the seconds until they are redirected. Is there a simple way to do this?
I already have a countdown function:
When the function is first called, the view 'view_login_success' is loaded and the $data[] array is passed to it. $data['sec'] holds the seconds remaining that should be printed on the screen.
When the do-while loop is over, the user should be redirected to the homepage.
What happends is, the 'view_login_success'-view isn't loaded at all and after 5 seconds, the user is redirected to the homepage.
function timerIn() {
$now = time();
$this->load->view('view_login_success', $this->data);
do {
if (time() - $now != 0) {
$this->data['sec'] = $this->data['sec'] - 1;
$this->load->view('view_login_success', $this->data);
$now = time();
}
} while ($this->data['sec'] != 1);
$this->data['sec'] = 5;
redirect('user/start');
}
Here is the view:
<!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">
<head>
<title>Login geslaagd</title>
<?php $this->load->view('templates/header2'); ?>
</head>
<body>
<h2 style="text-align: center;"><?php echo strtoupper(substr($this->session->userdata('gebruikersnaam'),0,1));
echo substr($this->session->userdata('gebruikersnaam'),1); ?>, U bent ingelogd!</h2>
<p>U word doorverwezen: <?php echo $sec; ?> seconden...</p>
</body>
<?php $this->load->view('templates/footer'); ?>
If there's a way to do this in JQuery of Javascript, then please explain to me what I should do to get it to work.
Thanks in advance! :)
That's not how php works.
If you want the page to redirect after 5 secs you have to do so in the JS code.
You redirect the user without sending the view to the client.
Right now you are only waiting 5 seconds to redirect. So:
Show the view
The JS will have the timer and then redirects.
The javascript code would be something like this:
setTimeout(function(){
window.location.href = 'theURLyouwant';
},5000)
OR you could use the ol' meta tag
<meta http-equiv="refresh" content="5; URL=theURLyouwant">
Use code-igniter to solve this problem. Use this reference class output. Output class will be loaded automatically. You don't need to load this. Use this function to set header.
$this->output->set_header('refresh:time-to-redirect; url='.site_url("your-url"));
For more detail about this class.

newbie problems with codeigniter

i'm trying to learn codeigniter (following a book) but don't understand why the web page comes out empty.
my controller is
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$data['title'] = "Welcome to Claudia's Kids";
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = $data['mainf']['id'];
$data['sidef'] = $this->MProducts->getRandomProducts(3, $skip);
$data['main'] = "home";
$this->load->vars($data);
$this->load->view('template');
}
the view is:
<--doctype declaration etc etc.. -->
</head>
<body>
<div id="wrapper">
<div id="header">
<?php $this->load->view('header');?>
</div>
<div id='nav'>
<?php $this->load->view('navigation');?>
</div>
<div id="main">
<?php $this->load->view($main);?>
</div>
<div id="footer">
<?php $this->load->view('footer');?>
</div>
</div>
</body>
</html>
Now I know the model is passing back the right variables, but the page appears completely blank. I would expect at least to see an error, or the basic html structure, but the page is just empty. Moreover, the controller doesn't work even if I modify it as follows:
function index()
{
echo "hello.";
}
What am I doing wrong?
Everything was working until I made some changes to the model - but even if I delete all those new changes, the page is still blank.. i'm really confused!
thanks,
P.
I've isolated the function that gives me problems.
here it is:
function getMainFeature()
{
$data = array();
$this->db->select("id, name, shortdesc, image");
$this->db->where("featured", "true");
$this->db->where("status", "active");
$this->db->orderby("rand()");
$this->db->limit(1);
$Q = $this->db->get("products");
if ($Q->num_rows() > 0)
{
foreach($Q->result_arry() as $row)
{
$data = array(
"id" => $row['id'],
"name" => $row['name'],
"shortdesc" => $row['shortdesc'],
"image" => $row['image']
);
}
}
$Q->free_result();
return $data;
}
I'm quite convinced there must be a syntax error somewhere - but still don't understand why it doesn't show any error, even if I've set up error_reporting E_ALL in the index function..
First port of call is to run php -l on the command line against your controller and all the models you changed and then reverted.
% php -l somefile.php
It's likely that there is a parse error in one of the files, and you have Display Errors set to Off in your php.ini. You should set Display Errors on for development and off for production, in case you haven't already.
(Edit: in the example above you have missed off the closing } of the class. It might be that.)
Make sure error_reporting in index.php is set to E_ALL and post your code for the model in question.
After looking through your function I suspect it's caused by $this->db->orderby("rand()");
For active record this should be $this->db->order_by('id', 'random');
Note that orderby is deprecated, you can still use it for now but the new function name is order_by
Not sure, but it can be also caused by php's "display_errors" is set to false.
You can change it in your php.ini file.

Resources