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.
Related
I´m a beginner starting with Laravel. I´m trying to show a question made on this website.
Here is the Controller page:
public function show($id)
{
//Use the model to get 1 record from the database
$question = $Question::findOrFail($id);
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
I have included at the top of the file:
use App\Question;
Here is my blade page:
#section('content')
<div class="container">
<h1> {{ $question->title }} </h1>
<p class="lead">
{{ $question->description }}
</p>
<hr />
</div>
#endsection
In the model I have not defined anything since I don´t need to specify any special rule. And finally here is the Route:
Route::resource('questions', 'QuestionController');
I got the error "ErrorException Undefined Variable: Question" and supposedly the error is on:
$question = $Question::findOrFail($id);
I´m looking forward to your observations.
Kind Regards.
You just need to change the controller section
public function show($id)
{
//Use the model to get 1 record from the database
$question = Question::findOrFail($id); // here is the error
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
Explanation:- You are going to use a variable $Question that is not defined. This is the basic PHP error, not the laravel issue.
However, You are using the "App\Question" model class not a sperate variable.
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.
It's my first post in here and I haven't yet figured out to format my post properly yet, but here it goes.
So basically I can only get my code to work if i point directly to a php-file. If I try to call a method within my controller, nothing seems to happen.
My JavaScript:
$(document).ready(function() {
$(".guide_button").click(function(){
var id = $(this).text();
var data = {};
data.id = id;
$.getJSON("/guides/hehelol", data, function(response){
$('#test').text(response.id);
});
return false;
});
});
My markup:
<div id="content_pane">
<ul>
<li>RL</li>
<li>LG</li>
<li>RG</li>
<li>SG</li>
<li>GL</li>
<li>MG</li>
</ul>
</div>
<div class="description">
<h3>Description</h3>
<p id="test">This text area will contain a bit of text about the content on this section</p>
</div>
My Controller:
<?php
class Guides extends CI_Controller {
public function Guides()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
public function index()
{
$this->load->view('guides_view');
$title = 'Some title';
}
public function hehelol() //The controller I am desperatly trying to call
{
$id = $_GET['id'];
$arr = array ('id'=>$id);
echo json_encode($arr);
}
}
It might be my controller I have done something wrong with. As it is the code only works if create a hehelol.php file and refer to it directly like this.
$.getJSON("hehelol.php", data, function(response){
$('#test').text(response.id);
});
Anyone who knows what I need to do to make my controller work properly? Help please! :)
i just put your exact code in its entirety in my codeigniter app and it worked for me. Meaning I used this: ...$.getJSON("/guides/hehelol",...
Because you are making a $_GET request, you have to enable query strings.
In your config.php file, make sure this line is set to TRUE:
$config['allow_get_array']= TRUE;
Using an ajax helper created for use with Jquery I've created a form that posts comments to "/comments/add", and this works as expected on my local wamp server but not on my production server. On my production server I get a '404 error, cannot find /comments/add'.
I've spent quite a bit of time searching for a resolution with no luck so far. I've focused on trying to identify a gap but nothing jumps out at me.
Here are some observations:
works as expected on local wamp server
requestHandler is listed as component
files on both local and production server are the same
controllers folder has write access
autoLayout and autoRender are both set to false
Here is the form in my view:
<div class="comments form">
<?php echo $ajax->form('/comments/add', 'tournament', array('url' => '/comments/add', 'update' => 'Comments', 'indicator' => 'commentSaved'));?>
<fieldset>
<legend><?php __('Add Comment');?></legend>
<div id="commentSaved" style="display: none; float: right;">
<h2>Loading...</h2>
</div>
<?php
echo $form->hidden('Comment.foreign_id', array('value' => $tournament['Tournament']['id']));
echo $form->hidden('Comment.belongs_to', array('value' => 'Tournament'));
echo $form->input('Comment.name');
echo $form->input('Comment.email');
echo $form->input('Comment.web', array('value' => 'http://'));
echo $form->input('Comment.content');
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
And here is my 'comment's controller add action:
function add() {
if($this->RequestHandler->isAjax())
{
$this->autoLayout = false;
$this->autoRender=false;
$this->Comment->recursive =-1;
$commentInfos = $this->Comment->findAllByIp($_SERVER['REMOTE_ADDR']);
$spam = FALSE;
foreach($commentInfos as $commentInfo)
{
if ( time() - strtotime($commentInfo['Comment']['created']) < 180)
{
$spam = TRUE;
}
}
if ($spam === FALSE)
{
if (!empty($this->data)) {
$this->data['Comment']['ip'] = $_SERVER['REMOTE_ADDR'];
$this->Comment->create();
if ($this->Comment->save($this->data)) {
$this->Comment->recursive =-1;
$comments = $this->Comment->findAll(array('Comment.foreign_id' => $this->data['Comment']['foreign_id'], 'Comment.belongs_to' => $this->data['Comment']['belongs_to'], 'Comment.status' =>'approved'));
$this->set(compact('comments'));
$this->viewPath = 'elements'.DS.'posts';
$this->render('comments');
}
}
}
else
{
$this->Comment->recursive =-1;
$comments = $this->Comment->findAll(array('Comment.foreign_id' => $this->data['Comment']['foreign_id'], 'Comment.belongs_to' => $this->data['Comment']['belongs_to'], 'Comment.status' =>'approved'));
$this->set(compact('comments'));
$this->viewPath = 'elements'.DS.'posts';
$this->render('spam');
}
}
else
{
$this->Session->setFlash(__('Invalid Action. Please view a post to add a comment.', true));
$this->redirect(array('controller' => 'pages', 'action'=>'display', 'home'));
}
}
As you can see I've made sure that 'autoLayout' and 'autoRender' are set to false, but in Firebug I still get a 404 error stating /comments/add cannot be found on the production server.
I should also point out that I'm using jquery, jquery.form and jquery.editable as well as a ajax helper created to be used with jquery.
Any help or even suggestions about how to trouble shoot this is really appreciated.
Cheers,
Paul
Turns out that this was caused by the last few lines of code, calling a session that I don't use in comments.
else
{
$this->Session->setFlash(__('Invalid Action. Please view a post to add a comment.', true));
$this->redirect(array('controller' => 'pages', 'action'=>'display', 'home'));
}
Lesson learned, be careful with code you get from a tutorial. It might have something you don't use.
-Paul
Have you made sure your .htaccess files are present on the production server? If so, is mod_rewrite installed, and is .htaccess overriding allowed in the home directory? It seems like the problem must lie in some difference between your local environment and your production environment, and not in the code itself.
I wanted to mention, i have simplepie working in my development environment but as soon as I uploaded the site I cannot get feeds into my homepage. Any ideas? here is the code that works on localhost:
function Homepage()
{
parent::Controller();
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
$this->images = $this->config->item('images');
$this->load->library('simplepie');
$this->simplepie->set_feed_url('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/world/rss.xml');
$this->simplepie->set_cache_location(APPPATH.'cache/rss');
$this->simplepie->init();
$this->simplepie->handle_content_type();
}
function index()
{
$data['rssdata'] = array(
"title" => $this->simplepie->get_title(),
"description" => $this->simplepie->get_description(),
"items" => $this->simplepie->get_items(0,5)
);
$this->load->view($data)
}
this is the code that is in the view:
<h3 class="ui-widget-header"><?= $rssdata['title']?></h3>
<div id="accordion" >
<div>
<h5><?= $rssdata['description']?></h5>
<p><?php foreach($rssdata['items'] as $item) :?>
<ul>
<li><?php anchor($item->get_link(),$item->get_title());?></li>
<li class="rssfeed"><?php echo $item->get_description();?></li>
</ul>
<p><small>Posted on <?php echo $item->get_date('j F Y g:i a');?></small></p>
<?php endforeach;?>
</div>
Use the newest "bleeding edge" from their GitHub profile, it sorts out several problems with PHP 5.3 which were making Apache explode from too many errors.
At time of writing, the bleeding edge is marked as v1.2.1-dev.
I've had this problem once, when my hosting setup did not have cURL installed, and error_reporting was off....
try setting var $force_fsockopen = false; to var $force_fsockopen = true; in the SimplePie config file to see if it makes a difference