Making pages on the fly from a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Making pages on the fly from a database

Post by .Stealth »

hi guys, i have just made a script to add a few things to a database, it goes like this:

id (autoincrement)
title
content

its for a tutorial system, you can add tutorials from a form


now i have done it, well as far as adding the data via a form, but im kinda stuck, i want it like this:

a page called tutorials, on this page there will be links, taken from the title, these links will go to a page that php made once the user clicked the link, and it will display all of the data from the content field.

how can i do this, i was thinking of using url variables but since i have no experience with them im stuck lol, im doing this to learn, but at the same time get something i need from it.


thanks for any help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You want PHP to generate static pages? fopen() + fwrite() + fclose()

You want to have PHP generate the pages to the browser? $_GET is how URL variables are communicated to your script.

foo.php?id=asdf

$_GET['id'] will hold "asdf"
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

sorry i didnt get one bit of that lol.
erm i want it to create the pages when a link is clicked, i dont know much about the url variables so forgive me if this is a stupid way but i was thinking:

i would somehow tell php to echo:

Code: Select all

<a href="www.url.com/tutorial.php?<? echo $id; ?></a>
then each id would be echoed on the end of the url, then php does some magic and on that page the corresponding content field would be echoed out between aload of html that would make up the style of that page.


thing is, ive not got a clue how to do it or even if its the easiest way to do it.

thanks for any help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look carefully at the last two lines of my previous post.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

feyd answered with what you just posted...

Basically your app is told what page to load based on either a page id or a page name through the query string. Each page in the app checks for the presence of the id/name of the page and, if it is present, grabs the data for the page from the database. If the id/name is not present, you default to index. It is pretty simple logic, and just as simple to code.
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

ohh yeah, sorry i didnt look closley enough, i just guessed you didnt know exactly what i meant when you said static pages, thanks for that, i will give it a go :D
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

oo im so close i cant beleive it, i have the url's coming up perfectly.

it is now like,

Code: Select all

<a href="www.mysite.com/tutorial.php?id=1">tutorial1</a>
sorry for not understanding correctly but how do i say to php:
get the id that is in the url and display the corresponding content field.

even just a hint would be fine, ive looked around the php.net site but can see much that helps.

thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Last line of my first response.
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

sorry feyd but im missing something.

ive tried alsorts, tried this, am i getting warm lol:

Code: Select all

				 <<?php
				 mysql_connect($host,$user,$password) or die('could not connect to database');
				 mysql_select_db($database);
				 $id=$_GET['id'];
				 $query="SELECT $id FROM tutorials";
				 $tutorial=mysql_query($query);
				 echo $tutorial;
				 ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$id will refer to the value alone, a selection often requires field names to select and the WHERE clause to specify which records you want.

Also, $id could be anything at this point. I'd suggest you use intval() or casting to force the value to an integer to avoid injections.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Code: Select all

<?php
mysql_connect($host,$user,$password) or die('could not connect to database');
mysql_select_db($database);
$id=$_GET['id'];
$query="SELECT FROM tutorials WHERE id='$id'";
$tutorial=mysql_query($query);
echo $tutorial;
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

tail wrote:

Code: Select all

<?php
mysql_connect($host,$user,$password) or die('could not connect to database');
mysql_select_db($database);
$id=$_GET['id'];
$query="SELECT FROM tutorials WHERE id='$id'";
$tutorial=mysql_query($query);
echo $tutorial;
?>
So your looking to echo a result resource, eh?
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

That's what .Stealth requested.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think he'd be more interested in the data that is fetched from running the result resource through a fetch function (like mysql_fetch_array()), don't you think?
Post Reply