PDA

View Full Version : PHP in module - just cant get it?


maximux1
06-15-2004, 09:32 PM
I have a very simple PHP RSS reader that I want to include in a module.


//marijuana_newsfeed.php

<?
include('/some/path/html/RSS/RSS.php');
$rss = new RSS();
$rss->display('http://www.domain.com/path/to/newsfeeds/aggragator.php');
?>



Here is the output of the above script when called directly;

http://www.marijuana.com/420/modules/marijuana_newsfeed.php

Script works, modules dont?

I've tried to create a new module, then use the drop down menu to select 'marijuana_newsfeed.php' and regardless of where I place the module on the page the php output always shows up at the very top of the page, prior to navbar or anything else.

My first thought was a broken table somewhere, so I removed all table formatting from the php script output - That made no difference.

Is there no way to just do a nice "include()" call into a module and spit out the contents of the php script? I've been working on this a few hours and have tried everything in the vBa CMPS manual - even changing any echo() statements to '$variable .=' and using $variable in a custom template, as suggested in a thread I researched.

If anyone has any ideas on this, I would greatly appreciate your help!

Thanks,
Max

maximux1
06-15-2004, 10:33 PM
aiy - this is maddening. I must just be overlooking something simple.

Our Sponsors
 

maximux1
06-16-2004, 12:32 AM
Oh, now I get it -

ok, I have figured my problem out. It mainly had to do with my php outputting straight to the buffer instead of being stored in a string.

Here's my new module php code - I include this when creating a new module in CMPS, via php file. I add 'adv_portal_marijuana_feed' to the cached templates field on the create module page.

<?

ob_start();
include('/SOME/PATH/html/RSS/RSS.php');
$rss = new RSS();
$rss->display('http://www.marijuana.com/newsfeeds/aggragator.php?q=marijuana&num=10&f=marijuana');
$string = ob_get_contents();
ob_end_clean();
eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_marijuana_feed') . '";');

?>

Notice the use of ob_start() and family. This ultimely was the solution to my problem. ob_start() is well documented at php.net and I advise you to read it if you are incorporating your own php modules. In a nutshell, ob_start() (http://www.php.net/manual/en/function.ob-start.php) will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

We can then assisgn the contents of the buffer to $string and use that in our template called from custom php mods.

Hope this helps someone else.

Max
Marijuana.Com

maximux1
06-16-2004, 02:14 AM
ran into another neat little problem of redclaring classes when I tried to include multiple modules using the same php class. Found a nice and simple solution if anyone runs into the same issue -

Simply surround your class with the following code, like so;


if ( !class_exists( myclassname ) ) {

class myclassname {
...
}
}


This just checks to see if the class has already been declared and if so does not redeclare it, thus avoiding the "Class already declared" error.

Our Sponsors
 

Jack_B
10-19-2004, 09:55 AM
I know, this thread is kinda old, even though you helped me out with the same problem. thank you - nice solution ... :)

jugo
10-19-2004, 11:33 AM
Threads like this should be put into a FAQ type of Setting....the most useful threads.....

diades
02-17-2005, 09:44 AM
Hi

Could you help me out here? I seem to be missing something (like my brain).
I have created a php file that deilvers, in string form, markup.
I create a page and a template for it that calls the file from the droplist.
and add adv_portal_mypage to the cached file list.

The script from above I edit thus (in my php file)

<?
ob_start();
$x = "<p>Blah</p>";
$string = ob_get_contents();
ob_end_clean();
eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_mypage') . '";');
?>

am I correct?

Brian
02-17-2005, 09:50 AM
Did you put $string in your 'adv_portal_mypage' template?

diades
02-17-2005, 10:13 AM
Hi Brian

This is what I have:

The page:
Title: Browser Archive Page
Page Identifier:browserarchive
Page Template:adv_portal_browser_archive_page

Module:
Module Title:Browser Archive Repository
File to Include:browserpart.php
Active: Yes
Templates Used:adv_portal_browser_archiv_page

