PHP box

Creating and modifying plugins.
Post Reply
Xanthouos
Regular
Posts: 115
Joined: Wed Mar 02, 2005 6:59 pm
Contact:

PHP box

Post by Xanthouos »

I have daily quote script that I use on other sites, but am having problems emplementing it in the sidebar php box plugin.
The script works, but isn't able to resolve the path to the quotes file.


This is the code in the php box:

Code: Select all

  include "inc/displayer.php";

 echo($display1);
and this is the line in the 'displayer.php" that calls the quotes file:

Code: Select all

$file1 = "quotes.txt";  // Files containing your tags
I tried adding the url, the full path, one or more "../", but it's never able too find it.
Any ideas how to make it work?

Thank you.

BTW, it's on version 8 at http://www.familyafrica.com
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: PHP box

Post by garvinhicking »

What does the full code of displayer.php look like? It seems your quote script is not able to use full paths.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Xanthouos
Regular
Posts: 115
Joined: Wed Mar 02, 2005 6:59 pm
Contact:

Post by Xanthouos »

I would like to continue using this script since I already have the specific quotes in the file, and would like to emplement it on several s9y sites.

You can see it in action here.

Thank you for the help.

Code: Select all

<script language="php">

/* *****************************************************************
        PHP-DISPLAYER V-2.02 (c) 2002 Peter Kerl

        Tips, Tags, Quotes and Pics for Webpages. Examples
        and more Quote-Files you will find at:
        http://passepartout.peterkerl.de
***************************************************************** */

//------------------------------------------------------------------
/*  DEFAULT-SETTINGS  */

// please don't forget to put a link to my site

$displayNo = 1; // Number of displayed tags/pics

$file1 = "inc/quotes.txt";  // Files containing your tags
// $file2, $file3 for further displays, otherwise $file1 is default

$mode1 = "random";  // "sequence", "random" or "periodical"
// $mode2, $mode3 for further displays, otherwise $mode1 is default

$period1 = "wday";     // the measure for changes in "periodical"-mode.
/*         "wday" = day of week (7); "mday" = day of month (31);
        "yday" = day of year (365); "hours" = hour of day (24);
        "weeks" = week of year (53)  */
// $period2, $period3 for further displays
$weekstart1 = "sun"; //only in "weeks"-period. Determines the day of change.
/*        "sun", "mon", "tue", "wed", "thur", "fri", "sat" */
// $weekstart2, $weekstart3 etc.

// general settings:
$sep = "%%";  // Signs used to separate the tags, according to your tag-files.
$lineBreak = 1; // automatic Linebreaks for simple text-displays,
                // otherwise set $lineBreak = 0
$startNo = 1;         // only in sequence-modus
$deleteRepeats = 50;  // only random-modus. 10 times without duplicates


//-------------------------------------------------------------------


/*  The CODE, do only change, if you know what you do  */

// overrule default-settings:
if (isset($o_displayNo)) $displayNo = $o_displayNo;
if (isset($o_lineBreak)) $lineBreak = $o_lineBreak;
if (isset($o_sep)) $sep = $o_sep;
if (isset($o_startNo)) $startNo = $o_startNo;
if (isset($o_deleteRepeats)) $deleteRepeats = $o_deleteRepeats;
$indexdata = ""; $randomdata = ""; $data = "";

for ($a=1; $a<=$displayNo; $a++) {

        $display = "display$a";
        $file = "file$a";
        $o_file = "o_file$a";
        $mode = "mode$a";
        $o_mode = "o_mode$a";
        $period = "period$a";
        $o_period = "o_period$a";
        $weekstart = "weekstart$a";
        $o_weekstart = "o_weekstart$a";
        $index = "index$a";
        $x = "x$a";
        $x_sum = "x_sum$a";

        if (isset($$o_file)) $$file = $$o_file;
        if (!isset($$file)) $$file = $file1;
        if (isset($$o_mode)) $$mode = $$o_mode;
        if (!isset($$mode)) $$mode = $mode1;

        $tags = FileToArray($$file);

        if (is_array($tags)) {

                if ($$mode == "sequence") {
                        if (! isset($$index)) {$$index = $startNo - 1;}
                        if ($$index < 0 || $$index >= count($tags)) {$$index = 0;}
                        ${$x}[] = $$index;

                        $$display = formatText($tags[$$index]);
                        $$index++;
                }

                elseif ($$mode == "random") {
                        srand ((double) microtime() * 10000000);

                        if (! isset($$index)) {$$index = getRKey($tags);}
                        ${$x}[] = $$index;
                        $$display = formatText($tags[$$index]);

                        if ($deleteRepeats > count($tags)) {$deleteRepeats = count($tags);}
                        if (count(${$x}) >= $deleteRepeats) {${$x} = array($$index);}
                        $$index = deleteReps($tags, $$x);
                }

                else {  // periodical
                        if (isset($$o_period)) $$period = $$o_period;
                        if (!isset($$period)) $$period = $period1;
                        if (isset($$o_weekstart)) $$weekstart = $$o_weekstart;
                        if (!isset($$weekstart)) $$weekstart = $weekstart1;

                        $t_stamp = time();
                        $arrDate = getdate($t_stamp);
                        if ($$period == "weeks") $number = getWeek($$weekstart);
                        else $number = $arrDate[$$period];
                        if ($$period == "mday" || $$period == "hours") $number--;

                        $max = count($tags);

                        if ($number >= $max) {$number %= $max;}
                        elseif ($number < 0) {$number = $max - 1;}
                        $$display = formatText($tags[$number]);
                }

        }
        else {
                $$x = array(0);
                $$display = "File \"${$file}\" not found";
        }

        $$x_sum = "";
        if ($deleteRepeats > 1 && $$mode == "random") {
                reset ($$x);
                while (list(, $value) = each ($$x)) {
                        ${$x_sum} .= "&x$a%5B%5D=$value";
                }
                $randomdata .= ${$x_sum};
        }
        if ($a == 1) $indexdata = "index$a=${$index}";
        else $indexdata .= "&index$a=${$index}";


} // end for

