note 31313 added to function.odbc-field-name
| From: | aleckzandr at yahoo dot com | Date: | Thu, 17 Apr 2003 05:04:38 +0000 |
| Subject: | note 31313 added to function.odbc-field-name | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46721@lists.php.net to get a copy of this message | ||
Well, I've been into PHP for four hours and thanks to "my predecessors before me"
(gold163, curt, et al.) I've managed the following. The first thing I try to learn with any web
scripting language is to build a dynamic table from a data source. (One thing you didn't have
to do gold -previous post- is build an array for the field value.) Cheers! Alex
<html>
<head>
<title>PHP Database Example</title>
</head>
<style type="text/css">
<!--
body {font: 10pt/12pt Tahoma, Verdana, Helvetica, sans-serif; color: indigo; margin: .25in .5in }
table {color:Navy; background-color:AntiqueWhite; border-color:Maroon; border-style:Solid;
border-width: 2px; }
th {color: blue; font-weight: bold; }
td {font-size: smaller; }
.mytable {color:Maroon; background-color:White; border-color:Navy; border-style:Solid;
border-width: 1px; }
th.mytable {background-color:#C0C0C0; }
//-->
</style>
<body>
<p><?php echo date("j F, Y"); ?></p>
<?php
$db = odbc_connect("eSell22MDB","","");
$result = odbc_exec($db, "select ProductID, ProductName, Description1 from Products");
// cool function - returns table
odbc_result_all($result, "border=\"1\" class=\"def\"");
$result = odbc_exec($db, "select * from Products") or die("Select failed");
$myUtil = new Utilities();
$myUtil->standard_table($result,"mytable");
class Utilities {
function standard_table($result,$class="")
{
// To format your table if you want to use cascading style sheets
if ($class == "")
{
$css_table = " border=\"1\"";
$css_tr = "";
$css_th = "";
$css_td = "";
}
else
{
$css_table = " class=\"$class\"";
$css_tr = " class=\"$class\"";
$css_th = " class=\"$class\"";
$css_td = " class=\"$class\"";
}
// Create field names for table header row
$i = 0;
$fieldCount = odbc_num_fields($result);
echo " <table$css_table>\n";
echo " <tr$css_tr>\n";
while ($i < $fieldCount)
{
$i++;
$fieldName = odbc_field_name($result, $i);
echo " <th$css_th>$fieldName</th>\n";
}
echo " </tr>\n";
# Create table data rows for query result
while (odbc_fetch_row($result))
{
$i = 0;
echo " <tr$css_tr>\n";
while ($i < $fieldCount)
{
$i++;
$fieldData = trim(odbc_result($result, $i));
if ($fieldData == "")
echo " <td$css_td> </td>\n";
else
echo " <td$css_td>$fieldData</td>\n";
}
echo " </tr>\n";
}
echo " </table>";
}
} // class Utilities
?>
</body>
</html>
--
http://www.php.net/manual/en/function.odbc-field-name.php
http://master.php.net/manage/user-notes.php?action=edit+31313
http://master.php.net/manage/user-notes.php?action=delete+31313
http://master.php.net/manage/user-notes.php?action=reject+31313