Im just starting out with php and cakePhp in general. I encountered the following error while trying to implement the blog tutorial. I have followed the steps in the tutorial till here http://book.cakephp.org/2.0/en/getting-started.html#creating-post-views
Screenshots of the error msgs are here-
[1]: http://i.imgur.com/xLvz3.png "top"
[2]: http://i.imgur.com/cJHPK.png "bottom"
Model/Post.php
<?php
class Post extends AppModel {}
Controller/PostsController.php
<?php
class PostsController extends AppController {
public $helpers = array(’Html’, ’Form’);
public function index() {
$this->set(’posts’, $this->Post->find(’all’));
}
public function view($id = null) {
$this->Post->id = $id;
$this->set(’post’, $this->Post->read());
}}
Posts/index.ctp
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
Replace " ’ " by a simple quote " ' ".
so you must declare helpers as :
public $helpers = array('Html', 'Form');
the same change for find method param :
$this->set('posts', $this->Post->find('all'));
at last
$this->set('post', $this->Post->read());
Anas
Related
I installed cakephp 2.9.7 and i am doing blog tutorial by referring(https://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html).
Create a Post model
class Post extends AppModel {
}
Creating a Post controller
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
Creating Post Views.I created Posts folder inside app/View folder.And i updated Database.php too so that it can connect to mysql database.
<!-- File: /app/View/Posts/index.ctp -->
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>
But when i run in my local cakephp/app error says
Error: The view for AppController::index() was not found.Confirm you have created the file: App/index.ctp in one of the following paths:
cakephp/app/View/App/index.ctp.stuck with solving this as i am new to cakephp.
Seems like you are requesting the wrong URL. If you request http://your-domain.de/app Cake will try to find the Action and View for index of the AppController.
You should get your desired Controller and View by requesting http://your-domain.de/posts or http://your-domain.de/posts/index.
I have a weird problem with Codeigniter and Datamapper,
I am making an CMS system and I am trying to edit an article from the Database using Datamapper. When I click to edit the article I get the message 'You have succesfully saved the article ' without clicking on the SAVE button, also all the data becomes empty and leaves 0. When I enter the data and click on SAVE it saves it but again when I try edit it again it goes back to 0 ... Can someone help out please
Here is my edit function in the controller
public function edit($id = NULL)
{
// Get articles by ID
$articles = new Article_model();
$article = $articles->where('id', $id)->get();
if ($id)
{
$id == NULL || $article;
count($article) || $error = 'Page not found ';
}
else
{
$article = $this->article_model->get_new();
}
$article->title = $this->input->post('title');
$article->text = $this->input->post('text');
if ($article->save())
{
echo 'You have succesfully saved the article';
}
else
{
echo 'Sorry something went terribly worng';
}
$data = array(
'admin_content' => 'admin/article/edit',
'article' => $article,
);
$this->parser->parse('admin/template_admin', $data);
}
and here is my view
<?php if ($this->tank_auth->is_logged_in()): ?>
<div class="container">
<div class="row">
<div class="col-md-9">
<h3><?php echo empty($article->id) ? 'Add an article' : 'Edit a Page ' . $article->title ;?></h3>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>Publication Date</td>
<td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
</tr>
<tr>
<td>Title</td>
<td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
</tr>
<tr>
<td>Body</td>
<td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
</div>
<?php else: redirect('/auth/login'); ?>
<?php endif; ?>
that's not weird at all...every time that controller is loaded it's going to execute the save() method of the article class.
you should probably include some form validation and only save the article if the form was submitted and validation passes.
PHP Function to select and display all the records stored under the column "clubName", from the table 'clubs'. The foreach statement ($club) returns an undefined variable error. What have I missed?
Controller:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Club_Controller extends Template_Controller {
public $template = 'kohana/template';
public function index()
{
$this->template->title = 'All clubs';
$this->template->content = new View('allclubs');
$clubs = ORM::factory('club')->find_all();
$this->template->content->club = $clubs;
}
}
?>
View:
<?php defined('SYSPATH') OR die('No direct access allowed.'); ?>
<div class="box">
<b><?php echo html::anchor('entry/form', 'add entry') ?></b><br>
<table cellpadding="10">
<?php foreach($clubs as $club): ?>
<tr>
<td align="left">
<?php echo html::anchor('entry/form/'.$club->id, 'edit') ?>
<?php echo html::anchor('entry/delete/'.$club->id, 'delete',
array('onclick'=>'return confirm("Are you realy realy want to do it?")')) ?>
</td>
<td align="left">
<?php echo $club->clubName ?>
</td>
</tr>
<?php endforeach ?>
</table>
</div>
Change this
$this->template->content->club = $clubs;
to this
$this->template->content->clubs = $clubs;
Hi Im trying to create a function that searches my Uploads model and displays the information that is in the that table.
Notice (8): Undefined variable: uploads [APP/View/Uploads/search.ctp, line 28]
Warning (2): Invalid argument supplied for foreach() [APP/View/Uploads/search.ctp, line 28]
Before I am even allowed to search I get this error,
This is my search.ctp
<?php $uploads = $this->requestAction('uploads/search');
?>
<div id="search">
<?php echo $this->Form->create('Upload',array('action'=>'search'));?>
<fieldset>
<legend><?php __('Upload Search');?></legend>
<?php
echo $this->Form->input('searchupload', array('label' => false, 'class'=>'searchinput'));
$options = array(
'label' => '',
'value' => 'Search',
'class' => 'searchbutton'
);
echo $this->Form->end($options);
?>
</fieldset>
</div>
<div id="contentbox">
<table>
<?php foreach($uploads as $upload) : ?>
<tr>
<td><?php echo $upload['Upload']['name'] ?></td>
<td><?php echo $upload['Upload']['eventname'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
and this is the function in the uploads controller:
function search() {
if (!empty($this->data)) {
$searchstr = $this->data['Upload']['search'];
$this->set('searchstring', $this->data['Upload']['search']);
$conditions = array(
'conditions' => array(
'or' => array(
"Upload.name LIKE" => "%$searchstr%",
"Upload.eventname LIKE" => "%$searchstr%"
)
)
);
$this->set('uploads', $this->Upload->find('all', $conditions));
}
}
Any help would be greatly appreciated!
Thanks in advance
<?php if(!empty($uploads)) : ?>
<?php foreach($uploads as $upload) : ?>
<tr>
<td><?php echo $upload['Upload']['name'] ?></td>
<td><?php echo $upload['Upload']['eventname'] ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<div>
No search matches found
</div>
<?php endif; ?>
This ensures that if an empty set is found then the no search match found is thrown into the webpage
Why are you running the uploads/search twice from the same view?
Try removing this from your view:
<?php $uploads = $this->requestAction('uploads/search'); ?>
It calls the same action you are rendering with search.ctp.
UPDATE:
If you are getting the Undefined Variable notice (that you will not see when in production and debug is set to 0 by the way), all you need to do is set the uploads variable to null in the controller (not the view). Before you run the search do: $this->set('uploads', array()); in the controller so the variable will be defined in the view. You do not want to clutter the view with logic.
I am building a portfolio website using CakePHP and have named my model 'Portfolio' and my controller 'PortfolioController' however cake decides to look for a table called portfolios instead of just portfolio! How can I fix this as I don't want to call my table portfolios!
Also when dealing with the foreach loop in the views where it has statements like
<?php foreach ($posts as $post): ?> how would I deal with the plural issue with my portfolio?
Thanks
EDIT Here is the index.ctp file where I have the plural issue:
<p><?php echo $this->Html->link("Add Post", array('action' => 'add')); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Action</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Portfolio']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Portfolio']['title'], array('action' => 'view', $post['Portfolio']['id']));?>
</td>
<td>
<?php echo $this->Html->link(
'Delete',
array('action' => 'delete', $post['Portfolio']['id']),
null,
'Are you sure?'
)?>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Portfolio']['id']));?>
</td>
<td><?php echo $post['Portfolio']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
EDIT Here is the controller:
<?php
class PortfolioController extends AppController
{
var $helpers = array ('Html','Form');
var $name = 'Portfolio';
function index()
{
$this->set('portfolio', $this->Portfolio->find('all'));
}
function view($id = null)
{
$this->Portfolio->id = $id;
$this->set('portfolio', $this->Portfolio->read());
}
function add()
{
if (!empty($this->data))
{
if ($this->Portfolio->save($this->data))
{
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
function delete($id)
{
if ($this->Portfolio->delete($id))
{
$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
function edit($id = null)
{
$this->Portfolio->id = $id;
if (empty($this->data))
{
$this->data = $this->Portfolio->read();
}
else
{
if ($this->Portfolio->save($this->data))
{
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
}
?>
Class Portfolio extends App_Model{
public $useTable = 'portfolio';
}
in you model you can set the $useTable variable to be whatever you need.
in views you pass variable values from controllers with set method
$this->set('portfolio',$array_of_data);
so the first parameter can be anything you like.
in the index action.
$this->set('portfolio', $this->Portfolio->find('all'));
see the code here? that's what I meant.
you are setting $portfolio variable for your view. so you must change all your $posts variables into $portfolio variables.
<p><?php echo $this->Html->link("Add Post", array('action' => 'add')); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Action</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($portfolio as $portfolio_item): ?>
<tr>
<td><?php echo $portfolio_item['Portfolio']['id']; ?></td>
<td>
<?php echo $this->Html->link($portfolio_item['Portfolio']['title'], array('action' => 'view', $portfolio_item['Portfolio']['id']));?>
</td>
<td>
<?php echo $this->Html->link(
'Delete',
array('action' => 'delete', $portfolio_item['Portfolio']['id']),
null,
'Are you sure?'
)?>
<?php echo $this->Html->link('Edit', array('action' => 'edit', $portfolio_item['Portfolio']['id']));?>
</td>
<td><?php echo $portfolio_item['Portfolio']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
or change portfolio in set method to posts like this.
$this->set('posts', $this->Portfolio->find('all'));