forum.coppermine-gallery.net

Support => cpg1.3.x Support => Older/other versions => cpg1.3 Themes/Skins/Templates => Topic started by: dafyd on January 02, 2005, 12:06:18 am

Title: Help with PHP includes
Post by: dafyd on January 02, 2005, 12:06:18 am
No doubt GauGau is going to tell me to search properly before I post this - I have, and I've followed the steps outlined as best I can, and I still need help...

My site at www.july162005.com has a header across all the pages with, among other things, a PHP countdown to July 16 2005. Now, I've got the rest of the header working fine by creating a new version of template.html, but for the life of me I can't get the dratted countdown to work properly. As I say, I've read everything on the board, and it really doesn't help.

The code for the countdown is the following:

Code: [Select]
<?php
define
("OFFSET"0);
define("YSECS"365*24*60*60);
define("DSECS"24*60*60);
define("HSECS"60*60);
define("MSECS"60);

function 
countdown($detail$year$month 1$day 1$hour 0$minute 0$second 0) {
$years $days $hours $minutes $seconds 0;
$now mktime() + OFFSET*60*60;
$then mktime($hour$minute$second$month$day$year);
$cdown abs($then $now);

if ($detail == 1$years round($cdown/YSECS);
else $years floor($cdown/YSECS);
$cdown %= YSECS;
if ($detail == 2$days round($cdown/DSECS);
else $days floor($cdown/DSECS);
$cdown %= DSECS;
if ($detail == 3$hours round($cdown/HSECS);
else $hours floor($cdown/HSECS);
$cdown %= HSECS;
if ($detail == 4$minutes round($cdown/MSECS);
else $minutes floor($cdown/MSECS);
$cdown %= MSECS;
$seconds $cdown;

$tnums = array($years$days$hours$minutes$seconds);
$ttext = array("year""day""hour""minute""second");

$shown 0;

for ($i=0;$i<$detail;$i++) {
if ($tnums[$i]) {
echo "$tnums[$i] $ttext[$i]";
$shown++;
if ($tnums[$i] != 1) echo "s";
$count 0;
for ($j=$i+1;$j<$detail;$j++) {
if ($tnums[$j]) $count++;
}
switch($count) {
case 0: break 2;
case 1: if ($shown>1) echo ","; echo " and "; break;
default: echo ", "; break;
}
}
}
if ($now $then) echo " ago";
if ($now == $then) echo "now";
}
?>


which is then called in the page as

Code: [Select]
<?php countdown(420050716000100); ?>

I think the problem I'm having is getting my head around the dynamic bit. If anyone can help, I'd be hugely grateful!
Title: Re: Help with PHP includes
Post by: Tranz on January 02, 2005, 12:19:19 am
http://coppermine.sourceforge.net/faq.php#customHeader

If you want php code, you need to edit theme.php
Title: Re: Help with PHP includes
Post by: Nibbler on January 02, 2005, 12:24:33 am
You can follow the usual instructions that apply to custom headers. Make sure you include() that file, and call the function. So your pageheader function in your theme.php needs something like

Code: [Select]
include 'that_file.php';
.. a few lines later...
'{COUNTDOWN}' => countdown(4, 2005, 07, 16, 00, 01, 00),

Where {COUNTDOWN} is mentioned somewhere in your theme.html.
Title: Re: Help with PHP includes
Post by: dafyd on January 02, 2005, 12:38:04 am
I've done what you suggested, Nibbler, but for some reason the countdown text appears as the very first thing in the source of the outputted page (you can see what I mean at http://www.july162005.com/gallery/), so it appears right at the top of the page, outside of the body of the document.... I still can't see what I'm doing wrong!

Here's the pageheader function code that I'm using:

Code: [Select]
function pageheader($section, $meta = '')
{
    global $CONFIG, $THEME_DIR;
    global $template_header, $lang_charset, $lang_text_dir;

include 'cdown.php';

    header('P3P: CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE"');
    user_save_profile();

    $template_vars = array('{LANG_DIR}' => $lang_text_dir,
        '{TITLE}' => $CONFIG['gallery_name'] . ' - ' . $section,
        '{CHARSET}' => $CONFIG['charset'] == 'language file' ? $lang_charset : $CONFIG['charset'],
        '{META}' => $meta,
        '{GAL_NAME}' => $CONFIG['gallery_name'],
        '{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
        '{MAIN_MENU}' => theme_main_menu(),
        '{ADMIN_MENU}' => theme_admin_mode_menu(),
        '{COUNTDOWN}' => countdown(4, 2005, 07, 16, 00, 01, 00),
        );

    echo template_eval($template_header, $template_vars);
}

and {COUNTDOWN} is called in the page where I want it to appear.

I really don't understand why it doesn't work!
Title: Re: Help with PHP includes
Post by: Tranz on January 02, 2005, 12:42:01 am
Could you please post the code that includes {COUNTDOWN} so we can see its placement?

Also, where do you want it placed?
Title: Re: Help with PHP includes
Post by: kegobeer on January 02, 2005, 12:44:14 am
Have your function return a value instead of echoing the values, ie: return $somevalue;
Title: Re: Help with PHP includes
Post by: Joachim Müller on January 02, 2005, 12:46:18 am
refer to http://forum.coppermine-gallery.net/index.php?topic=13001.msg59293#msg59293 or http://forum.coppermine-gallery.net/index.php?topic=9863.msg44419#msg44419 - in those threads I walked two users through custom header setups (that's something your search should have come up with).

Joachim
Title: Re: Help with PHP includes
Post by: Nibbler on January 02, 2005, 12:47:01 am
Try like this:

Code: [Select]
function pageheader($section, $meta = '')
{
    global $CONFIG, $THEME_DIR;
    global $template_header, $lang_charset, $lang_text_dir;

    ob_start();
    include 'cdown.php';
    countdown(4, 2005, 07, 16, 00, 01, 00);
    $countdown = ob_get_clean();

    header('P3P: CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE"');
    user_save_profile();

    $template_vars = array('{LANG_DIR}' => $lang_text_dir,
        '{TITLE}' => $CONFIG['gallery_name'] . ' - ' . $section,
        '{CHARSET}' => $CONFIG['charset'] == 'language file' ? $lang_charset : $CONFIG['charset'],
        '{META}' => $meta,
        '{GAL_NAME}' => $CONFIG['gallery_name'],
        '{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
        '{MAIN_MENU}' => theme_main_menu(),
        '{ADMIN_MENU}' => theme_admin_mode_menu(),
        '{COUNTDOWN}' => $countdown,
        );

    echo template_eval($template_header, $template_vars);
}
Title: Re: Help with PHP includes
Post by: dafyd on January 02, 2005, 12:52:07 am
Nibbler - that works, thank you ever so much!

GauGau - I did search, and I did follow the steps you laid out as best I could, but I came to an impasse. That's why I came to the forum for help. Sorry to be an incovenience to you. You advice in the previous posts was very useful for clearing up some other problems, though - thank you.