Not sure what is going on here….not exactly a developer just trying to learn. For now the idea is that code snip can be pasted into template but eventually would want to learn how to make a simple extension out of it.
Thank you.
EDIT NOTE: it seems that if I remove the use of functions it gets processed right…what gives? Can we not put queries inside functions?
Fatal error: Using $this when not in object context in /sandbox/ee2/system/expressionengine/libraries/Functions.php(656) : eval()’d code on line 15
<?php
// =============================================================
// Delete all comments that are made by guests - nonmembers
// and update comment counter
// =============================================================
// --- 'Show Comments'
function find_comments()
{
// connect to database
global $db;
// SHOW comments that will be deleted
$show_comments = $this->EE->db->query ("SELECT * FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");
if ($show_comments->num_rows > 0)
{
echo "Name » Comment
<hr >\n";
foreach($show_comments->result as $row)
{
echo $row['name'].' » '.$row['comment']."
<hr >\n";
}
}
else {
echo "All of the comments are made by members.";
}
}
// --- END 'Show Comments'
// execute
find_comments();
// --- 'Delete Comments'
function delete_comments ()
{
// connect to database
global $db;
// DELETE the comments shown above
$delete_comments = $this->EE->db->query("DELETE FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");
echo $this->EE->db->affected_rows." comments were deleted.";
}
// execute
delete_comments();
// --- END 'Delete Comments'
// build form here
?>$this is a special variable that can only be used inside of a method/function that is bound to an instance of a class.
Ex.
class Person {
function get_name()
{
return $this->name;
}
}For your code you do not need to use $this. Try something like this (pardon the pun):
<?php
// =============================================================
// Delete all comments that are made by guests - nonmembers
// and update comment counter
// =============================================================
// --- 'Show Comments'
function find_comments()
{
// connect to database
$EE = get_instance();
// SHOW comments that will be deleted
$show_comments = $EE->db->query ("SELECT * FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");
if ($show_comments->num_rows > 0)
{
echo "Name » Comment
<hr >\n";
foreach($show_comments->result as $row)
{
echo $row['name'].' » '.$row['comment']."
<hr >\n";
}
}
else {
echo "All of the comments are made by members.";
}
}
// --- END 'Show Comments'
// execute
find_comments();
// --- 'Delete Comments'
function delete_comments ()
{
// connect to database
$EE = get_instance();
// DELETE the comments shown above
$delete_comments = $EE->db->query("DELETE FROM exp_comments WHERE NOT EXISTS (SELECT * FROM exp_members WHERE exp_members.member_id = exp_comments.author_id)");
echo $EE->db->affected_rows." comments were deleted.";
}
// execute
delete_comments();
// --- END 'Delete Comments'
// build form here
?>(edit: damn… not quite quick enough)
afaik, because the code in a template is eval()’d, it isn’t an object and therefore doesn’t have a $this variable. In addition to that, the EE object isn’t referenced and so not available either.
You could try to get access to the EE object with the same function call as a module would use, just without reference to the $this object:
$EE =& get_instance();(the variable “$EE” is flexible of course)
You would then reference this for a query with:
$EE->db->queryThank you both. Now it makes more sense…I did not realize that $EE was a flexible variable. Is this EE 2 specific way of connecting to db?
$EE = get_instance();Docs don’t have this referenced. Thank you.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.