Jump to content
xisto Community
bakr_2k5

Variable From Line Further Then Current Line?

Recommended Posts

Hello,

Is it in some way possible to load a variable that further then the current line in the script?
I don't know if you know what i mean so I'll try to point it out in the script below.

1 <?php2	  echo $var1;3	  # $var1 needs to be loaded from foo.php4	 # Lets say i want $var1 to be echoed in <title>$var1</title>5	   - - - -	  bla bla bla55	  - - - -56	  switch($_GET['page']) {57		  case "foo":58			  include("foo.php");59			  #- - - - from foo.php - - - -60				   $var1 = "what ever you want";61			  #- - - - end from foo.php - - - -62			  break;63	  }64 ?>
Well is it possible at all?
I already tried with "ob_start() / ob_end_flush()" but doesn't work either... (understood the purpose of ob_start() a bit wrong :))
If you know a solution or an other way doing this, please tell me :D

bakr_2k5

Share this post


Link to post
Share on other sites

1 <?php2	  echo $var1;3	  # $var1 needs to be loaded from foo.php4	 # Lets say i want $var1 to be echoed in <title>$var1</title>5	   - - - -	  bla bla bla55	  - - - -56	  switch($_GET['page']) {57		  case "foo":58			  include("foo.php");59			  #- - - - from foo.php - - - -60				   $var1 = "what ever you want";61			  #- - - - end from foo.php - - - -62			  break;63	  }64 ?>
Maybe you can move the whole section from line 56 onwards to before "title", or in other words, delay the output of title after line 64. Whatever that's between line 5 to 56, if it's output of html, can be easily move to after line 56.
Another way to prevent such problem next time is to use template engine, or something similiar. I've copied some tactic from some common php template engine and wrote my own one, though less powerful, and not optimize. I split the php and the html. Say if this page is mypage.html, i took out the php code, put inside mypage.php. Replace anything i need to output in the html with {VAR_NAME}, towards the end of the php file, i just read the html into memory, then use str_replace to put in the variable. Then your problem will not appear at all. E.g.
HTML :<title>{TITLE}</title>PHP:str_replace("{TITLE}", $var1, $page);

After that just print out the html in memory. Another advantage of this method is you don't have to repeatedly add section on the page that appear on every other page.

Say you need to print the username at the top of the page. What i did was place the last print out function into another php file, change it into a function, then add
str_replace("{USERNAME}, $username, $page);ORstr_replace("{USERNAME}, $_SESSION['username'], $page);

before the print out function, that's all, then on those page that i need to display the username, just put in {USERNAME}. Provided you've set the $username or session properly

If you suddenly see {SOMETHING}, then you know you've miss something. Or you can place {ANYTHING} at those places you want to put something there, but will only do later, that way you won't forgot to do it.

Another benefit of this method is that you can pass the html to a designer to touch up your page, while they don't have to worry that thet'll break you php codes. At the same time, you can still update your php code without waiting for them.

Share this post


Link to post
Share on other sites

Hi faulty.lee,

 

That's not exactly what i meant,

That switch from line 56 to 63 puts it's content in to a table

-------------------------

| top |

-------------------------

| m | |

| e | |

| n | content |

| u | |

--------------------------

EDIT:

Right ... The ASCII thing doesn't really work the way i want!

END EDIT!

My main page is build up with tables and everything goes from there.

In the content section lays that switch from line 56 to 63, so it loads the page it needs

into the content table.

<?php switch($_GET['page'])   {   case '1' : include('page1.php'); break;   case '2' : include('page2.php'); break;   case '3' : include('page3.php'); break;   # etc.  } ?>
That script is something like mine in the content table. So i can't really move lines 56 to 63 to the top (or some where near) in the script, because the page will be included at the top what's not the purpose of it :)

 

I hope you understand what I mean, I'm from the Netherlands so it could be weird for English people to understand it :D

 

bakr_2k5

Edited by bakr_2k5 (see edit history)

Share this post


Link to post
Share on other sites

