Welcome to Geeklog Saturday, September 21 2019 @ 07:53 pm EDT

Geeklog Forums

Help needed w/ block position


Theophile

Anonymous
I am trying to get working a block that will display a random item from a CafePress store. Here is what I have in lib-common.php:
PHP Formatted Code
function phpblock_randomitem()
// SET VARIABLES //

{
srand ((float) microtime() * 10000000);

$start = '<!-- ### Items ### -->';

$end = '<!-- ### end of ITEMS ### -->';

$stores = 'themerediths';

$url = 'http://www.cafepress.com/' . $stores;

$itemfile = 'cpitemfile.inc';

$mode = $_SERVER['QUERY_STRING'];



if ($mode == "update") {

        echo "Beginning CP item file update...<br>\n";



        // connect to CP

        $reqheader = "GET /$stores HTTP/1.0\r\nHost: www.cafepress.com\r\nUser-Agent: MS Internet Explorer\r\n\r\n";

        $socket = @fsockopen("www.cafepress.com", 80, &$errno, &$errstr);

        if ($socket)

        {

                fputs($socket, $reqheader);

                while (!feof($socket))

                {

                        $cpfile .= fgets($socket, 4096);

                }

        }

        fclose($socket);





        $items = eregi("$start(.*)$end", $cpfile, $cparray);



        $newcparray = split ("\n", $cparray[1]);



        foreach ($newcparray as $line) {

        if (strlen($line) > 100)

                {

                $newestcparray[] = $line;

                }

        }



        $handle = fopen($itemfile, "w");

        foreach ($newestcparray as $line) {

        $line = ereg_replace("<td align=\"center\" valign=\"top\"><p>","",$line);

        $line = ereg_replace("</td>","",$line);

        $line = ereg_replace("150","100",$line);

        $line = ereg_replace("<b>(.*)</b>","",$line);

//      $line = ereg_replace("\\$(.*)\r", "\r", $line);  // Uncomment to hide prices

        $line = ereg_replace("prod.aspx\?","prod&",$line); //this line is just used to integrate with custom store. delete to use regular cp store.

        if (eregi("<td colspan=3>",$line)) {unset ($line);}

        if (eregi("<tr>",$line)) {unset ($line);}

        $line = ereg_replace("<a href=\"","<a href=\"http://www.themerediths.org/store.php?item=",$line);

        $line = ereg_replace("src=\"/cp/img","src=\"http://www.cafepress.com/cp/img",$line);

        if (!empty($line)) {

                $lastcparray[] = $line;

                fwrite($handle, $line);

                }

        }

        fclose($handle);

        echo "CP item file update completed.\n";

}

else {

        $itemhandle = fopen($itemfile, "r");

        $itemarray = fread($itemhandle,filesize("$itemfile"));

        fclose($itemhandle);

        $lastcparray = split ("\r", $itemarray);



        $item = $lastcparray[rand(0,count($lastcparray)-2)];

        $item = ereg_replace("[\x27]",'',$item);

        $item = trim($item);



        //for javascript use:

        //echo "document.write('<div align=\"center\">" . $item . "</div>');";



        //for php use:

        echo $item;

}
}
 

It is working, except is is putting the block contents at the very top of the page, above the header. Looking at the rendered HTML, it places the code rendered from this block on the very top line of the HTML, above even the HTML tag.

Anyone know what needs to be done?

Thanks!

Status: offline

Dirk

Site Admin
Admin
Registered: 12/01/02
Posts: 13073
Location:Stuttgart, Germany
Don't use echo in a phpblock. You have to return the contents of the block and let Geeklog display it.

bye, Dirk

Theophile

Anonymous
Ah yes. that is much better.

I must confess I know nothing about php. I did not write this script, I only got it from somewhere else. The only thing I need to figure out now is how to center the contects of the block and how to add a line of text with a hyperlink underneath the block contents.

I want to add a link to the store front underneath the random item in the block.

Would anyone mind filling me in on how to do that? Thanks!

All times are EDT. The time is now 07:53 pm.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content