Thread: Data Table !?
View Single Post
Old Apr 4th, 2006, 7:57 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Here is some example code, beginning simply, but with PHP, style, and HTML stuff. You'll have to arrange for your own DB and connection, of course. You can see this work here.
[php]
<?php
/*
The database manipulation area. You'll have to arrange your own DB
and connection functions
*/
require_once ("connect.php");
demoConnect ();
$advisory = "Advisory goes here";
if ($_POST ['Name'])
{
/*
Path of execution if information has been posted. More complex
session control may be required under some circumstances.
Note that there is nothing to detect, restrict, or prevent
duplicate information. That's as it should be. In Hogwallow,
there might be four brothers with the same name. The PersonID
field ensures that each is unique. When I want to guard against
this, I make the totality of information more complex (even an
address and email address are not enough) and make an extraction
code which I put in the DB.
*/
$insert = "INSERT INTO AddressBook
SET PersonID=NULL,
Name='".ucwords (strtolower ($_POST ['Name']))."',
City='".ucwords (strtolower ($_POST ['City']))."'";
$iResult = mysql_query ($insert) or die (mysql_error () . " insertion failure<br/>");
$PersonID = mysql_insert_id ();
$advisory = "You inserted {$_POST ['Name']} with ID $PersonID";
}
// Query the DB
$query = "SELECT * FROM AddressBook ORDER BY PersonID";
$addresses = mysql_query ($query) or die (mysql_error () . " query failure<br/>");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Address Book</title>
<!-- Control things with CSS. Obvioulsy, widely used styles on a multi-page
site should be aggregated in a style sheet and pulled in -->
<style type="text/css">
body
{
margin: 5em;
}
td
{
border: 1px solid #000000;
}
th
{
border: 1px solid #000000;
width: 15em;
font-weight: bold;
}
.shorty
{
width: 5em;
}
.entry
{
font-weight: bold;
}
.pid
{
text-align: center;
font-weight: bold;
}
.theForm
{
padding: 10px;
border: 3px outset #000000;
width: 60%;
margin: auto;
}
.advisory
{
padding: 20px;
width: 50%;
margin: auto;
text-align: center;
}
</style>
</head>

<body>
<!-- Simple HTML. Presentation and content are mixed here. Ya gotta
start somewhere -->
<table align="center" cellspacing="0" cellpadding="0" class="book">
<tr>
<th class="shorty">ID</th><th>Name</th><th>City</th>
</tr>
<?php
while ($address = mysql_fetch_array ($addresses))
{
echo '<tr>';
echo '<td class="pid">'.$address ["PersonID"].'</td>';
echo '<td class="entry">'.$address ["Name"].'</td>';
echo '<td class="entry">'.$address ["City"].'</td>';
echo '</tr>';
}
?>
</table>
<p align="center"><strong>Add Entries Here</strong></p>
<div align="center" class="theForm" id="theForm">
<form name="addEntry" id="addEntry" method="post"
action="http://www.daweidesigns.com/AddressBook.php">
Name: <input type="text" size="20" name="Name" id="Name"/>
City: <input type="text" size="20" name="City" id="City"/>
<input type="submit" value="Add" name="Submit" />
</form>
</div>
<div class="advisory" id="advisory">
<?php echo $advisory;?>
</div>
</body>
</html>
[/php]
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote