Jump to content

Php eval, functions and variables

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hi there,

Working on a widget system for my template system and made it that all widgets divs html/php are saved inside variables. Though one of these requires another file to be included ( and the output to be saved inside the widget variable ), which doesn't seem to work. This is the codes it's regarding to:

sidebar.php
$widgets['mainPolls'] = "
<div class='rect'> <span class='heading'>Polls</span> <p>
".getMainPolls(TRUE)."
</div>
";

system.class.php - (important part of function getMainPolls() : what it returns)
return eval( 'include($set[\'cms_absolutepath\']."/templates/".$set[\'cms_template\']."/poll.php");');

And the function that gets the widget variables and outputs them in correct order @ correct hook:

system.class.php
public function getWidgets($hook) {
global $set;
include($set['cms_absolutepath']."/templates/".$set['cms_template']."/sidebar.php"); 
//hook would be for ex. 'sidebar-left' or 'sidebar-right'
$select_widgets = mysql_query("SELECT * FROM widgets WHERE template = '".$set['cms_template']."' AND hook = '$hook' ORDER BY orderid")or die(mysql_error());
while($widget = mysql_fetch_assoc($select_widgets)) {
echo $widgets[$widget['name']];
}
}

What it currently outputs for the poll widget ( which includes another file's content into the variable of the widget using eval ) is the content of the poll file included from the getMainPolls function unalligned at top of the page and then the div ( surrounding the getMainPolls file and so the div in which this content SHOULD be alligned/put in ) hanging on bottom of page alligned under the other divs ( but empty ).

Anyone can help me?

Thanks in advanced!


EDIT: Seems the eval makes it include the file content first instead of outputting the file content inside the variable $widgets['poll']... how do I fix this? ( make it put the content of the file inside the variable at the place of where the function getMainPolls is called inside the var ).

#2
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Finally found a great solution for this on internet, been searching for this for long time, finally found!

This way I can still use include but set a buffer which puts all incoming data into a variable:

ob_start(); // start buffer
include ($set['cms_absolutepath']."/templates/".$set['cms_template']."/poll.php");
$content = ob_get_contents(); // assign buffer contents to variable
ob_end_clean(); // end buffer and remove buffer contents

Got it working now! =]
</SPAN>