Template:adv_portal_browser_archive_page
content:
.....
<tbody id="collapseobj_opera" style="$collapseobj_opera">
<tr>
<td>
<!--
start adv_portal_browser_archive
-->
$browserString
<!--
end adv_portal_browser_archive
-->
</td>
</tr>
</tbody>

browserpart.php
<?
error_reporting(E_ALL);
ob_start();
include($_SERVER['DOCUMENT_ROOT']."/forums/modules/global_functions.php");
function insertLinks(){
$fname = $_SERVER['DOCUMENT_ROOT']."/forums/modules/browser.xml";
$flag = "r";
$fileHandle = openfile($fname,$flag);
$supplier = getStruct($fileHandle);
$cnt = 0;
foreach($supplier as $browser){
if($browser["tag"] == "BROWSER"){
$cnt++;
}
}
$browserString = '<!-- Start browser input list --><div class="brDiv"><table summary="">';
$name = "";
$type = "";
$version = "";
$country = "";
$browserString = "";
foreach($supplier as $supp){
if($supp["type"] == "open" && $supp["tag"] == "MANUFACTURER"){
$name = $supp["attributes"]["NAME"];
$type = $supp["attributes"]["TYPE"];
$version = $supp["attributes"]["VERSION"];
$browserString .= '<tr><td class="thead">';
$browserString .= $supp["attributes"]["NAME"].' '.$supp["attributes"]["TYPE"].' v'.$supp["attributes"]["VERSION"];
$browserString .= '</td></tr>';
}else if($supp["type"] == "open" && $supp["tag"] == "LANGUAGE"){
$country = $supp["attributes"]["COUNTRY"];
$browserString .= '<tr><td class="alt2">';
$browserString .= $supp["attributes"]["COUNTRY"];
$browserString .= '</td></tr>';
}else if($supp["type"] == "complete" && $supp["tag"] == "BROWSER"){
$browserString .= '<tr><td class="alt1">';
$browserString .= '<a href="'.$supp["attributes"]["HREF"].'" title="'.$supp["attributes"]["BLAH"].'">Download '.$name.' '.$type.' v'.$version.' ('.$country.') '.$supp["attributes"]["BLAH"].'</a>';
$browserString .= '</td></tr>';
}else{
}
}
$browserString .= '</table></div><!-- End browser input list -->';
return $browserString;
}
$browserString = ob_get_contents();
ob_end_clean();
eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_browser_archive_page') . '";');
?>
and ... nothing :(

Brian
02-17-2005, 10:28 AM
Try removing the ob_start() and ob_end_clean() stuff. Then replace this:

$browserString = ob_get_contents();

With this:

$browserString = insertLinks();

And see if that works any better.

diades
02-17-2005, 10:43 AM
Gotcha!


Many thanks Brian!:tup:

diades
02-17-2005, 12:32 PM
Expanding on the theme, if I wanted to return different sections for various parts of the template (One per collapse section) would I have to create a php page and module to include for each or could I return an array and then use
browserString[0] ??

Brian
02-17-2005, 01:00 PM
If I'm thinking right here, you could probably do that by using a foreach statement and faking a moduleid. Something like this...

$fakemodid = 1000;
foreach ($browserString AS $bstring)
{
eval('$home[$fakemodid][\'content\'] = "' . fetch_template('adv_portal_browser_archive_page') . '";');
$fakemodid++;
}

Then replace $browserString in your template with $bstring.

diades
02-17-2005, 01:15 PM
or, perhaps, just add the collapsable elements to the mix and import those also?
$string = '<!-- adv_portal_browser_archive Block -->'
.'<table align="center" border="0" cellpadding="$stylevar[cellpadding]"'
.'cellspacing="$stylevar[cellspacing]" class="tborder"width="100%">'
.'<thead>'
.'<tr>'
.'<td class="tcat">'
.'<a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse('browser')"><img id="collapseimg_feeds" src="$stylevar[imgdir_button]/collapse_tcat$collapseimg_browser.gif" alt="" border="0" /></a>'
.'<span class="smallfont"><strong>$vba_options[portal_blockbullet] Browser archive</strong></span></td>'
.'</tr>'
.'</thead>'
.'<tbody id="collapseobj_browser" style="$collapseobj_browser">'
.'<tr>'
.'<td class="alt2">'
.$browserSting.
.'</td>'
.'</tr>'
.'<tr>'
.'<td class="alt1">'
.'<p style="font-size:0.8em"><a href="http://www.webxpertz.net/index.php?page=browserarchive">Download browsers</a></p></td>'
.'</td>'
.'</tr>'
.'</tbody>'
.'</table>'
.'<br />'
.'<!-- End adv_portal_browser_archive Block -->';

Brian
02-17-2005, 01:23 PM
Should work. :)

