Displaying dynamic PHP content from a page.

I have a website that I'm developing in PHP.  I have 45 or so pages of content, each a paragraph long.

How can I organize these individual paragraphs and store them in one central location, and then as each PHP page is rendered - how can I pull the content from a page based on some sort of anchor link?

I've seen sites with this type of technology being used and the URL looks similar to:

http://www.servername.com/info.php?article=45

Is there a script out there that accomplishes this?
LVL 1
jpipitoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harisha M GEngineerCommented:
Hi jpipitone,
    When you have to goto the new page, give the location as .... (in JS)

    "http://www.servername.com/info.php?article=" + yourid

    Then in PHP, you can recieve the id using $_GET["article"]


Bye
---
Harish
jpipitoneAuthor Commented:
I don't understand your English too well - can you describe to me in detail how I would organize the articles and the command used to grab the articles?

I'm assuming its something like <? php $_GET["1"] ?>

But how would I organize the content file?
Harisha M GEngineerCommented:
>> I'm assuming its something like <? php $_GET["1"] ?>
Yes

You can use a database to store the articles, and then get the corresponding one.
dorwardCommented:
It would be $_GET['article'] (since that was the name part of the query string parameter:   foo.php?name_of_parameter=value_of_parameter)

And you would have to do something with it to get the article.

For example:

$subject = "abcdef";
$pattern = '/^def/';

<?php
  if (isset($_GET['article']) && preg_match('/^\d+$/', $_GET['article']) {
     include('/path/to/include/files/article_' . $_GET['article'] . '.txt');
  }
?>

That should work, although my PHP is a little rusty. Note the test with the regular expression to ensure the article variable contains only digits - this prevents it being loaded up with "../" in order to access arbitary files on your file system.
Diablo84Commented:
hi jpipitone,

With that many pages i would take an approach like this...

Have your content files in a specific directory, for example /content
Your files might be named, 1.php, 2.php, 3.php etc

You script will look something like this:

<?php
$dir = $_SERVER['DOCUMENT_ROOT'].'/content/'; //directory where content files are located
$ext = '.php'; //filename extensions
$default = '1'; //default page to include (filename without extension)

if (isset($_GET['content'])) {
 $filename = $dir.basename($_GET['content'],$ext).$ext;
 if (preg_match('/^[\w\-\.%]+$/',$_GET['content']) && file_exists($filename)) $content = $filename;
}
if (!isset($content)) $content = $dir.$default.$ext;

include($content);
?>

There are several things added here for reasons of security... storing the files in a dedicated directory, the use of the basename function and the added validation means that only files that you want to have included can be included. Part of the validation checks for valid filenames... if you are only using numeric filenames (ie. 1.php, 2.php etc) then you could optionally
replace: preg_match('/^[\w\-\.%]+$/',$_GET['content'])
with: is_numeric($_GET['content'])

In order to load different content you would access the page using:

nameofscript.php?content=1
nameofscript.php?content=2
nameofscript.php?content=3 etc

The snippet of code would appear in your page where you want the content to be included, eg:

<html>
<head></head>
<body>

<div style="width:80%">
<!-- php code here for example -->
</div>

</body>
</html>

The alternative option is to store the content in separate rows in a database table and running a select query based on the value passed to the script via the url query string. (as opposed to storing the content in physical files).

NOTE: refereshed before posting to see other posts have been made, i am going to post this anyway since i have spent some time on it.

Diablo84

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.