12 Replies - 550 Views - Last Post: 29 August 2009 - 06:10 AM Rate Topic: -----

#1 dreadfear  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 08-December 08

Inserting outputted code into a variable

Posted 28 August 2009 - 03:16 AM

function willitwork(){
 while($row1 = mysql_fetch_array($list_of_filehosts)){
	 echo ("<option>" . $row["TAGS"] . "</option>");
	 }
}


I need that code to be inserted into a variable, ive tried using external files but i cant seem to get it.

Thanks to anyone who can share there knowledge
Is This A Good Question/Topic? 0
  • +

Replies To: Inserting outputted code into a variable

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 04:20 AM

Do you want a ) to put the code as a string in a variable or b ) simply just what it outputs? or c ) the function as a function?

if a ) wrap it inside quotes and assign it to a variable, note that you have to escape the dollar signs.
if b ) either just add what you echo to a variable instead of outputting it or take a look at the ob_start() / ob_get_contents() functions.
if c ) you should rather try to turn it into a class.

View Postdreadfear, on 28 Aug, 2009 - 12:16 PM, said:

function willitwork(){
 while($row1 = mysql_fetch_array($list_of_filehosts)){
	 echo ("<option>" . $row["TAGS"] . "</option>");
	 }
}


I need that code to be inserted into a variable, ive tried using external files but i cant seem to get it.

Thanks to anyone who can share there knowledge

This post has been edited by Wimpy: 28 August 2009 - 04:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 dreadfear  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 08-December 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 04:23 AM

I want it so that when it outputs onto the other page, it will work like html works. Ill try escaping the dollar signs

 $wasd = "while(" . $row1 . " = mysql_fetch_array(" . $list_of_filehosts . ")){
	 echo ('<option>'" . $row1["TAGS"] . "</option>');
	 }";


Tried that, it didnt work. Just outputted while( etc

Just looked at ob_start, cant find any good resources. At the moment it makes no sense to me, can you explain it or give me an example?

Thanks

This post has been edited by dreadfear: 28 August 2009 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 05:10 AM

You know that PHP doesn't run client side, right? You can't output it to a page as anything other than data. You could include it to run prior to output, or maybe you want to convert function to be used as part of a javascript?
Was This Post Helpful? 0
  • +
  • -

#5 dreadfear  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 08-December 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 05:13 AM

I dont get what you mean, i just need the output code of the function put onto the page.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 05:19 AM

Then call the function.

Include it and call it or have it as part of your page and call it. Either way, a function does nothing unless its called.

This post has been edited by CTphpnwb: 28 August 2009 - 05:19 AM

Was This Post Helpful? 1
  • +
  • -

#7 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 150
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 07:11 AM

like CT said functions don't run until you make it run so maybe this will help you understand better ...

<?php
function willitwork(){
 while($row = mysql_fetch_array($list_of_filehosts)){
	 echo ("<option>" . $row["TAGS"] . "</option>");
	 }
}
?>

<select name="itshouldwork">
   <?php willitwork(); ?>
</select>



or just put it in line and let it work that way

<select name="itshouldwork">
<?php
 while($row = mysql_fetch_array($list_of_filehosts)){
	 echo ("<option>" . $row["TAGS"] . "</option>");
	 }
?>
</select>



what i think you were trying to do is make a single var to call later ...

$myOptions = "";
 while($row = mysql_fetch_array($list_of_filehosts)){
	 $myOptions .= "<option>" . $row["TAGS"] . "</option>";
	 }



where as you can use it later like

<select name="itshouldwork">
<?php echo $myOptions; ?>
</select>



Simplest method of all these is to just run the code inline unless of course your using the SAME options for multiple selects on the SAME page

EDIT: i just noticed you have $row1 = ... and than you call it using $row ... watch your variable assignments ... they can get you in trouble

This post has been edited by RPGonzo: 28 August 2009 - 07:16 AM

Was This Post Helpful? 1
  • +
  • -

#8 dreadfear  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 08-December 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 03:33 PM

Ok, i should of exlpained this better.

The code i have has to be put into a variable, which then needs to be put into an array.

The array needs to be called on a .tpl file for a phpbb engine. I cant use any php code in the .tpl code, i can only call the array.

So i need the code to run in the external file, grab the html code it outputs, insert it into the array then call it like this.
	   <select name="dropdown">
			{SOURCE_TAG}
		   </select>

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 05:33 PM

It sounds like you're trying to create a template file, in which case your php code should read the html file and search/replace for template codes before outputting. If you do it your way then the purpose of a template is defeated by running php code within it.
Was This Post Helpful? 1
  • +
  • -

#10 dreadfear  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 08-December 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 05:42 PM

Im not creating a file.

Im using phpbb, im editing search.php with php code, which then interacts with search_body.tpl.

Im not running php code on the .tpl file, im running the php code on search.php then inseting it into an array, which is then called by search_body.tpl so the value of the array will appear on the search_body.tpl file.

	   <select name="dropdown">
	   {SOURCE_TAG}
		</select>


{SOURCE_TAG} Is part of the array where my data is stored. That html code above is on the search_body.tpl.
I need that array to have the outputted html code in there that

$list_of_filehosts = mysql_query("SELECT `TAGS` FROM `tags`");
while($row = mysql_fetch_array($list_of_filehosts)){
	 echo ("<option>". $row["TAGS"] . "</option>");
	 }

Will generate

I hope ive explained it well enough this time
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 06:50 PM

I think you're looking for something like this:

<?php
$phpbb = array(
"{sometag}" =>"<b>Hello!</b>",
"{headers}"=>'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<title>My Title</title></head><body>
',
"{footer}"=>'</body></html>',
"{SOURCE_TAG}" => ''
);
function mytemplate($replace_array, $template_data)
{
	$search = array();
	$replace = array();
	foreach($replace_array as $key => $value)
	{
		$search[] = $key;
		$replace[] = $value;
	}
	return str_replace($search, $replace, $template_data);
}

function sourcetag()
{
	// Don't forget code to connect to database and select table!
	$output = '';
	$list_of_filehosts = mysql_query("SELECT `TAGS` FROM `tags`");
	while($row = mysql_fetch_array($list_of_filehosts)){
		$output .= "<option>". $row["TAGS"] . "</option>";
	}
	return $output;
}

$tpl_file = '{headers}{sometag}{SOURCE_TAG}{footer}';  // You would read the file here.

$phpbb["{SOURCE_TAG}"] = sourcetag();

$content = mytemplate($phpbb, $tpl_file);

echo $content;

?>

Was This Post Helpful? 1
  • +
  • -

#12 dreadfear  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 08-December 08

Re: Inserting outputted code into a variable

Posted 28 August 2009 - 09:06 PM

Ok thanks, it works. But it doesnt work. It returns nothing, however i was also having that problem when i was just trying to fill it using an array as opposed to the mysql database. I think this might be something else.

Thanks for your help :)
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Inserting outputted code into a variable

Posted 29 August 2009 - 06:10 AM

Did you run it as I posted it? It works for me. You need to make sure the the array $phpbb contains all the tags you're using and the information you want to use. That information doesn't have to be static. It could come from a variable:

$phpbb['{A_tag}'] = $some_variable;

The .= is shorthand.

$output .= "something";
is the same as:
$output = $output."something";
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1