Making pages on the fly from a database
Moderator: General Moderators
Making pages on the fly from a database
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
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
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:
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
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>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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
oo im so close i cant beleive it, i have the url's coming up perfectly.
it is now like,
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
it is now like,
Code: Select all
<a href="www.mysite.com/tutorial.php?id=1">tutorial1</a>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
sorry feyd but im missing something.
ive tried alsorts, tried this, am i getting warm lol:
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;
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
$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.
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.
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;
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
So your looking to echo a result resource, eh?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; ?>
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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?