Re: Using HTML_Table



bob.herbst@xxxxxxxxx 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();

// start from here
//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");

Ehh...

$header = array("Last name", "First name", "email", "Advisor",
"Graduation year", "Highest degree", "Attending");
$table->addRow($header, null, 'TH');

//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";

You are doing a lot of unecessary stuff here.

// You need this specific order
$query = 'SELECT lastname,firstname,email,advisor,year,degree,attend 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++;
}

Sorry, but should have a look at HTML_Table specs again... I would suggest something like this:

while ($row = $mysqli->mysqli_fetch_array($result, MYSQLI_NUM) {
$table->addRow($row);
}

Basically, one of the good things about HTML_Table is that you no longer need to think of tables as cells, instead you can think of it as rows, columns, cells, and all sorts of things inbetween. Setting one cell at a time defeats the concept. The only thing that you need to pay attention to is that you select the required fields in the correct order for use with "addRow".

//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

.



Relevant Pages

  • Re: Specify default on drop-down based on DRW?
    ... > HTML Web component, and nothing happened. ... I also placed the script after ... > entry in the database column as opposed to the current value. ... manufactures the drop-down list's HTML on the fly. ...
    (microsoft.public.frontpage.programming)
  • Re: Using HTML_Table
    ... Currently I am using the script below, ... database to display the table info in an HTML table. ... echo mysqli_connect_errno; ...
    (comp.lang.php)
  • Re: need client side form converts get to a post
    ... Given that much of data to be submitted, you should use a POST request and a ... <!DOCTYPE html> ...  The content model of HTML SCRIPT ... thanks for your post, the output is a get from a desktop database, i ...
    (comp.lang.javascript)
  • Using HTML_Table
    ... I have been trying to use HTML_Table from PEAR to write a PHP script ... database to display the table info in an HTML table. ...
    (comp.lang.php)
  • Re: PHP web design patterns
    ... the html pages being the view and the processing script ... and some guidelines from "Database Design for Mere Mortals," which I ... pointers on other suitable patterns, that will cover both web & database? ...
    (alt.php)