diades
02-17-2005, 01:28 PM
I will give it a whirl:D

diades
02-17-2005, 04:23 PM
Hi,

I have edited the script but it causes something nasty enough to cause the page to be mia :eek:
I am wondering if the variables called in the template are causing a problem when supplied in this manner?
<?
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT']."/forums/modules/global_functions.php");
function insertLinks($sName){
$fname = $_SERVER['DOCUMENT_ROOT']."/forums/modules/".$sName.".xml";
$flag = "r";
$fileHandle = openfile($fname,$flag);
$supplier = getStruct($fileHandle);
$cnt = 0;
$templateString = array();
foreach($supplier as $browser)if($browser["tag"] == "BROWSER")$cnt++;
$templateString = '<!-- Start browser input list --><div class="brDiv"><table summary="">';
$type = "";
$version = "";
$country = "";
foreach($supplier as $supp){
if($supp["type"] == "open" && $supp["tag"] == strtoupper($sName)){
$type = $supp["attributes"]["TYPE"];
$version = $supp["attributes"]["VERSION"];
$templateString .= '<tr><td class="thead">';
$templateString .= ucfirst($sName).' '.$supp["attributes"]["TYPE"].' v'.$supp["attributes"]["VERSION"];
$templateString .= '</td></tr>';
}else if($supp["type"] == "open" && $supp["tag"] == "LANGUAGE"){
$country = $supp["attributes"]["COUNTRY"];
$templateString .= '<tr><td class="alt2">';
$templateString .= $supp["attributes"]["COUNTRY"];
$templateString .= '</td></tr>';
}else if($supp["type"] == "complete" && $supp["tag"] == "BROWSER"){
$templateString .= '<tr><td class="alt1">';
$templateString .= '<a href="'.$supp["attributes"]["HREF"].'" title="'.$supp["attributes"]["BLAH"].'">Download '.ucfirst($sName).' '.$type.' v'.$version.' ('.$country.') '.$supp["attributes"]["BLAH"].'</a>';
$templateString .= '</td></tr>';
}else{
continue;
}
}
$templateString .= '</table></div><!-- End browser input list -->';
return $templateString;
}
function fillBlocks(){
$supplier = array(0 => "microsoft",1 => "mozilla",2 => "netscape",3 => "opera");
$blocks = array(insertLinks("microsoft"),insertLinks("mozilla"),insertLinks("netscape"),insertLinks("opera"));
$templateString = "";
foreach($supplier as $key => $manuf){
$templateString .= '<table class="tborder" cellpadding="$stylevar[cellpadding]" width="100%"><thead><tr><td class="tcat"><a href="#top" style="float:$stylevar[right]"';
$templateString .= 'onclick="return toggle_collapse(\''.$manuf.'\')"><img';
$templateString .= 'id="collapseimg_'.$manuf.'"';
$templateString .= 'src="$stylevar[imgdir_button]/collapse_tcat$vbcollapse[collapseimg_'.$manuf.'].gif"';
$templateString .= 'alt="" border="0" /></a><strong>'.ucfirst($manuf).' repository</strong>';
$templateString .= '</td></tr></thead>';
$templateString .= '<tbody id="collapseobj_'.$manuf.'" style="$vbcollapse[collapseobj_'.$manuf.']">';
$templateString .= '<tr><td>'.$blocks[$key].'</td></tr></tbody></table><br />';
}
return $templateString;
}
$templateString = fillBlocks();
eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_browser_archive_page') . '";');
?>