$data = "?$indexdata$randomdata";


function FileToArray($filename) {
        global $sep;
        if (file_exists($filename)) {
                if (filesize($filename) == 0) return 0;
                $fcontent = join ('', file ($filename));
                return explode ($sep, $fcontent);
        }
        return 0;
}

function deleteReps($tags, &$x) {

        $loops = 0;
        $maxLoops = 15;
        do {
                $ndx = getRKey($tags);
                $found = 0;
                $loops++;
                reset ($x);
                while (list(, $value) = each ($x)) {
                        if ($ndx == $value) {
                                $found = 1;
                                break;
                        }
                }

        } while ($found == 1 && $loops < $maxLoops);
        return $ndx;
}

function getRKey($arr){
        $c = count($arr) - 1;
        return rand(0, $c);
}

function formatText($text) {
     global $lineBreak;
     $text = trim($text);

     if ($lineBreak == 0) return $text;

     $text = str_replace("\r\n\r\n", "<p>", $text);
     $text = str_replace("\r\n", "<br>", $text);
     $text = str_replace("\n\n", "<p>", $text);
     $text = str_replace("\n", "<br>", $text);
     $text = str_replace("\t", "   ", $text);
     $text = str_replace("  ", "  ", $text);
     return $text;
}

function getWeek($wstart) {
        global $arrDate;

        $YD = $arrDate["yday"];
        $Y7 = ($YD % 7) + 1;
        $YD++;
        $WD = $arrDate["wday"];

        if ($wstart == "sun") $wstart = 0;
        elseif ($wstart == "mon") $wstart = 1;
        elseif ($wstart == "tue") $wstart = 2;
        elseif ($wstart == "wed") $wstart = 3;
        elseif ($wstart == "thur") $wstart = 4;
        elseif ($wstart == "fri") $wstart = 5;
        elseif ($wstart == "sat") $wstart = 6;

        $WD = $WD - $wstart;
        if ($WD < 0) $WD += 7;
        $WD++;

        $week = ceil($YD / 7);
        if ($WD < $Y7) $week += 1;

        return $week - 1;
}
</script>
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Okay, the code looks okay.

If you use $file1 = "/absolute/path/to/s9y/quote.txt" it MUST really work. Do you not get ANY code displayed, or do you see any error? Did you try any other PHP box include stuff that already worked? I am not sure but maybe you need to omitthe <script> tags at the beginning and end of the script.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Xanthouos
Regular
Posts: 115
Joined: Wed Mar 02, 2005 6:59 pm
Contact:

Post by Xanthouos »

When using the absolute path I get the following error:

Code: Select all

Warning: explode(): Empty delimiter. in /home/family/public_html/inc/displayer.php on line 146
File "/home/family/public_html/inc/quotes.txt" not found
..and if I omit the <script> tag, it displays the whole code in the file.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Ah,now I see the problem. It's the "GLOBAL" statement in the functions.

You'll need to modify your script to not use globals, or declare all globals in the php box plugin in the generate_content() method...

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Xanthouos
Regular
Posts: 115
Joined: Wed Mar 02, 2005 6:59 pm
Contact:

Post by Xanthouos »

garvinhicking wrote: You'll need to modify your script to not use globals, or declare all globals in the php box plugin in the generate_content() method...
And how do I go about doing that? My knowlage of PHP is still quite limited.
Thank you so much!


BTW, what do you think of my implementation of s9y at FamilyAfrica.com
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

I normally don't recode foreign code, but here - try this: http://nopaste.php-q.net/137283 I hope it works, I removed the global references.

Your site looks very unique, seems you really customized Serendipity to a good level. Great! :-)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Xanthouos
Regular
Posts: 115
Joined: Wed Mar 02, 2005 6:59 pm
Contact:

Post by Xanthouos »

garvinhicking wrote:I normally don't recode foreign code, but here - try this: http://nopaste.php-q.net/137283 I hope it works, I removed the global references.
Thank you Garvin, it really means a lot to me that you took your time for this. It worked like a charm. :wink:
(If you like, you can re-visit the site and evertime you refresh, it loads a new quote.)
garvinhicking wrote: Your site looks very unique, seems you really customized Serendipity to a good level. Great! :-)

Regards,
Garvin
Wow, that was encouraging. Again thank you!
Post Reply