PHP Template Tutes?

Status
Not open for further replies.

Mike

New member
Jun 27, 2006
6,777
116
0
51
On the firing line
Anybody know of any? I've got a vague idea of how to do what I want, but would like to see some example code to make sure I'm on the right track.

Basically, I want to pull results from a DB and list them out, with the titles being linked to their own page with the complete description, etc. So, I need a script that shows how to pull the data (I pretty much have that figured out), us pagination (got an example in a book), and dynamically create pages for each db entry (no clue).

Does that make sense?

Thanks!
 


It sounds like you're looking to create a sitemap of sorts for a certain table(s) in your database. Assuming you're just looking for how to display the stuff on a seperate page, you'd want to do something to the effect of having a seperate php file, called template.php. This page would take a parameter called title, which gives template.php?title={title} where {title} is the title of your db entry.

Now, in template.php put a line to grab the title, $title = $_GET['title']. Then, you can lookup the rest of the data in your table using $title as your key.
 
Code:
<?php require_once('Connections/connection1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_connection1, $connection1);
$query_Recordset1 = "SELECT domains.domain_name FROM domains";
$Recordset1 = mysql_query($query_Recordset1, $connection1) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

Connection string above. Requires connection file with db info. Also does select from database.

Code:
<?php do { ?>
  <a href="pagename.php?="<?php echo $row_Recordset1['domain_name']; ?>">a<?php echo $row_Recordset1['domain_name']; ?></a><BR />
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

Creates the list with links to the same name. You can change the field to be a primary key from the table (much faster database access that way).


Code:
<?php
mysql_free_result($Recordset1);
?>

Be a good boy and close your recordset out to free resources.
 
Here's the basic idea in readable english:

1. User visits "home" page.
2. PHP script verifies that user is viewing home page and grabs all entries from the database.
3. PHP script displays each entry's title as a link to the full entry with part of the URI would have an id or some other identifying value of the entry.
4. A user clicks on the entry title link.
5. The link takes the user to another PHP script which reads the identifying information about the entry from the URI (Query String).
6. PHP script fetches the entry from the database and displays it on the page.
5.
 
Here's the basic idea in readable english:

1. User visits "home" page.
2. PHP script verifies that user is viewing home page and grabs all entries from the database.
3. PHP script displays each entry's title as a link to the full entry with part of the URI would have an id or some other identifying value of the entry.
4. A user clicks on the entry title link.
5. The link takes the user to another PHP script which reads the identifying information about the entry from the URI (Query String).
6. PHP script fetches the entry from the database and displays it on the page.
5.

Exactly! That's what I was thinking of!

Thanks for the help everyone, I'm off to play with my Zend IDE. :D
 
I use:

PHP:
<?php 
$page = $_GET['page'];
if (eregi("http", $page) || eregi("ftp", $page) || eregi("../", $page)) { 
   # Someone tries to include external file - this is big no-no 
   $page = "picture"; //File to include instead
} 
if(empty($page))
{
$page = "picture"; //Same as above
}
include($page.".php"); 
?>
 
Status
Not open for further replies.