diades
02-17-2005, 06:39 PM
Hi,

I got the page back but I am also now back to square 2, it is displaying at the start of the page.:)
The reason is that a string is not supplied now as the function writes the markup direct to the page. Is there any way to place it where required using this method?

Brian
02-17-2005, 06:55 PM
Can you post the code you have in the file now?

diades
02-17-2005, 07:04 PM
Hi Brian

Yes, here:
<?
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/forums/modules/global_functions.php');
function insertLinks($sName){
$fname = $_SERVER['DOCUMENT_ROOT'].'/forums/modules/'.$sName.'.xml';
$flag = 'r';
$fileHandle = openfile($fname,$flag);
$supplier = getStruct($fileHandle);
$cnt = 0;
$templateString = array();
foreach($supplier as $browser)if($browser['tag'] == 'BROWSER')$cnt++;
$templateString = '<!-- Start browser input list --><div class="brDiv"><table summary="">';
$type = '';
$version = '';
$country = '';
foreach($supplier as $supp){
if($supp['type'] == 'open' && $supp['tag'] == strtoupper($sName)){
$type = $supp['attributes']['TYPE'];
$version = $supp['attributes']['VERSION'];
$templateString .= '<tr><td class="thead">';
$templateString .= ucfirst($sName).' '.$supp['attributes']['TYPE'].' v'.$supp['attributes']['VERSION'];
$templateString .= '</td></tr>';
}else if($supp['type'] == 'open' && $supp['tag'] == 'LANGUAGE'){
$country = $supp['attributes']['COUNTRY'];
$templateString .= '<tr><td class="alt2">';
$templateString .= $supp['attributes']['COUNTRY'];
$templateString .= '</td></tr>';
}else if($supp['type'] == 'complete' && $supp['tag'] == 'BROWSER'){
$templateString .= '<tr><td class="alt1">';
$templateString .= '<a href="'.$supp['attributes']['HREF'].'" title="'.$supp['attributes']['BLAH'].'">Download '.ucfirst($sName).' '.$type.' v'.$version.' ('.$country.') '.$supp['attributes']['BLAH'].'</a>';
$templateString .= '</td></tr>';
}else{
}
}
$templateString .= '</table></div><!-- End browser input list -->';
return $templateString;
}
function createBlocks(){
$supplier = array(0 => 'microsoft',1 => 'mozilla',2 => 'netscape',3 => 'opera');
$blocks = array(0 => insertLinks('microsoft'),1 => insertLinks('mozilla'),2 => insertLinks('netscape'),3 => insertLinks('opera'));
$templateString = '';
$cnt = 0;
foreach($supplier as $manuf){
?>
<table class="tborder" cellpadding="$stylevar[cellpadding]" width="100%">
<thead>
<tr>
<td class="tcat"><a href="#top" style="float:$stylevar[right]"
onclick="return toggle_collapse('<? echo($manuf);?>')"><img id="collapseimg_<? echo($manuf);?>" src="$stylevar[imgdir_button]/collapse_tcat$vbcollapse[collapseimg_<? echo($manuf);?>].gif" alt="" border="0" /></a>
<strong><? echo(ucfirst($manuf));?> repository</strong>
</td>
</tr>
</thead>
<tbody id="collapseobj_<? echo($manuf);?>" style="$vbcollapse[collapseobj_<? echo($manuf)?>">
<tr>
<td><? echo($blocks[$cnt++]);?></td>
</tr>
</tbody>
</table>
<br />
<?
}
// return $templateString;
}
createBlocks();
eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_browser_archive_page') . '";');
?>

