/    Sign up×
Community /Pin to ProfileBookmark

Generating HTML Pages With Database Content

How exactly do you generate a new HTML page with database content when your database is updated?

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — I'm sorry for the double post, but I REALLY need help with this.

For instance, on a message board, you can access a thread with a direct URL. I don't understand how when you pull the thread and its post out of the database you have a URL to a new web page that displays the output of the database (the thread).

Please help.
Copy linkTweet thisAlerts:
@NogDogJul 04.2008 — The page is dynamically generated each time it is requested by querying the DB for the necessary data. For example, the content you see displayed on this page right now does not exist anywhere as a file. All that exists is the "showthread.php" file. Each time that file is called, based on the various input paramters it receives in the URL, it queries the database and generates the output "on the fly".
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — Do you mind giving me an example in PHP code?
Copy linkTweet thisAlerts:
@NogDogJul 04.2008 — Here's a simple example. It looks for a "page" value in the url query string, e.g. "http://example.com/showpage.php?page=3". It's obviously very bare-bones with minimal error-handling. (And I'm sure there's at least one typo in there somewhere.)
[code=php]
<html><head><title>Test</title></head><body>
<h1>This title always prints</h1>
<?php
if(!empty($_GET['page']))
{
mysql_connect('localhost', 'user', 'password') or die(mysql_error());
mysql_select_db('db_name') or die(mysql_error());
$sql = "SELECT * FROM table_name WHERE page = " . (int)$_GET['page'];
$result = mysql_query($sql) or die(mysql_error());
if(($row = mysql_fetch_assoc($result)) !== false)
{
echo "<h2>" . htmlentities($row['title']) . "</h2>n";
echo "<p>" nl2br(htmlentities($row['content'])) . "</p>n";
}
else
{
echo "<p class='error'>Invalid page specified.</p>n";
}
}
else
{
?>
<p>This only gets displayed if no page was specified in URL.</p>
<?php
}
?>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — So, instead of page, I would put a URL?
Copy linkTweet thisAlerts:
@skywalker2208Jul 04.2008 — The $_GET['page'] is the variable being passed by the url. See how after the ? the word is page http://example.com/showpage.php?[B]page=3[/B]

So $_GET['page'] is 3 from the example.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — But how does $_GET['page'] know to get the third page?
Copy linkTweet thisAlerts:
@NogDogJul 04.2008 — Because that's how the $_GET super-global array works: each [b]name=value[/b] pair in the URL query string is assigned to an element of that array. So the URL "http://example.com/sample.php?user=123&page=5&style=2" would populate the $_GET array as if you had programmed it as:
[code=php]
$_GET = array(
'user' => 123,
'page' => 5,
'style' => 2
);
[/code]

It's the same as if you had submitted a HTML form using [b]method="get"[/b]. (When you submit a form using [b]method="post[/b] then the same thing happens, except the values are stored in $_POST instead of $_GET.)

See [url=http://www.php.net/manual/en/language.variables.external.php]Variables From External Sources[/url] in the manual for more info.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — What exactly do you mean by "URL query?"
Copy linkTweet thisAlerts:
@NogDogJul 04.2008 — What exactly do you mean by "URL query?"[/QUOTE]
The "URL query string" is the (optional) part of a URI which follows a "?" and precedes any "#". So in this URI...
<i>
</i>http://www.example.com/directory/page.php?[color=red]var1=one&amp;var2=two[/color]#part2
...the part in red is the URL query string being used to pass "get" data to the page. "var1" would be the first variable name with a value of "one", and "var2" would be the second name with a value of "two". The ampersand ("&") is used to separate each name=value pair.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — Let's go back to the example you posted. I don't really understand how the code works. To me, it looks a little too vague, like it's not being specific enough as to what to get. Could you explain that?
Copy linkTweet thisAlerts:
@skywalker2208Jul 04.2008 — [code=php]
<html><head><title>Test</title></head><body>
<h1>This title always prints</h1>
<?php
if(!empty($_GET['page'])) <--grabs page number from url
{
mysql_connect('localhost', 'user', 'password') or die(mysql_error());
mysql_select_db('db_name') or die(mysql_error());
$sql = "SELECT * FROM table_name WHERE page = " . (int)$_GET['page']; <--queries database by page number
$result = mysql_query($sql) or die(mysql_error());
if(($row = mysql_fetch_assoc($result)) !== false) <--assoc array return of contents
{
echo "<h2>" . htmlentities($row['title']) . "</h2>n"; <-- prints title from database
echo "<p>" nl2br(htmlentities($row['content'])) . "</p>n"; <--prints contents from database.
}
else
{
echo "<p class='error'>Invalid page specified.</p>n"; <-- bad page number.
}
}
else
{
?>
<p>This only gets displayed if no page was specified in URL.</p>
<?php
}
?>
</body>
</html>
[/code]

Hope that helps and that it comes out the way I want it to.
Copy linkTweet thisAlerts:
@NogDogJul 04.2008 — Note that my sample code was assuming a hypothetical content database named "db_name" with a table named "table_name" with (at least) 3 fields: "page" (an integer and the primary key), "title", and "description".

If it's still all Greek to you (no offense intended to any Greek members, it's just an expression ? ), it's time to buy a book, perhaps, so that you can follow the process step by step.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — I've got two PHP books. A basic one that I've finished, and an intermediate one that I'm about half-way through with.

So is page like the primary key in the database table?
Copy linkTweet thisAlerts:
@skywalker2208Jul 04.2008 — I've got two PHP books. A basic one that I've finished, and an intermediate one that I'm about half-way through with.

So is page like the primary key in the database table?[/QUOTE]

Most likely, but it doesn't have to be. If it is the primary key it will make for a more efficient query.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — So if it's the primary key, would that mean that it's an integer and most likely just used for the ID of the information?
Copy linkTweet thisAlerts:
@skywalker2208Jul 04.2008 — So if it's the primary key, would that mean that it's an integer and most likely just used for the ID of the information?[/QUOTE]
pretty much
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 04.2008 — Now how would you go about making the list of links to the posts?
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorJul 23.2008 — I apologize for bringing back an old post, but I need some more info for this, and I figured that it made more sense to post here instead of starting a new thread. If this was the wrong decision, please correct me?

Okay, after some more study on this subject, I'm beginning to understand more as to how it works. How exactly would you list the URLs with their correct page numbers? I see that $_GET receives the page number from the URL, but how would you post the correct URLs without having to manually edit your HTML page every time in order to put the correct URLs in?
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...