From the "english" point of view, that makes 2 of us, i'm not english, either is my mother language. I'm chineseIn that case, i don't think it's possible. Well i might be wrong. So if you use "ob_start() / ob_end_flush()", you'll have to manipulate the output buffer, which i believe would not worth the trouble. As a general programming guideline, if you program need to do some tweak or something ppl seldom do, you show rethink your design, as there should be a better proven method. (there's an original cooler way of saying this, but i forgot how was it :) )In that case, i'll recommend you to try the template method, Won't take too long to convert, well unless you have thousand of files, and in that case you should have been using template in the first place. If you want to try my so call engine, let me know, i can post it here.

Share this post


Link to post
Share on other sites

Well it isn't THAT important :DBut it would be nice if it was possible.If you want to, i would like to try your engine. Maybe I'll get some ideas from it to solve my problem. :)Thank you so far :Dbakr_2k5

Share this post


Link to post
Share on other sites

Not really an engine per se, but didn't know better words. Use at your own risk, and it's not optimized
This is the part in the common file

function template_open($filename){	if(!file_exists($filename))	{		return false;	}	$handle = fopen($filename, "r");	$content = "";	while (!feof($handle)){[code]		$content = $content . fread($handle, 8192);	}	fclose($handle);	return $content;}function output_page(&$page){	global $username;	$page = str_replace("{USERNAME}", $username, $page);	echo $page;}