I was thinking perhaps of replacing the var in the template with eval('createBlocks()') and eval('$home[$mods[\'modid\']][\'content\'] = "' . fetch_template('adv_portal_browser_archive_page') . '";');

Thanks for you effort in this, it is much appreciated.

Brian
02-17-2005, 07:33 PM
?>
<table class="tborder" cellpadding="$stylevar[cellpadding]" width="100%">
<thead>
<tr>
<td class="tcat"><a href="#top" style="float:$stylevar[right]"
onclick="return toggle_collapse('<? echo($manuf);?>')"><img id="collapseimg_<? echo($manuf);?>" src="$stylevar[imgdir_button]/collapse_tcat$vbcollapse[collapseimg_<? echo($manuf);?>].gif" alt="" border="0" /></a>
<strong><? echo(ucfirst($manuf));?> repository</strong>
</td>
</tr>
</thead>
<tbody id="collapseobj_<? echo($manuf);?>" style="$vbcollapse[collapseobj_<? echo($manuf)?>">
<tr>
<td><? echo($blocks[$cnt++]);?></td>
</tr>
</tbody>
</table>
<br />
<?

Looks like the problem is that bit of code needs to be in a string and that string returned from the function. Then you should be able to use this to put it into the $home array:

$home["$mods[modid]"]['content'] = createBlocks();

diades
02-17-2005, 07:54 PM
Hi Brian

An error here:
foreach($supplier as $manuf){
$templateString .= '<table class="tborder" cellpadding="$stylevar[cellpadding]" width="100%">\n';
$templateString .= '<thead>\n';
$templateString .= '<tr>\n';
$templateString .= '<td class="tcat"><a href="#top" style="float:$stylevar[right]" \n';
$templateString .= 'onclick="return toggle_collapse(\''.$manuf.'\')"><img id="collapseimg_'.$manuf.'" src="$stylevar[imgdir_button]/collapse_tcat$vbcollapse[collapseimg_'.$manuf.'].gif" alt="" border="0" /></a>\n';
$templateString .= '<strong>'.ucfirst($manuf).' repository</strong>\n';
$templateString .= '</td>\n';
$templateString .= '</tr>\n';
$templateString .= '</thead>\n';
$templateString .= '<tbody id="collapseobj_'.$manuf.'" style="$vbcollapse[collapseobj_'.$manuf.'">\n';
$templateString .= '<tr>\n';
$templateString .= '<td>'.$blocks[$cnt++].'</td>\n';
$templateString .= '</tr>\n';
$templateString .= '</tbody>\n';
$templateString .= '</table>\n';
$templateString .= '<br />\n';
}
return $templateString;
}
eval("$home[$mods[\'modid\']][\'content\'] = \'".createBlocks()."\';");
Parse error: parse error, unexpected '[', expecting ']' in /home/virtual/site3/fst/var/www/html/forums/modules/browserpart.php on line 63
which is the eval

Brian
02-17-2005, 08:33 PM
Did you try the code I posted?

diades
02-18-2005, 06:55 AM
Hi Brian

Sorry about the delay, sleep took over and then ssh transport problems on the server:mad:
I thiught that you meant to replace the current $home setion in the eval with the one supplied. I have now tried yours on its own but it just causes all pages from the portal to give a dns error:
Cannot find server or DNS Error

Brian
02-18-2005, 09:02 AM
Hmm... Would you rather just submit a support ticket and let me have a look at things?

diades
02-18-2005, 09:18 AM
Hi Brian

Unfortunately I cannot get to this at the moment as I am on the way out and will not be back till tomorrow. I have fowarded the details of this thread to my partner (username caislander) and asked him to take this up with you until I can return.

caislander
02-18-2005, 02:41 PM
Brian,

I am a bit jammed up at the moment, I will get and account setup for you and either file the ticket or get the info to diades.