|
Posted by noone on 10/23/97 11:40
bob.herbst@gmail.com wrote:
> I have been trying to use HTML_Table from PEAR to write a PHP script
> that will access a database and retrieve my data into an HTML table
> that can be sorted by column. Currently I am using the script below,
> which does not include sorting (I want the basic table to work first)
> but all I get is the column headers and no data in the column can
> anyone tell me how to fix this problem and have the script access my
> database to display the table info in an HTML table.
>
> <html>
> <head>
> <title>Untitled Document</title>
> <meta http-equiv="Content-Type" content="text/html;
> charset=iso-8859-1">
> </head>
> <body>
> <?php
> //Include the HTML_Table package
> require_once "HTML/Table.php";
> $link = @new mysqli("host", "user", "password", "database");
> if (!$link) {
> echo mysqli_connect_errno();
> } else {
> $msg = "Connection was a success!!";
> }
> $table = new HTML_Table();
> //Set the Headers
> $table->setHeaderContents(0, 0, "Last Name");
> $table->setHeaderContents(0, 1, "First Name");
> $table->setHeaderContents(0, 2, "E-mail Address");
> $table->setHeaderContents(0, 3, "Advisor");
> $table->setHeaderContents(0, 4, "Graduation Year");
> $table->setHeaderContents(0, 5, "Highest Degree");
> $table->setHeaderContents(0, 6, "Attending");
> //Cycle through the array to produce the table data
> //Create and Execute the Query
> $query = "SELECT lastname as 'Last Name', firstname as 'First Name',
> email as 'E-Mail Address',
> advisor as 'Advisor', year as 'Graduation Year', degree as 'Highest
> Degree', attend as 'Attending' FROM rsvp
> ORDER BY lastname";
> $result = $mysqli->query($query);
> $rownum=1;
> while($obj = $result->fetch_object()){
> $table->setCellContents($rownum, 0, $obj->lastname);
> $table->setCellContents($rownum, 1, $obj->firstname);
> $table->setCellContents($rownum, 2, $obj->email);
> $table->setCellContents($rownum, 3, $obj->advisor);
> $table->setCellContents($rownum, 4, $obj->year);
> $table->setCellContents($rownum, 5, $obj->degree);
> $table->setCellContents($rownum, 6, $obj->attend);
> $rownum++;
> }
> //Alternate row styling
> $table->altRowAttributes(1, null, array("class"=>"alt"));
> //output the data
> echo $table->toHTML();
> //Close the connection
> $mysqli->close();
> ?>
> </body>
> </html>
>
> Thanks
> Bob
>
I use something like this...
//retrieve results from database
$result = $mysqli->query($query);
//Start building the table
$table = "<table border=1 >\n <TR>\n";
$result_array = array_keys($results);
//Lets display the column headers first
foreach($result_array as $myresult)
{
$data = $myresult;
$table .= " <TD>$data</TD>";
}
$table .=" </TR>";
echo $table . "\n";
//Now - populate the table data
for($i=0;$i<$rows;$i++)
{
$table = "<TR> \n";
foreach($results as $myresult)
{
$data = chop($myresult[$i]);
if (!isset($data)){$data = "<center>-</center>";}
$table .= " <TD>$data</TD>";
}
$table .=" </TR>";
// All Done - send results to the browser.
echo $table . "\n";
}
echo "</table>";
[Back to original message]
|