This part in the page itself "./index.php"
$temp_main = "./templates/index.html";$temp_menu = "./templates/menu.html";$menu = template_open($temp_menu);$page = template_open($temp_main);if ($page){	if ($menu)	{		$page = str_replace("{CONTENT}", $content, $page);		//This one load the "menu" page into "{MENU}" section. 		//More or less like the "include('page1.php')"		$page = str_replace("{MENU}", $menu, $page);		output_page();	}}

If you need to have processing on the"menu.php", then you can encapsulate it into a function which return the processed HTML as $menu.

If you can follow up till here. Then the next part is much more powerful one, it's to generate table with odd or even row. Cause i use diff coloring for each that's why.

This part inside common
function template2_open($filename, &$row_content_odd, &$row_content_even){	if(!file_exists($filename))	{		return false;	}	$handle = fopen($filename, "r");	$content = "";	while (!feof($handle)){		$content = $content . fread($handle, 8192);	}	fclose($handle);	//$content2 = explode("\n", $content);		$start_odd_row_pos = strpos($content, "BEGIN_ODD_ROW");	$start_odd_row_pos = strpos($content, "\n", $start_odd_row_pos);	$end_odd_row_pos = strpos($content, "END_ODD_ROW", $start_odd_row_pos);	$end_odd_row_pos = strpos($content, "\n", $end_odd_row_pos);	$start_even_row_pos = strpos($content, "BEGIN_EVEN_ROW");	$start_even_row_pos = strpos($content, "\n", $start_even_row_pos);	$end_even_row_pos = strpos($content, "END_EVEN_ROW", $start_even_row_pos);	$end_even_row_pos = strpos($content, "\n", $end_even_row_pos);	$row_content_odd = substr($content, $start_odd_row_pos, $end_odd_row_pos - $start_odd_row_pos);	$row_content_even = substr($content, $start_even_row_pos, $end_even_row_pos - $start_even_row_pos);	$content = substr_replace($content, "{ROW_CONTENT}", $start_odd_row_pos, $end_even_row_pos - $start_odd_row_pos);	return $content;}

The part of HTML, i only show the table part
the 4 tag has to be in new line each
<!-- BEGIN_ODD_ROW --><!-- END_ODD_ROW --><!-- BEGIN_EVEN_ROW --><!-- END_EVEN_ROW -->
<table><!-- BEGIN_ODD_ROW -->	<tr> 		  <td>{COUNTER}</td>		  <td>This is odd row</td>		  <td>{DATA}</td>	</tr><!-- END_ODD_ROW --><!-- BEGIN_EVEN_ROW -->	<tr> 		  <td>{COUNTER}</td>		  <td>This is even row</td>		  <td>{DATA}</td>	</tr><!-- END_EVEN_ROW -->

This is the part in the page.
$temp_main = "./templates/index.html";$temp_table = "./templates/table.html";//Use diff function here. and you need to declare the odd and even row first$row_content_odd = "";$row_content_even = "";$row_content = "";$table= template2_open($temp_table, $row_content_odd, $row_content_even);$page = template_open($temp_main);if ($page){	if ($table)	{		$page = str_replace("{CONTENT}", $content, $page);		$row_counter = 0;		for ($row_counter = 0; $counter < 10; $counter++)		{			if ($row_counter % 2 == 0)			{				$row_content_temp = $row_content_odd;			}			else			{				$row_content_temp = $row_content_even;			}			$row_content_temp = str_replace("{COUNTER}", $counter, $row_content_temp);			$row_content_temp = str_replace("{DATA}", "Some data", $row_content_temp);			$row_content .= $row_content_temp;			$row_counter++;		}				//"{ROW_CONTENT}" this one you can't change, as it is inserted by template2_open		$menu = str_replace("{ROW_CONTENT}", $row_content, $menu);		$page = str_replace("{MENU}", $table, $page);		output_page();	}}

That's it, if you happen to improve this, and if you don't can, please share with me or us, the fellow forumer

Good luck.

Share this post


Link to post
Share on other sites

Thank you faulty.lee,I'll take a look at it and see if it's usable in my scripts. It seems a bit complicated.So need to figure out what everything does. But it looks like something very useful! :)bakr_2k5

Share this post


Link to post
Share on other sites

Sorry, i was very lazy at putting in comment. Anyway, feel free to ask if you don't understand. Or just change the coding around to see how it works.That code has works for me for quite sometime, save a lot of hours of programming also. Glad it helps.

Share this post


Link to post
Share on other sites
<html><head><?php	  	  switch($_GET['page']) {		  case "foo":			 include("foo.php");			  #- - - - from foo.php - - - -				  $var1 = "what ever you want";			 #- - - - end from foo.php - - - -			  break;	 }	 echo "<title>$var1</title>;?>
Edited by CrazyPensil (see edit history)

Share this post


Link to post
Share on other sites

<html><head><?php	  	  switch($_GET['page']) {		  case "foo":			 include("foo.php");			  #- - - - from foo.php - - - -				  $var1 = "what ever you want";			 #- - - - end from foo.php - - - -			  break;	 }	 echo "<title>$var1</title>;?>
This won't work ... foo.php is for example news.php it contains < $var1 = "news" >
Then <title>*Sitename* || <?php echo $var1 ?></title>...
So your code wouldn't work ... But it isn't possible the way i want.

Thanks for trying anyways.

Bakr_2k5

Share this post


Link to post
Share on other sites

I don't know how to do this either. I was thinking javascript with document.title to change it dynamically, but it doesn't change the title on the fly. Well it does, but not visually, well it did and didn't. I think the form has to resubmit in order for that to happen for all browsers to do it correctly. You can attempt it however.

Place that into your content includes...well a modified version of that.

<?php $SomeVariable = 'This would be the title'; ?><!-- This goes inside your include file with your variable--><script type="text/javascript" language="JavaScript"><!--document.title='<?php echo $SomeVariable;?>';--></script>

For testing purposes here's what I used
<html><head><title>Some Title</title></head><body><div>Example</div><script type="text/javascript" language="JavaScript"><!--document.title='Other Title';--></script></body></html>

Note: This presents 2 problems.
1.) User does not have javascript enabled.
2.) In IE user gets that annoying yellow bar to disable javascript.

I tried in firefox and IE, both worked.

------------------
Other Solutions
------------------

There are several solutiions to this problem if your title is static for each section. I assume you wanted to use the variable because the title will be dynamic. In that case I'm not sure what you should do.

The only thing I could think of would be to re-do your include files. That way you could include them before your title.

Your include files would look something like this.

//Logic and all data gather - not text output//Variables and data goes here that can be passed into the function to outputfunction print_content();//This contains all your <td><tr> or w/e you're outputting

Then simply instead of doing your case inc etc etc you just call your function with standard variable names for arguments. An array to hold your data would be easy to walk through in a standard way. There's plenty of ways to do this. Let me know if you need any help with it or don't understand what I'm talking about. But to do what you need to do you have to declare the variable first I believe.

EDIT:
You replied before I finished adding my new findings =p Reread
Edited by minnieadkins (see edit history)

Share this post


Link to post
Share on other sites

I don't know how to do this either. I was thinking javascript with document.title to change it dynamically, but it doesn't change the title on the fly. Well it does, but not visually. I think the form has to resubmit in order for that to happen.

 

There are several solutiions to this problem if your title is static for each section. I assume you wanted to use the variable because the title will be dynamic. In that case I'm not sure what you should do.

 

The only thing I could think of would be to re-do your include files. That way you could include them before your title.

 

Your include files would look something like this.

 

//Logic and all data gather - not text output//Variables and data goes here that can be passed into the function to outputfunction print_content();//This contains all your <td><tr> or w/e you're outputting

Yeah that's pretty much what faulty.lees script does if I'm right.

But had some other thoughts about using MySQL instead of loading the var from the foo.php script.

It could also be done this way:

URL: "http://example.com/?page instead of id>"

 

And then do this: <title>example.com || <?php echo $_GET['page']; ?></title>

 

It gives the same result as i want to only in a different way :)

 

So thank you all for your suggestions!! And I consider this problem solved :D

 

Bakr_2k5

Edited by bakr_2k5 (see edit history)

Share this post


Link to post
Share on other sites

I don't know how to do this either. I was thinking javascript with document.title to change it dynamically, but it doesn't change the title on the fly. Well it does, but not visually, well it did and didn't. I think the form has to resubmit in order for that to happen for all browsers to do it correctly. You can attempt it however.
Place that into your content includes...well a modified version of that.

<?php $SomeVariable = 'This would be the title'; ?><!-- This goes inside your include file with your variable--><script type="text/javascript" language="JavaScript"><!--document.title='<?php echo $SomeVariable;?>';--></script>

For testing purposes here's what I used
<html><head><title>Some Title</title></head><body><div>Example</div><script type="text/javascript" language="JavaScript"><!--document.title='Other Title';--></script></body></html>

Note: This presents 2 problems.
1.) User does not have javascript enabled.
2.) In IE user gets that annoying yellow bar to disable javascript.

