Page 1 of 1

dynamic html tables using php

Posted: Sat Mar 11, 2006 5:53 pm
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]

Posted: Sat Mar 11, 2006 6:08 pm
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

Posted: Sat Mar 11, 2006 6:15 pm
by feyd
the table cells have a few boogers in them.. not to mention it'll print nothing in the cells.

Posted: Sat Mar 11, 2006 7:55 pm
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...

Posted: Sun Mar 12, 2006 7:13 am
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

Posted: Sun Mar 12, 2006 7:35 am
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. ;)