PDA

View Full Version : PHP Newbie question


Yabadoo
21st July 2008, 23:10
The following php code is used in a part of an index.php file which is a part of my website.

if ($config['webmessage_DBstatus'] == 1) {
$query = mysql_query("SELECT * FROM newsgroups LIMIT 1");
$result = mysql_fetch_array($query);
echo '<br>';
echo '<br>';
echo 'Database Size: '. showdbsize(). '<br>';
echo 'Current Retention: '. avgRetention() . ' days<br>';
echo 'Amount of Files in database: '. GetFileCount().'<br>';
echo 'Amount of headers in database: '. GetHeaderCount().'<br>';
}

Because this takes to much cpu load i want to do this in a seperate php file which i can run in cron job once every 2 hours.
So i want to write the result of this file tot a .cache file.

I use the 2 php file's listed below.
hour.cron.php
<?
include_once "../inc/functions.inc.php";
GetConfig(1);
include_once "other.functions.php";

cache_dbstatuse();
?>

other.functions.php
<?
if(!function_exists('file_put_contents'))
{
function file_put_contents ($filename, $data)
{
if($fp = fopen($filename, 'w'))
{
fwrite($fp, $data);
fclose($fp);

return true;
}else{
return false;
}
}
}

function cache_dbstatus()
{
$content = $config['web_title'];
file_put_contents ('../cache/dbstatus.cache', $content);
}
?>
I don't now how to program the above listed code from my index file to write it to the file ..cache/webtitle.cache...

Any help is welkom!

Thanks in advance.

falko
22nd July 2008, 15:33
You can run crontab -e to create the cron job. The cron job could look as follows:
0 */2 * * * /usr/bin/php /path/to/phpscript

Yabadoo
22nd July 2008, 19:17
Thanks for the answer Falko, but this was not my question. I know how to make a cronjob, but what i don't now is how to translate this php code
if ($config['webmessage_DBstatus'] == 1) {
$query = mysql_query("SELECT * FROM newsgroups LIMIT 1");
$result = mysql_fetch_array($query);
echo '<br>';
echo '<br>';
echo 'Database Size: '. showdbsize(). '<br>';
echo 'Current Retention: '. avgRetention() . ' days<br>';
echo 'Amount of Files in database: '. GetFileCount().'<br>';
echo 'Amount of headers in database: '. GetHeaderCount().'<br>';

to run this in a cron job, and the output must go to a seperate file wich
i can call in my index.php.

function cache_dbstatus()
{
$content = $config['web_title'];
file_put_contents ('../cache/dbstatus.cache', $content);
}
?>

in this way..???

falko
23rd July 2008, 20:07
Instead of using echos I'd save the output in a variable and then write the content of that variable to a file using file_put_contents().

Yabadoo
23rd July 2008, 22:37
I solved it in another way.....
The code below is a php file, the output i redirect to a file. (i run this as a cron job every 2 hours...)

GetConfig(1);
if(!function_exists('file_put_contents'))
{
function file_put_contents ($filename, $data)
{
if($fp = fopen($filename, 'w'))
{
fwrite($fp, $data);
fclose($fp);

return true;
}else{
return false;
}
}
}
$empty = "";
file_put_contents ('../cache/dbstatus.cache', $empty);

$query = mysql_query("SELECT * FROM newsgroups LIMIT 1");
$result = mysql_fetch_array($query);
echo '<br>';
echo '<br>';
echo 'Database Size: '. showdbsize(). '<br>';
echo 'Current Retention: '. avgRetention() . ' days<br>';
echo 'Amount of Files in database: '. GetFileCount().'<br>';
echo 'Amount of headers in database: '. GetHeaderCount().'<br>';



in the overal funtion.php file i added this routine
function dbstatus()
{
echo file_get_contents (PATH . '/cache/dbstatus.cache');
}

In my index.php i added the folowing line
{
dbstatus
}


This works okay for me now.... maybe not the best solution, but it works for me!!

Thanks for the reply.