Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Oct 31, 2002, 11:38   #1
JdL
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.
JdL is offline   Reply With Quote
Old Oct 31, 2002, 11:53   #2
Wilco
SitePoint Zealot
 
Join Date: Jan 2002
Posts: 167
hhmm u want apache modrewrite

http://www.javascriptkit.com/howto/htaccess7.shtml
Wilco is offline   Reply With Quote
Old Oct 31, 2002, 12:00   #3
JdL
SitePoint Enthusiast
 
Join Date: Oct 2002
Posts: 75
Quote:
Originally posted by Wilco
hhmm u want apache modrewrite

http://www.javascriptkit.com/howto/htaccess7.shtml
I don't think so. I want it in PHP (server-side), not Java or HTML (client-side). I'm pretty new to PHP, though. Perhaps I misunderstand?
JdL is offline   Reply With Quote
Old Oct 31, 2002, 12:02   #4
Wilco
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
Wilco is offline   Reply With Quote
Old Oct 31, 2002, 12:05   #5
JdL
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?
JdL is offline   Reply With Quote
Old Oct 31, 2002, 12:17   #6
Mike
SitePoint Genius
 
Mike's Avatar
 
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.
Mike is offline   Reply With Quote
Old Oct 31, 2002, 12:28   #7
slider
jigga jigga what?
 
slider's Avatar
 
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.
slider is offline   Reply With Quote
Old Oct 31, 2002, 12:28   #8
JdL
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... (?)
JdL is offline   Reply With Quote
Old Oct 31, 2002, 12:30   #9
slider
jigga jigga what?
 
slider's Avatar
 
Join Date: Oct 2002
Location: Utah (USA)
Posts: 310
Quote:
Originally posted by JdL
Can't I write a whole PHP/HTML script and use it as a variable?
I really don't understand what you're talking about with that. What you can do is use an INCLUDE and I think that accomplishes the same thing you're talking about (though, like I said I'm not sure what you're talking about).
slider is offline   Reply With Quote
Old Oct 31, 2002, 12:36   #10
klassicd
Free me php
 
klassicd's Avatar
 
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.
klassicd is offline   Reply With Quote
Old Oct 31, 2002, 12:39   #11
JdL
SitePoint Enthusiast
 
Join Date: Oct 2002
Posts: 75
Quote:
Originally posted by slider
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.
Hmmm... too many files to keep track of. I just want two!

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!
JdL is offline   Reply With Quote
Old Oct 31, 2002, 12:50   #12
JdL
SitePoint Enthusiast
 
Join Date: Oct 2002
Posts: 75
Quote:
Originally posted by slider
I really don't understand what you're talking about with that. What you can do is use an INCLUDE and I think that accomplishes the same thing you're talking about (though, like I said I'm not sure what you're talking about).
Yeah, well I'm not sure what I'm talking about either. The thing is, I know what I want---but I have to say it in a different language, you know?

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].
JdL is offline   Reply With Quote
Old Oct 31, 2002, 12:55   #13
Mike
SitePoint Genius
 
Mike's Avatar
 
Join Date: Apr 2001
Location: Canada
Posts: 6,270
Quote:
Originally posted by JdL
"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... (?)
SitePoint is down right now, so no link

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
Mike is offline   Reply With Quote
Old Oct 31, 2002, 13:03   #14
JdL
SitePoint Enthusiast
 
Join Date: Oct 2002
Posts: 75
Quote:
Originally posted by naramation

SitePoint is down right now, so no link
Weird! SitePoint is working fine for me. Maybe you should empty your History? It could be that your Internet server's connection to the rest of the world is not working. Definitely weird.
JdL is offline   Reply With Quote
Old Oct 31, 2002, 13:08   #15
Mike
SitePoint Genius
 
Mike's Avatar
 
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
Mike is offline   Reply With Quote
Old Oct 31, 2002, 13:20   #16
JdL
SitePoint Enthusiast
 
Join Date: Oct 2002
Posts: 75
Quote:
Originally posted by naramation
Well, if it works for you, the article is called "Search Engine Friendly URLs". Do a search for it
I found it: http://www.promotionbase.com/article/485

It's not what I'm looking to do either. Please read my other posts and try to piece together my thoughts.
JdL is offline   Reply With Quote
Old Oct 31, 2002, 13:46   #17
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Quote:
Originally posted by JdL
It's not what I'm looking to do either. Please read my other posts and try to piece together my thoughts.
Search engine friendly URLs are what you want... or at least they are the closest thing to it that you'll find documented anywhere.

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.
randem is offline   Reply With Quote
Old Oct 31, 2002, 14:34   #18
JdL
SitePoint Enthusiast
 
Join Date: Oct 2002
Posts: 75
Quote:
Originally posted by randem


Search engine friendly URLs are what you want... or at least they are the closest thing to it that you'll find documented anywhere.

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.
Of course search engine friendly URLs are what I want...

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!
JdL is offline   Reply With Quote
Old Oct 31, 2002, 20:42   #19
slider
jigga jigga what?
 
slider's Avatar
 
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.
slider is offline   Reply With Quote
Old Nov 1, 2002, 06:30   #20
JdL
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:

Template.php

<?php
//graphics, layout, etc.
include ($article)
//more graphics, layout, etc.
?>
File2

PHP Code:

Page1A.php

<?php
$article
= "page1b.php"
include (template.php);
?>
File3

PHP Code:

Page1B.php

//graphics, layout, HTML content
//other PHP stuff, etc.
Thanks for the help, I guess.
JdL is offline   Reply With Quote
Old Nov 1, 2002, 06:36   #21
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Quote:
Originally posted by JdL
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.
Are you using Apache?
randem is offline   Reply With Quote
Old Nov 1, 2002, 06:59   #22
JdL
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.
JdL is offline   Reply With Quote
Old Nov 1, 2002, 07:11   #23
randem
morphine for a wooden leg
 
randem's Avatar
 
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.
randem is offline   Reply With Quote
Old Nov 1, 2002, 10:03   #24
slider
jigga jigga what?
 
slider's Avatar
 
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.
slider is offline   Reply With Quote
Old Nov 1, 2002, 10:26   #25
randem
morphine for a wooden leg
 
randem's Avatar
 
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/
randem is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 22:30.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved