Building a dynamic site - Part 3

With any dynamic site, you’ll need a page that links to all your dynamic pages. The main index. If you visit the many pages on CLiX Network, there are actually many versions of these index pages that all are sub categories of the main front page.
<?
$global_dbh = mysql_connect("localhost","username","password");
mysql_select_db("mydatabasename", $global_dbh);
$query = "SELECT * FROM `pages`";
$result = mysql_query($query, $global_dbh);
$cnt = mysql_num_rows($result);
for ($i=0; $i<$cnt; $i++) {
$row = mysql_fetch_array($result);
echo "<A HREF=\"page.php?id={$row["ID"]}\">"
."{$row["Title"]}</A><BR>"
.substr($row["Body"],0,255)."<P>";
}
?>
By now you should understand what the first half of the script does, so I’ll just start below that.
$cnt = mysql_num_rows($result);
The reason I’ve defined $cnt here, instead of using the mysql_num_rows function in the for statement is for speed. Instead of running the function over and over, it runs it once.
page.php?id={$row["ID"]}
Remember that we pass the page ID to the script we made in Part 2. Because we are using the $row array inside quotes, we surround it with brackets { } so PHP knows to insert the value of $row["ID"] and not just $row.
".substr($row["Body"],0,255)."
Putting just links to the different pages, and nothing more is kind of boring. This code gives us the first 255 characters of the page underneith the link. Kind of like a preview of what it is about. You can increase it or decrease it as you like - or even take it out to just do a page full of links.
So there you have it. A simple index page for your new dynamic site.






