Using HTML_Table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bob.herbst@gmail.com

    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>Untitl ed 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->setHeaderConte nts(0, 0, "Last Name");
    $table->setHeaderConte nts(0, 1, "First Name");
    $table->setHeaderConte nts(0, 2, "E-mail Address");
    $table->setHeaderConte nts(0, 3, "Advisor");
    $table->setHeaderConte nts(0, 4, "Graduation Year");
    $table->setHeaderConte nts(0, 5, "Highest Degree");
    $table->setHeaderConte nts(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->setCellContent s($rownum, 0, $obj->lastname);
    $table->setCellContent s($rownum, 1, $obj->firstname);
    $table->setCellContent s($rownum, 2, $obj->email);
    $table->setCellContent s($rownum, 3, $obj->advisor);
    $table->setCellContent s($rownum, 4, $obj->year);
    $table->setCellContent s($rownum, 5, $obj->degree);
    $table->setCellContent s($rownum, 6, $obj->attend);
    $rownum++;
    }
    //Alternate row styling
    $table->altRowAttribut es(1, null, array("class"=> "alt"));
    //output the data
    echo $table->toHTML();
    //Close the connection
    $mysqli->close();
    ?>
    </body>
    </html>

    Thanks
    Bob

  • noone

    #2
    Re: Using HTML_Table

    bob.herbst@gmai l.com wrote:[color=blue]
    > 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>Untitl ed 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->setHeaderConte nts(0, 0, "Last Name");
    > $table->setHeaderConte nts(0, 1, "First Name");
    > $table->setHeaderConte nts(0, 2, "E-mail Address");
    > $table->setHeaderConte nts(0, 3, "Advisor");
    > $table->setHeaderConte nts(0, 4, "Graduation Year");
    > $table->setHeaderConte nts(0, 5, "Highest Degree");
    > $table->setHeaderConte nts(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->setCellContent s($rownum, 0, $obj->lastname);
    > $table->setCellContent s($rownum, 1, $obj->firstname);
    > $table->setCellContent s($rownum, 2, $obj->email);
    > $table->setCellContent s($rownum, 3, $obj->advisor);
    > $table->setCellContent s($rownum, 4, $obj->year);
    > $table->setCellContent s($rownum, 5, $obj->degree);
    > $table->setCellContent s($rownum, 6, $obj->attend);
    > $rownum++;
    > }
    > //Alternate row styling
    > $table->altRowAttribut es(1, null, array("class"=> "alt"));
    > //output the data
    > echo $table->toHTML();
    > //Close the connection
    > $mysqli->close();
    > ?>
    > </body>
    > </html>
    >
    > Thanks
    > Bob
    >[/color]


    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($res ults);

    //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<$ro ws;$i++)
    {
    $table = "<TR> \n";
    foreach($result s 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>";

    Comment

    • bob.herbst@gmail.com

      #3
      Re: Using HTML_Table

      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_outp ut.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

      Comment

      • Marcin Dobrucki

        #4
        Re: Using HTML_Table

        bob.herbst@gmai l.com wrote:[color=blue]
        > 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>Untitl ed 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();[/color]

        // start from here[color=blue]
        > //Set the Headers
        > $table->setHeaderConte nts(0, 0, "Last Name");
        > $table->setHeaderConte nts(0, 1, "First Name");
        > $table->setHeaderConte nts(0, 2, "E-mail Address");
        > $table->setHeaderConte nts(0, 3, "Advisor");
        > $table->setHeaderConte nts(0, 4, "Graduation Year");
        > $table->setHeaderConte nts(0, 5, "Highest Degree");
        > $table->setHeaderConte nts(0, 6, "Attending" );[/color]

        Ehh...

        $header = array("Last name", "First name", "email", "Advisor",
        "Graduation year", "Highest degree", "Attending" );
        $table->addRow($header , null, 'TH');
        [color=blue]
        > //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";[/color]

        You are doing a lot of unecessary stuff here.

        // You need this specific order
        $query = 'SELECT lastname,firstn ame,email,advis or,year,degree, attend
        from rsvp ORDER BY lastname";
        [color=blue]
        > $result = $mysqli->query($query );
        > $rownum=1;
        > while($obj = $result->fetch_object() ){
        > $table->setCellContent s($rownum, 0, $obj->lastname);
        > $table->setCellContent s($rownum, 1, $obj->firstname);
        > $table->setCellContent s($rownum, 2, $obj->email);
        > $table->setCellContent s($rownum, 3, $obj->advisor);
        > $table->setCellContent s($rownum, 4, $obj->year);
        > $table->setCellContent s($rownum, 5, $obj->degree);
        > $table->setCellContent s($rownum, 6, $obj->attend);
        > $rownum++;
        > }[/color]

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

        while ($row = $mysqli->mysqli_fetch_a rray($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".
        [color=blue]
        > //Alternate row styling
        > $table->altRowAttribut es(1, null, array("class"=> "alt"));
        > //output the data
        > echo $table->toHTML();
        > //Close the connection
        > $mysqli->close();
        > ?>
        > </body>
        > </html>
        >
        > Thanks
        > Bob
        >[/color]

        Comment

        Working...