Jump to content

[Joomla] Problem when calling this function


kikookik

Recommended Posts

Hello;

I have a portion of code that work outside a function, but when i include it inside a function it didn't work. I don't know why?

Certainly i am missing something but i could see it. If someone saw the error so please please let me correct it.

Thank you for your help.

 

This is the source code of the file:

<?php
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once(JPATH_BASE.'/includes/defines.php');
require_once(JPATH_BASE.'/includes/framework.php');
require_once(JPATH_BASE.'/libraries/joomla/factory.php');


$article_life   	= 72;	// Article lifetime in hours, 72 hours = 3 days
$articles_number  	= 15; 	// Alowed number of articles inside a category

$db     = JFactory::getDBO();
$query  = $db->getQuery(true);

Cleanup(49, 48, 0);

function Cleanup($catid, $life = $article_life, $number = $article_number){

	// Request articles in the category from database
	$query	->select("*")
		->from("#__content")
		->where("catid = " . $catid);
	$db	->setQuery($query);
	$articles	= $db->loadObjectList();
	$nbr = count($articles);
	if($nbr <= $number)
		return;
	foreach($articles as $article) { // loop through articles
		// $article->created looks like 2012-12-10 16:53:15
		$date_created = DateTime::createFromFormat('Y-m-d H:i:s', $article->created);
		$timestamp_created = $date_created->format('U');
		$diff_s = time() - $timestamp_created;
		$diff_h = $diff_s / 3600;
		if($diff_h > $life) { // If article life is bigger than the allowed value, remove it!
			$query = "DELETE from #__content where id = " . $article->id;
		        $db     ->setQuery($query);
		        $db     ->query();
		}
	} // End foreach
	return;
} // End Cleanup

?>
Link to comment
Share on other sites

You need to read up on "variable scope". The $db variable does not exist INSIDE the function since it is defined OUTSIDE the function (there may be others, I did not go through all of the code). You need to pass $db to the function in the parameter list.

Link to comment
Share on other sites

Ah, sorry for that. Now i did include it but the seem issue still appear.

Thank you.

<?php
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once(JPATH_BASE.'/includes/defines.php');
require_once(JPATH_BASE.'/includes/framework.php');
require_once(JPATH_BASE.'/libraries/joomla/factory.php');


$article_life   	= 72;	// Article lifetime in hours, 72 hours = 3 days
$articles_number  	= 15; 	// Alowed number of articles inside a category

Cleanup($db, 49, 48, 0);

function Cleanup($catid, $life = $article_life, $number = $article_number){
	$db	= JFactory::getDBO();
	$query	= $db->getQuery(true);

	// Request articles in the category from database
	$query	->select("*")
		->from("#__content")
		->where("catid = " . $catid);
	$db	->setQuery($query);
	$articles	= $db->loadObjectList();
	$nbr = count($articles);
	if($nbr <= $number)
		return;
	foreach($articles as $article) { // loop through articles
		// $article->created looks like 2012-12-10 16:53:15
		$date_created = DateTime::createFromFormat('Y-m-d H:i:s', $article->created);
		$timestamp_created = $date_created->format('U');
		$diff_s = time() - $timestamp_created;
		$diff_h = $diff_s / 3600;
		if($diff_h > $life) { // If article life is bigger than the allowed value, remove it!
			$query = "DELETE from #__content where id = " . $article->id;
		        $db     ->setQuery($query);
		        $db     ->query();
		}
	} // End foreach
	return;
} // End Cleanup

?>

Link to comment
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.