syntax: returning php code from an external function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

syntax: returning php code from an external function

Post by taldos »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


K, another syntax question.  I am trying to use a function to return php in order that will in turn replace variables within a template.

The following is the code I am using to set up the template file

Code: Select all

$t->set_file(array("PAGE" => "roundtablesThread.tpl"));
$t->set_block("PAGE", "THREADS_ROW", "BROWSE_OUTPUT");
$t->set_block("PAGE", "AH_ROW", "AH_OUTPUT");

//bellow is the call to the function, in same page as the template setup

thread_display ($_REQUEST['cid'], 0, $moderator);
And the last piece of code is the function itself which is in my functions file

Code: Select all

function thread_display($global, $parent, $moderator)
{
	$sql = "SELECT title, thread_id, author, date FROM roundtable_text WHERE (category_id='".$global."' AND parent_id='".$parent."') ORDER BY date DESC";
	$result = mysql_query($sql)
        or diet("Could  not select discussion thread from DB");
        
        while ($row = mysql_fetch_array($result)) 
   	    {
   	    	display($row['title'], $row['author'], $row['date'], $moderator, $global, $row['thread_id']);
   	    	if($row['child_id'] == 1)
   	    	{
   	    		thread_display($global, $row['thread_id'], $moderator);
   	    	}
   	    }
   	return;
}

function display($title, $author, $date, $moderator, $global, $disp_thread)
{
	if($moderator)
	{
		$t->set_var(array("TITLE" => $title));
		$t->set_var(array("AUTHOR" => $author));
		$t->set_var(array("DATE" => $date));
		$t->set_var(array("TRASH" => "<a href=discussionDelete.php?cid=".$global."&tid=".$disp_thread."> <img height =11px src=http://www.vicouncil.org/templates/images/business/trash.gif border=0> </a>"));
		$t->parse("BROWSE_OUTPUT", "THREADS_ROW", true);
	}
	else
	{
		$t->set_var(array("TITLE" => $title));
		$t->set_var(array("AUTHOR" => $author));
		$t->set_var(array("DATE" => $date));
		$t->parse("BROWSE_OUTPUT", "THREADS_ROW", true);
	}
	return;
}
I am getting an error with the engine telling me that using the "$t" variable within the display function is invalid, which I understand because it has not been declared within the scope of that function. Now my question is how to I get this function to return code to that will then parse the information to the template file. I am sure it is a syntax issue, but since I am a newbie I ask your indulgence. :oops:

Best,

Ed.


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

global $t
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

fresh pair of eyes

Post by taldos »

I used globa, and that is working fine. however I have come across another problem. and I think a fresh pair of eyes would do the trick.

Code: Select all

<?php
function thread_display($cat_id, $parent, $moderator)
{
	$threadsql = "SELECT date, title, thread_id, name FROM roundtable_text WHERE (category_id='".$cat_id."' AND parent_id='".$parent."') ORDER BY date DESC";
	$threadresult = mysql_query($threadsql)
        or diet("Could  not select actual PEACE browse list from DB");
        
        while ($row = mysql_fetch_array($threadresult)) 
   	    {
     		if($moderator)
			{
				global $t->set_var(array("TITLE" => $row['title']));  //<----- this is where I am getting a parse error; parse error, expecting `','' or `';'' 
				global $t->set_var(array("AUTHOR" => $row['name']));
				global $t->set_var(array("DATE" => $row['date']));
				global $t->set_var(array("TRASH" => "<a href=discussionDelete.php?cid='".$cat_id."'&tid='".$row['thread_id']."'> </a>"));
				global $t->parse("BROWSE_OUTPUT", "THREADS_ROW", true);
			}
			else
			{
				global $t->set_var(array("TITLE" => $row['title']));
				global $t->set_var(array("AUTHOR" => $row['name']));
				global $t->set_var(array("DATE" => $row['date']));
				global $t->parse("BROWSE_OUTPUT", "THREADS_ROW", true);
			}
			   
			if($row['child_id'] == 1)
   	    	{
   	    		thread_display($cat_id, $row['thread_id'], $moderator);
   	    	}
   	    }
   	return;
}
this script shoudl essenstially loop through the database and pull threads. displaying top tier threads first then if a child exists, displaying the child until completion.

if anyone can point me to a working script of threads I can take a look at it and maybe figure it out.

thanks.

ed.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: fresh pair of eyes

Post by feyd »

Code: Select all

<?php
function thread_display($cat_id, $parent, $moderator)
{
	global $t;
	$threadsql = "SELECT date, title, thread_id, name FROM roundtable_text WHERE (category_id='".$cat_id."' AND parent_id='".$parent."') ORDER BY date DESC";
	$threadresult = mysql_query($threadsql)
        or diet("Could  not select actual PEACE browse list from DB");
        
        while ($row = mysql_fetch_array($threadresult)) 
   	    {
     		if($moderator)
			{
				$t->set_var(array("TITLE" => $row['title']));  //<----- this is where I am getting a parse error; parse error, expecting `','' or `';'' 
				$t->set_var(array("AUTHOR" => $row['name']));
				$t->set_var(array("DATE" => $row['date']));
				$t->set_var(array("TRASH" => "<a href=discussionDelete.php?cid='".$cat_id."'&tid='".$row['thread_id']."'> </a>"));
				$t->parse("BROWSE_OUTPUT", "THREADS_ROW", true);
			}
			else
			{
				$t->set_var(array("TITLE" => $row['title']));
				$t->set_var(array("AUTHOR" => $row['name']));
				$t->set_var(array("DATE" => $row['date']));
				$t->parse("BROWSE_OUTPUT", "THREADS_ROW", true);
			}
			   
			if($row['child_id'] == 1)
   	    	{
   	    		thread_display($cat_id, $row['thread_id'], $moderator);
   	    	}
   	    }
   	return;
}
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

thanks :-)

Post by taldos »

Thanks a lot for the help. Though I tried to search the new code to see the difference I failed to see it. I had to resort to copying and pasting. if you could point out my error I would appreciate it, so next time I would be bugging you.

thanks again,

Ed.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

removed the 'global' you had in front of each $t and placed it at the beginning of the function, where it's supposed to be. :)
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Thanks

Post by taldos »

Muchos gracias oh devine one. much appreciated.
Post Reply