I tried in firefox and IE, both worked.

------------------
Other Solutions
------------------

There are several solutiions to this problem if your title is static for each section. I assume you wanted to use the variable because the title will be dynamic. In that case I'm not sure what you should do.

The only thing I could think of would be to re-do your include files. That way you could include them before your title.

Your include files would look something like this.

//Logic and all data gather - not text output//Variables and data goes here that can be passed into the function to outputfunction print_content();//This contains all your <td><tr> or w/e you're outputting

Then simply instead of doing your case inc etc etc you just call your function with standard variable names for arguments. An array to hold your data would be easy to walk through in a standard way. There's plenty of ways to do this. Let me know if you need any help with it or don't understand what I'm talking about. But to do what you need to do you have to declare the variable first I believe.

EDIT:
You replied before I finished adding my new findings =p Reread


I edited my post. Perhaps you could place in the javascript and make a working copy. Once I tested it inside the html it seemed to work. But I was modifying it from the url address wtih a javascript injection and it didn't work.

IE.

URL : java script:void(document.title='Changed title')

Did not work.

But the <script> tags inside the html did. You can attempt it, but as I said it has a few flaws.

Share this post


Link to post
Share on other sites

I edited my post. Perhaps you could place in the javascript and make a working copy. Once I tested it inside the html it seemed to work. But I was modifying it from the url address wtih a javascript injection and it didn't work.
IE.

URL : java script:void(document.title='Changed title')

Did not work.

But the <script> tags inside the html did. You can attempt it, but as I said it has a few flaws.


Hmm i was a bit too fast i guess :D

In Firefox java script:void(document.title='Changed title') works. And that yellow bar to disable javascript is because you
run the script locally if I'm right.

But the Javascript isn't really practical because users can have it disabled as you pointed out :D

I think i'll go for the <title>example.com || <? echo $_GET['page'] ?></title> it would also be a lot cleaner then building a whole script around it to get the title changed :)

Bakr_2k5

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.