Forum Moderators: coopster

Message Too Old, No Replies

getting php variables before the 'include' takes place

         

Aleister

12:37 am on Jan 29, 2005 (gmt 0)

10+ Year Member



On one of my sites, I have a page that generates links based on the contents of a subdirectory. It creates these links like this:


<a href="data/template.php?id=file1">file1</a>
<a href="data/template.php?id=file2">file2</a>
etc..

Where 'file1' and 'file2' are the first files in the subdirectory it generated the link for (file1.php and file2.php). These are just examples, and I am also using mod_rewrite, but I am showing this for simplicity.

As you can see, it simply loads template.php, and includes the text found in file1.php. The template.php file also uses the name of the variable passed to it, to generate the page title, and the page headers.

But often I need to have punctuation in the page title and headers, and of course I cannot use question marks etc. in the file name.

I know I could do something like this in the file1.php:

<?php $page_title = "whatever";?>

But of course these variables can't be used by template.php because they would of been needed before the content file is included.

I hope I am explaining this well :) So you may see what I am getting at.. And I am also not using databases for the moment, so this is not an option either.

I am just trying to figure out the best way to pass variables from the file1.php into template.php before the location in the file where it is being included, while still keeping this type of method. This method allows me to simply add a new file into the directory, and the links will automatically exist, etc.

Any thoughts? Thanks!

mincklerstraat

10:31 am on Jan 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can include files before any output has begun, including the stuff in <head>. Then you can put variables from file(s) included into your <title> tags.

Typically you get stuck with included files when they combine both data and code execution - that is information, but they also do something. If they're files that do something, like output stuff (yours is doing that by all the non-php stuff), they of course need to be included at exactly the right place where that stuff needs to be done or outputted. This can be a pain, like in your case where you need the info first.

The thing to do then is to convert all the stuff your include file does into information of some kind. You can do this by either setting variables - which have your page contents, which you just output later - or if you need to do fancy stuff, you can set functions, and later call those functions. Thing is, though, you can't set a function with the same name twice. So when you're including files with functions, use include_once, just in case some sloppy piece of code later tries to include the same file again.

Happy coding.

Aleister

3:00 pm on Jan 29, 2005 (gmt 0)

10+ Year Member



Ah thanks! That should do the trick!

You mentioned using both a variable for content, and a function to display the content.

If I make the included content one big variable, it seems like it would be too big. I am not sure if there would be any problems with that. I will have to look up the limitations, etc.

The functions idea sounds very nice. I will probably try that. Since the only function will need to be the actual 'content' of the included file, I see no reason why I would need to call it up more than once in the same instance of the template file, so that may be best.

Thanks again! I will let you know how it works for me.

I really tried to search around for this info, but it is one of those things that can be tricky to search for it seems :) Maybe it's just me!

mincklerstraat

5:15 pm on Jan 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hey, who ever said variables have gotta be small ;)

You should see some of the variables I sometimes end up producing. There really should be a law against it.

Aleister

6:20 pm on Jan 29, 2005 (gmt 0)

10+ Year Member



The only possible issue I am thinking about is that the content in the included files has some html formatting. Probably nothing more than paragraphs and breaks, but I don't remember if I will have to convert all those so they will be ok in a big string, or even just a straight echo or something :)

We will see!

mincklerstraat

1:17 am on Jan 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem at all. Variables can contain all the HTML formatting you want. You only want to watch out for variables which have their origin in user input - these should have the HTML checked or stripped first, but then you can cut 'n paste stuff together to make one honking huge variable that's many times larger than any self-respecting webpage if you really feel you have to.

dosdawgs

6:48 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



Guys, i have spent two plus hours searching for the mod_rewrite for dynamic url to static url. every time i find a good one to post to, as luck would have it, its expired, i forget to read the date before i spend my time trolling through all the posts. So fellas, my question is this.....and i have been to apache, sitepoint, and tons of other sites, just joined this one this morning becuase of all the others i frequented this one seems to be loaded with a tick more intellegence. So could somebody please help me. i have tried just about every example i could find, and i am just not making progress. just say for the file in the dir /directory/index.php and i want it to go to index.html of course there is not the infamous? that would clutter the poor ol bot there, so one of my other urls would be like this /directory/search_detail.php needs to be /directory/search_detail.html

i have like i said tried several instances, and i know mod_write is working, thats not the issue, i actually had the server looking for index.html, and that confused me got the 404 not found, so whats the next step? sorry for the long post, but getting kinda desparate.

thank you guys
dosdawgs