To create dynamic URL with php and mysql is quite simple and this small tutorial will teach you how very easily. If you look at Chipmunk directory, board, or CMScore, you
will find that they all use Dynamic URLs with PHP/Mysql. This following code snipplet will show you everything, for this code, you must assume that you have following mysql table:
Table name: thetable
Field I should be a field called ID with the properties bigint, length 21, primary and auto increment.
Field II should be a field called message with the properties longtext, length 6000
Field III should be a field called name with properties varchar and length of 256
I'm also assuming the filename you write this code on is called "index.php".
Now supposed you have some entries. Observe the following code:
< ?
if (isset($ID)) //this is looking at a specific ID
{
$S="SELECT* from thetable where Entry where ID='$ID'";
//$ID is the number after the ID= in your browser
$S2=mysql_query($S1);
$S3=mysql_fetch-array($S2);
print "$S3[longtext]";
//prints the text or entries of the field with the set ID
}
else if(!isset($ID)) // else looking at root
{
$d="SELECT * from thetable"; //select all the entries in the table
$d2=mysql_query($d);
while($d3=mysql_fetch_array($d2)) //this goes through all the entries that you selected
{
print "<A href='index.php?ID=$d3[id]'>$d3[name]</a> ";
}
}
?>
In the code above, the comment relates to the line or the code on the line or the line of code above it. Basically the first if case says that if an ID is set then
you select all the entries from the sql where the ID field is equal to the ID set in the browser, since ID is auto-incrmenting and each ID is unique(provided that you have less than 100 quadrillion entries in the table), that means
it selects the one entry your looking for, then it queries it and then puts it into an array form, since there is only one possible entry,
its okay to set the variable $S3 to the array of the query. the print "$S3[longtext]" simply prints the textfield of the entry associated with whose ID is set by the number following the
ID= in the browser.
Now lets look at the second case which is simply if there is not ID set in the browser. When there is no ID value set in the browser URL, it
simply select every single entry in the table and then the while look goes through all the entries possible. The print statement prints a hyperlink URL
index?ID=$d3[ID] every time the loop goes through, which in this case is from the first entry in the SQL table until it runs out of entries. This, combined with
the previous IF case effectively prints out links to every entry ID in the SQL table. When any of the links is clicked on ID is now set and the links still leads to index.php but it
now an ID number is set, and you see the message associated with that ID.
Thats all there is to dynamic URLS!! For a better understanding of how dynamic URL's work, I suggest you look at the code for index.php in my CMS core script, going through that script will give
you a true understanding of how dynamic URLs are generated.
It you have questions, Please post them in the Support Forums
|