|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Can I make a variable as a whole bunch of code?
I can't stand using URLs like:
"www.exampleURL.com/template.php?article=page1.php" Instead, I'd like to use "www.exampleURL.com/page1.php" and still use the template. How can I do this without creating extra files? Is it possible to simply make the content of "page1.php" a variable, and then include the "template.php" all in one file ("page1.php")? EDIT: This method requires only 2 files (what I want), but you have a lengthy URL---I don't want that. "www.exampleURL.com/template.php?article=page1.php" This method requires 3 files: (File name: Page1_link.php) <?php $article = "page1.php"; include "template.php"; ?> The method I'd like to figure out how to do: (File name: Page1.php) <?php $article = "All the content/code/text of page1.php"; include "template.php"; ?> How do I set this up? Thanks! Last edited by JdL; Oct 31, 2002 at 11:52. |
|
|
|
|
|
#2 |
|
SitePoint Zealot
![]() ![]() Join Date: Jan 2002
Posts: 167
|
|
|
|
|
|
|
#3 | |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Quote:
|
|
|
|
|
|
|
#4 |
|
SitePoint Zealot
![]() ![]() Join Date: Jan 2002
Posts: 167
|
u cant do it in PHP, as that is only ran after apache has got the right page. u need to use .htaccess files
![]() |
|
|
|
|
|
#5 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
No, no. I still want to use the "template.php" file.
Can't I write a whole PHP/HTML script and use it as a variable? |
|
|
|
|
|
#6 |
|
SitePoint Genius
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2001
Location: Canada
Posts: 6,270
|
an easy way would be to use it as:
www.url.com/template/page1 instead of what you have. Theres a good article on SitePoint (which is down right now) about doing this. |
|
|
|
|
|
#7 |
|
jigga jigga what?
![]() ![]() ![]() Join Date: Oct 2002
Location: Utah (USA)
Posts: 310
|
http://www.sitepointforums.com/showt...threadid=82139
That's a thread I started on a topic along these same lines. Check it out and see if anything in there helps you. I was thinking of making my site have only one front-end file: index.php, and then depending on what the user wanted to do simply include the appropriate back-end files. I had entertained thoughts of using a session variable to do this, but then realized you can't set a session variable with a hyper-link. What you can do is have a real page1.php that does just that, then simply redirects (using Header()) back to your template.php. After thinking about the whole concept more, I've decided I don't want to do my site based on that model. At least not wholly. |
|
|
|
|
|
#8 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
"naramation," What article? I need a link.
![]() I'm not sure this is what I want either, though. That implies just making extra folders too... (?) |
|
|
|
|
|
#9 | |
|
jigga jigga what?
![]() ![]() ![]() Join Date: Oct 2002
Location: Utah (USA)
Posts: 310
|
Quote:
|
|
|
|
|
|
|
#10 |
|
Free me php
![]() ![]() ![]() ![]() Join Date: Sep 2001
Location: San Diego
Posts: 537
|
Simple.
Use mysql. Call the page as article.php?articleid=45. Then in article.php simply select the article from the database. |
|
|
|
|
|
#11 | |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Quote:
1. The template file (template.php) --- which has "include ($article)" in the necessary location 2. The "article" file (page1.php) --- which has a the variable "$article," and defines $article as all the PHP/HTML code. It's got to be possible, but how do I do it? Functions? Objects? Hey, I'm new to this PHP thing! ![]() |
|
|
|
|
|
|
#12 | |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Quote:
![]() Basically, I want folks to just type in "www.123456.com/page1.php," where "page1.php" INCLUDEs "template.php," "template.php" INCLUDEs "$article," and the variable $article is defined in "page1.php." Sounds weird, right? ![]() MySQL is out. I don't have it, and I don't think I will need it [yet]. |
|
|
|
|
|
|
#13 | |
|
SitePoint Genius
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2001
Location: Canada
Posts: 6,270
|
Quote:
![]() And no, you don't need to make folders, you can explode the URL to treat the /'s as variables. Its ahrd for me to explain, I didnt write it ![]() |
|
|
|
|
|
|
#14 | |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Quote:
|
|
|
|
|
|
|
#15 |
|
SitePoint Genius
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2001
Location: Canada
Posts: 6,270
|
Well, if it works for you, the article is called "Search Engine Friendly URLs". Do a search for it
![]() |
|
|
|
|
|
#16 | |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Quote:
It's not what I'm looking to do either. Please read my other posts and try to piece together my thoughts. |
|
|
|
|
|
|
#17 | |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
Quote:
If you absolutely must have the variable page name be the first thing after the domain name, there's only one way I can think of to cheat it... which would be to put the PHP (or equivalent) code into a custom 404 error handler file, which then does the include for you. But the repercussions of this plan of action are uncertain. |
|
|
|
|
|
|
#18 | |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Quote:
Okay everybody. (this is frustrating) What I want to do is:1. Type in "www.123456.com/page1.php" in the address bar. 2. The file "page1.php" INCLUDEs the file "template.php" 2B. The file "page1.php" contains the variable "$article" and defines it 3. The file "template.php" INCLUDEs the variable "$article" 4. The variable "$article" is defined in "page1.php" Okay, items 2B and 4 are where I enter unknown territory. It's easy to make a variable equal a single word, phrase, number, etc. (example: $article = "This"). I want a variable ($article) to equal an entire page of PHP and HTML code. But I don't know how! I don't want to mess with any other files, there are only two involved here: template.php and page1.php. Help! ![]() |
|
|
|
|
|
|
#19 |
|
jigga jigga what?
![]() ![]() ![]() Join Date: Oct 2002
Location: Utah (USA)
Posts: 310
|
Maybe just try laying out in simple terms what you're wanting this page/site to do. Don't tell us what you want to do in PHP, just tell us what you want to do in English.
![]() What I'm deciphering is that you want to have this variable $article contain a whole... article, HTML markup and all. Your file template.php has your fancy formatting, graphics, whatever, and then right in the middle you have something like <body> <?php echo $article;?> </body> so that template.php does its thing with borders, graphics, whatever... but prints content from $article based on whatever you set it as in page1.php. Did I get that right? If I did get that right then sure, just go ahead and define $article to be whatever you want. If you do $article = 'lots of content here <i>including</i> HTML markup'; with the single quotes ('') instead of double quotes ("") then it'll not try to process any special characters you put in like HTML code. There are other special characters you may have to "escape" so it doesn't evaluate them however. You'll have to look into that a bit yourself. Also, I don't know off the top of my head what kind of a limit you have on the size a variable can be, so I don't know if this is practical to include a whole article or whatever. It sounds a bit hokey to me. I would suggest you extend (forget) your limit of only having two files, and simply assign $article to be a filename. In page1.php you do this $article = '/content/article_10-28-02.txt'; Then in template.php you do this <body> <?php include($article); ?> </body> article_10-28-02.txt contains whatever article text (HTML markup included) you want to display. You can edit that article by editing a simple text file. And you can change what article is displayed on your front page simply by editing the path you assign to $article. You'll preserve old articles this way, and as you learn more PHP this will make the transition to being able to let the user choose which article they want to see much, much easier. You'll just have to add some easy code to page1.php. Did I get anything close to what you're looking for? Last edited by slider; Oct 31, 2002 at 20:44. |
|
|
|
|
|
#20 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
Hey Slider, you hit what I wanted to do right in the stomach. (ouch) In other words, you're on the right track.
I guess it'll be too complicated to do this without a database (I don't want to have to change any special characters, I want to be able to create and edit the files in normal HTML editors like Dreamweaver or 1st Page 2000) Anyway, I guess I'll have to live with the three-file technique for now: File1 PHP Code:
PHP Code:
PHP Code:
![]() |
|
|
|
|
|
#21 | |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
Quote:
|
|
|
|
|
|
|
#22 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2002
Posts: 75
|
I'm not sure whether or not Apache is used. My service is a shared hosting plan, so I don't own any servers, and probably don't have access to some of the specifics.
I do know that it can run PHP code, though. |
|
|
|
|
|
#23 |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
Okay. I had a suggestion, but it would require you to have Apache, and a bit of administrative privilege over the configuration.
|
|
|
|
|
|
#24 |
|
jigga jigga what?
![]() ![]() ![]() Join Date: Oct 2002
Location: Utah (USA)
Posts: 310
|
Well, one of the points that I was making with the above is that the three file technique gives you a lot of freedom. Your third file should contain nothing but the actual article text (and any HTML markup you want to have in it, like links to other pages, font formatting, etc.). Your template.php file will be the one that contains any navigation links, page graphics, all core HTML tags (everything down to <body>). This is really similar to a database-based design, except that you're accessing a physical file to retrieve your article content rather than a database field. If you retain the same abstraction/separation you will experience the same freedom.
If you want to edit the article you simply edit the article include file. If you want to edit your site layout you simply edit the template file. If you want to add to your site's functionality you can add code to page1.php (or template.php, depending on what you want to add). If you want to add a new article in place of the old one, you upload a new file and change the filename assignment in page1.php. But the important point to make is all core HTML tags, site graphics, etc. are in the template file, not the article file. |
|
|
|
|
|
#25 |
|
morphine for a wooden leg
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
|
The idealogical seperation you're trying to accomplish is exactly the reason XML and XHTML have generated so much buzz for so long.
The content goes into an XML file, your "template" would be an XSLT file, and the server-side code would stay where it is, completely seperated from the other two. Furthermore, for that matter, all layout should be handled by CSS, and client side code should be maintained in external JS files. You've got the right idea, but a)you're not breaking any new ground, and b)you should do a little reading. Start at http://www.xml.com/ |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 22:30.








(this is frustrating) What I want to do is:




Linear Mode
