Jump to content

Recommended Posts

Hi

 

I have an undefined variable called todo and I'm not sure how to fix it. I have looked at the tutorials and everything but I'm not sure on how to apply those to my problem.

 

Here is my itemscontroller.php

 

<?php

class ItemsController extends Controller {

function view($id = null,$name = null) {

	$this->set('title',$name.' - My Todo List App');
	$this->set('todo',$this->Item->select($id));

}

function viewall() {

	$this->set('title','All Items - My Todo List App');
	$this->set('todo',$this->Item->selectAll());


}

function add() {
	$todo = $_POST['todo'];
	$this->set('title','Success - My Todo List App');
	$this->set('todo',$this->Item->query('insert into items (item_name) values (\''.mysql_real_escape_string($todo).'\')'));	
}

function delete($id = null) {
	$this->set('title','Success - My Todo List App');
	$this->set('todo',$this->Item->query('delete from items where id = \''.mysql_real_escape_string($id).'\''));	
}

}

 

and this is my viewall.php where the error is showing up

<form action="../items/add" method="post">
<input type="text" value="I have to..." onclick="this.value=''" name="todo"> <input type="submit" value="add">
</form>
<br/><br/>
<?php $number = 0?>

<?php foreach ($todo as $todoitem):?>
<a class="big" href="../items/view/<?php echo $todoitem['Item']['id']?>/<?php echo strtolower(str_replace(" ","-",$todoitem['Item']['item_name']))?>">
<span class="item">
<?php echo ++$number?>
<?php echo $todoitem['Item']['item_name']?>
</span>
</a><br/>
<?php endforeach?>

Link to comment
https://forums.phpfreaks.com/topic/265044-undefined-variable/
Share on other sites

I'd start by making sure that $this->Item->selectAll() is returning rows by adding a log statement and checking the logs after the call.

 

This:

 

$this->set('todo',$this->Item->query('insert into items (item_name) values (\''.mysql_real_escape_string($todo).'\')'));

 

and this:

 

$this->set('todo',$this->Item->query('delete from items where id = \''.mysql_real_escape_string($id).'\''));

 

are definitely very wrong. You don't really want to set the view variable to the results of an INSERT or a DELETE query do you?. And I'm quite sure you're doing it wrong by using query() instead of the model's create()/save() and delete() methods.

Link to comment
https://forums.phpfreaks.com/topic/265044-undefined-variable/#findComment-1358198
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.