I have two problems...
Here is code :

<?php
// Create connection to Oracle
$conn = oci_connect("root", "admin", "//127.0.0.1/xe");
$query = 'select * from TEST_RESULTS';
$stid = oci_parse($conn, $query);
$r = oci_execute($stid);

print '</br>';
// Fetch each row in an associative array
print '<b> MONITORING SRODOWISK TESTOWYCH </br>';
print '</br>';

print '<table border="1">';
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
   print '<tr>';
   foreach ($row as $item) {print '<td>'.($item !== null ? htmlentities($item, ENT_QUOTES) : '&nbsp').'</td>';}
   print '</tr>';
}
print '</table>';

?>

on my local website , I have table :

id result1 result2 result3 resul4

  1. ok ok ok pass
  2. nok ok ok fail
  3. ok nok ok fail
    ...
    1) firstly , I need add column headings, because now I do not have
    2) I would like change my background( bgcolor="#FF0000" ) color in every row when result4 = fail
    Please any suggestions how to best do this
    thx !

Dani AI

Generated

Short answer: build the table headers from the first returned row, output rows with a CSS class when the relevant column equals "fail" (case-insensitive), and escape all cell values. Avoid deprecated attributes like bgcolor and font tags; use a small CSS rule such as .fail { background:#ffdddd; }.

Example (uses associative fetch so headers come from column names and the entire row can be marked when a column equals "fail"):

<?php
$stid = oci_parse($conn, 'SELECT * FROM TEST_RESULTS');
oci_execute($stid);

// get first row to capture headers
$first = oci_fetch_assoc($stid);
if ($first === false) { echo '<p>No rows</p>'; exit; }
$cols = array_keys($first);

echo '<style>
table{border-collapse:collapse}
th,td{padding:6px;border:1px solid #ccc}
tr.fail{background:#ffdddd}
</style>';

echo '<table><thead><tr>';
foreach ($cols as $c) echo '<th>'.htmlspecialchars($c).'</th>';
echo '</tr></thead><tbody>';

// print first row + remaining rows; detect "fail" in RESULT4 (adjust name as needed)
do {
  $status = isset($first['RESULT4']) ? trim($first['RESULT4']) : '';
  $class = (strcasecmp($status, 'fail') === 0) ? 'fail' : '';
  echo '<tr'.($class ? " class=\"$class\"" : '').'>';
  foreach ($cols as $c) echo '<td>'.htmlspecialchars($first[$c]).'</td>';
  echo '</tr>';
} while ($first = oci_fetch_assoc($stid));

echo '</tbody></table>';
?>

Notes: Oracle often returns unquoted column names in uppercase (check the keys with array_keys). If the target column name differs, replace 'RESULT4' with the correct column name (or detect by column position). Use trim() and strcasecmp() to avoid mismatches from spacing or case. As suggested, generate a header row; s existing oci_fetch_all approach can keep the same class-application logic when iterating the stored arrays.

Recommended Answers

All 4 Replies

<table broder="1">
          <tr>
            <td>Result 1</td>
            <td>Result 2</td>
            <td>Result 3</td>
            <td>Result 4</td>
          </tr>

Then step through your while loop.

For changing colour:

if $result4 =='fail'{
    do this
}

I improve code, but I still Have problem with change color in row when is 'fail' value.. how do this

<?php
$conn = oci_connect("root", "admin", "//127.0.0.1/xe");
$query = 'select * from TEST_RESULTS';
$stid = oci_parse($conn, $query);


$stmt=oci_parse($conn,'select * from TESTS');


$ret=oci_execute($stmt);
$nrows=oci_fetch_all($stmt,$results);


if ($nrows>0)
{
echo "<table border='1' align='center' bordercolor='gray' cellspacing='0' cellpadding='5'> \n";
echo "<tr>\n";

foreach ($results as $key => $val)
{ echo "<th bgcolor='#FF9900' height='20' align='center'> <font color='white' face='verdana' size='2'> $key </font> </th> \n";
}
echo "</tr>\n";


for ($i=0; $i<$nrows; $i++) {
echo "<tr>\n";

foreach ($results as $data){

if($data[$i] == null)
{
   echo "<td bgcolor='white'  align='center'> <FONT COLOR='black' FACE='verdana' align='center' SIZE='3'>   </FONT> </td>\n";
}
else
{
echo "<td bgcolor='white' > <FONT COLOR='black' FACE='verdana' SIZE='2'> $data[$i] </font>  </td>  \n";
}

}
echo "</tr>\n";
}
echo "<table> \n"; 

}else 

{
echo '<font color="red"> error - missing id<br/> </font>';
}
oci_free_statement($stmt);
oci_close($conn);

?>

thanks for your help
I managed to improve

It would help to know the area you want to change to red.

As i had stated you would use an if statement nested into your existing loops. You will need to validate the data:

for example if $result4 == 'data'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.