dynamic html tables using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
new_developer
Forum Newbie
Posts: 4
Joined: Sat Mar 11, 2006 7:01 am

dynamic html tables using php

Post by new_developer »

hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,

I am using php 4, an oracle 10g database, and a linux server.

I am trying to construct a loop to create a table. I am retrieving data from an oracle database. It has 3 fields (question, answer, and comments). I now want to display these three fields on a page. I assume that I can use a for loop to create the fields, but I am unclear on the syntax. Can I use html tags inside a php script or do I enclose the for statement in the <? ?> and then add html tags?

Can someone help me with the code below?

Code: Select all

$sql = "select a.candidatequestion_id,
               a.question,
               a.answer,
               a.comments
          from table_name  a
         where a.candidate_id = 33034
         order by a.displayorder";  

if (!$dbcon->select_data($sql)) $dbcon->manage_error();
$result = $dbcon->get_result_set();

<table border="1" cellpadding="3">

<tr>
  <? for ($z=0;$z<sizeof($result);$z++){ ?>
         <td align="left"> <? $result[$z]->QUESTION&nbsp; ?> </td>
         <td align="left"> <? $result[$z]->ANSWER&nbsp; ?> </td>
         <td align="left"> <? $result[$z]->COMMENTS&nbsp; ?> </td>

  <? } ?>
</tr>
Does this look correct?

TIA,
new_developer


hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

change

Code: Select all

if (!$dbcon->select_data($sql)) $dbcon->manage_error();
$result = $dbcon->get_result_set();

<table border="1" cellpadding="3">

<tr>
  <? for ($z=0;$z<sizeof($result);$z++){ ?>
         <td align="left"> <? $result[$z]->QUESTION&nbsp; ?> </td>
         <td align="left"> <? $result[$z]->ANSWER&nbsp; ?> </td>
         <td align="left"> <? $result[$z]->COMMENTS&nbsp; ?> </td>

  <? } ?>
to

Code: Select all

if (!$dbcon->select_data($sql))
    $dbcon->manage_error();

?>
<table border="1" cellpadding="3">

<tr>
  <? while ($result = $dbcon->get_result_set()){ ?>
         <td align="left"> <? $result[$z]->QUESTION&nbsp; ?> </td>
         <td align="left"> <? $result[$z]->ANSWER&nbsp; ?> </td>
         <td align="left"> <? $result[$z]->COMMENTS&nbsp; ?> </td>

  <? } ?>
and you should be good
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the table cells have a few boogers in them.. not to mention it'll print nothing in the cells.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

It just so happens that I was at Zend doing some reading the function below when I stumbled on to this thread. The function takes a dataset and builds a dynamic table.

Code: Select all

function get_result_set_html ($resultHandle, $actions)
{
    if (mysql_numrows($resultHandle) > 0) {
        $fieldCount = mysql_num_fields($resultHandle);
        $resultHTML .= "
            <table border='0' cellpadding='5' cellspacing='0'>
            <tr bgcolor='#efefef'>
        ";

        for ($i = 0; $i < $fieldCount; $i++) {
            $rowName = mysql_field_name($resultHandle, $i);
            $resultHTML .= "<th>$rowName</th>";
        }

        $resultHTML .= "<th>Actions</th></tr>";

        while ($row = mysql_fetch_array($resultHandle)) {
            $resultHTML .= "<tr>";

            for ($i = 0; $i < $fieldCount; $i++) {
                $resultHTML .= "<td valign='top'>".htmlentities($row[$i]). "</td>";
            }

            eval("\$currentAction = \"$actions\";");
            $resultHTML .= "<td valign='top' nowrap>$currentAction</td></tr>";
        } /* end while */

        $resultHTML .= "</table>";
    } else { /* no data was found matching the query */
        $resultHTML = 0;
    } /* end if */

    return $resultHTML;
} /* end get_result_set_html */
Hope that will get you started on finding your solution...
new_developer
Forum Newbie
Posts: 4
Joined: Sat Mar 11, 2006 7:01 am

Post by new_developer »

feyd wrote:the table cells have a few boogers in them.. not to mention it'll print nothing in the cells.
Now what is the point of posting this message if you are not going to explain why? :x
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

new_developer wrote:
feyd wrote:the table cells have a few boogers in them.. not to mention it'll print nothing in the cells.
Now what is the point of posting this message if you are not going to explain why? :x
In hopes that you can figure out why =] Most people like for you to try to learn instead of telling you the problems. But I think it's because you need an echo or print before your $result[$z]-> inside of the table cells. ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply