473,473 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using HTML_Table

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

Feb 20 '06 #1
3 2522
bo********@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>";
Feb 20 '06 #2
Now I am getting this following error:
Fatal error: Call to a member function query() on a non-object in
/opt/lampp/htdocs/phptrials/html_table_output.php on line 37

My line 37 is $result = $mysqli->query($query); which is part of this
next statement.
$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);

I am sorry for all this I am new to PHP and I am just having syntax
problems.

Thanks again

Feb 20 '06 #3
bo********@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();
// 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,atten d
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

Feb 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robbie | last post by:
Hi to everyone, I'm relatively new to php and to PEAR in particular. I'm using PEAR, especially the HTML_Table package. It's almost clear how the things work, but I've a question for which...
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
1
by: Julien Sobrier | last post by:
Hello, I can't install the pear package HTML_Table: # pear install HTML_Table Warning: xml_parse() http://www.php.net/function.xml-parse]: Unable to call handler _pkginfo_cdata_2_0() in...
7
by: jwhitby3 | last post by:
Hi all, I am trying to develop what amounts to a data entry page for the company I work for, (mostly to make my job easier). I think that I am beginning to grasp php, but I am at a loss now. I...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.