Page 1 of 1
Making pages on the fly from a database
Posted: Mon Jan 29, 2007 2:19 pm
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
Posted: Mon Jan 29, 2007 2:26 pm
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"
Posted: Mon Jan 29, 2007 2:32 pm
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
Posted: Mon Jan 29, 2007 2:49 pm
by feyd
Look carefully at the last two lines of my previous post.
Posted: Mon Jan 29, 2007 2:50 pm
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.
Posted: Mon Jan 29, 2007 2:59 pm
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

Posted: Mon Jan 29, 2007 3:31 pm
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
Posted: Mon Jan 29, 2007 3:33 pm
by feyd
Last line of my first response.
Posted: Mon Jan 29, 2007 3:47 pm
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;
?>
Posted: Mon Jan 29, 2007 3:56 pm
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.
Posted: Mon Jan 29, 2007 5:26 pm
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;
?>
Posted: Mon Jan 29, 2007 6:21 pm
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?
Posted: Mon Jan 29, 2007 6:26 pm
by tail
That's what .Stealth requested.
Posted: Mon Jan 29, 2007 6:28 pm
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?