Change <td> color on condition - php

Hi i have this PHP which take sql query divide it into two columns and print table. Now i need to change color of TD where value > 0. I've add color change by class. It's working but not correct. It change color of whole string but not TD cell.
$stmt=ociparse($olink, $sql);
if (!$stmt) {
$e = oci_error($olink);
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$r =ociexecute($stmt,OCI_NO_AUTO_COMMIT);
if (!$r) {
$e = oci_error($stmt); // For oci_execute errors pass the statement handle
print htmlentities($e['message']);
print "\n<pre>\n";
print htmlentities($e['sqltext']);
printf("\n%".($e['offset']+1)."s", "^");
print "\n</pre>\n";
}
$ncols = oci_num_fields($stmt);
$cur_dt = 1;
echo "<TABLE border=\"1\" width=\"100%\" align='center' cellspacing='0' cellpadding='0' class=\"sortable\">";
/* echo "\n<tr>"; */
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
$str=1;
while (oci_fetch($stmt)) {
if ($str % 2 == 1)
{
echo "\n";
echo "<tr";
$hrr="";
echo ">";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
echo "</TABLE>";
oci_close($olink);
?>
</div>
</body>
</html>
0 in second column should be blue, but it ged "hour" class and change color

create a class in your css file like .change{background-color:green} in your
<td>
for ($i = 1; $i <= $ncols; $i++) {
$hrr ="";
if (oci_result($stmt, 2) >= $cur_dt) {$hrr = 'class="change"';}
echo "<td ".$hrr.">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}

Use css to style a single cell style="background-color: #2B75C1;"
If you want to change the color of the string style="color: #2B75C1;"
echo '<th style="background-color: #2B75C1;" class=\"header\">'.oci_field_name($stmt, $i).'</th>';

Replace
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
For
echo '<td' . (oci_result($stmt, 2) >= $cur_dt ? ' class="hour"' : '') . '>';
// ^ shouldn't be there '$i'?
echo oci_result($stmt, $i);
echo '</td>'; // closing tag for <td> in optional, you can remove that to make your HTML more readable.
// remove $hrr variable, you don't need it.

I think, you have to replace the "2" with $i . Replace the following code
if (oci_result($stmt, 2) >= $cur_dt)
With the solution.
Solution 1 :
if (oci_result($stmt, $i ) >= $cur_dt)
Solution 2 :
if ( (oci_result($stmt, $i ) >= $cur_dt) || oci_result($stmt, $i ) > 0)

Related

Table of numbers in a pattern using PHP

A couple of days ago, I made a post similar to this one.
I continued my work, but went back to this exercise after.
I need 2 more patterns in a table. The last 2 tables, I did with nested For-loops, which worked excellent, but now I'm stuck again.
What I need:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
What I tried:
<table>
<b>Patroon III</b>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 6; $col >= $row; $col--){
if($col <= $row){
echo "<td class='td2'>" . $col . "</td>";
}
else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>
What I get:
1
2
3
4
5
6
And the second table I need:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
What I tried:
<table>
<b>Patroon IV</b>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 1; $col <= $row; $col++){
if($col >= $row){
echo "<td class='td2'>" . $col . "</td>";
}
else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>
What I get:
1
2
3
4
5
6
Any suggestions? I'm thinking about another nested for-loop, but I don't know where to start. Not sure if the if-else statement is needed.
This is the very basic thing in php. You just need to think about the logic. That's it.
Here is your solution to your question
<b>Patroon III</b>
<table border=1>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 6; $col >= 1; $col--){
if($col <= $row){
echo "<td class='td2'>" . $col . "</td>";
}
else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>
<b>Patroon IV</b>
<table border=1>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 1; $col <= $rows; $col++){
if($col >= $row){
echo "<td class='td2'>" . $col . "</td>";
} else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>

PHP array with for loop

i have array of 10 item and each item has own price.
i have to select some item and display the selected item's average and if any of selected item's above the average i have to display it's pic in 2x2 table different pic in each cell.
i write this code but the problem is the pic repeat in the 4 cell's:
<?php
echo "<table border=2>";
$incVar = 0;
for ($x = 0; $x < 2; $x++) {
echo"<tr>";
for ($y = 0; $y < 2; $y++) {
echo"<td>";
while($incVar <=9){
if (isset($camera[$incVar]) && $camera[$incVar] >= $avreag){
echo '<img src="' . $pic[$incVar] . '" width="200" height="200">';
}
$incVar++;
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
first you have to create image array because if you will display in this manner and if some items does not match the condition then it will leave blank <td>
solution :
<?php
echo "<table border=2>";
$incVar = 0;
for ($x = 0; $x < 2; $x++) {
echo"<tr>";
for ($y = 0; $y < 2; $y++) {
echo"<td>";
if (isset($camera[$incVar]) && $camera[$incVar] >= $avreag) {
echo '<img src="' . $pic[$incVar] . '" width="200" height="200">';
}
$incVar++;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
no you need to display $imagesArr in 4x4 for loop

Change code with 2 for loop in PHP

I tried many hours to simplify this code:
<?
echo '<div class="eme3-left">'."\n".'<table>'."\n";
for ($n=1,$i=10; $n<=100,$i<=100;$n+=10,$i+=10)
{
echo '<tr>
<td class="dick-grau">'.$n.' → '.$i.'</td>
<td>'.$n.' = '.decbin($n).'<sub>2</sub></td>
<td>'.($n+1).' = '.decbin($n+1).'<sub>2</sub></td>
<td>'.($n+2).' = '.decbin($n+2).'<sub>2</sub></td>
<td>'.($n+3).' = '.decbin($n+3).'<sub>2</sub></td>
<td>'.($n+4).' = '.decbin($n+4).'<sub>2</sub></td>
<td>'.($n+5).' = '.decbin($n+5).'<sub>2</sub></td>
<td>'.($n+6).' = '.decbin($n+6).'<sub>2</sub></td>
<td>'.($n+7).' = '.decbin($n+7).'<sub>2</sub></td>
<td>'.($n+8).' = '.decbin($n+8).'<sub>2</sub></td>
<td>'.($n+9).' = '.decbin($n+9).'<sub>2</sub></td>';
};
echo '</tr></table>'."\n".'</div>';
?>
How to add the if condition from the third code for the second for loop ?
<?php
echo '<div class="eme3-left">'."\n".'<table>'."\n";
for ($n=1,$i=10; $n<=100,$i<=100; $n+=10, $i+=10) {
echo '<TR>'."\n".'<td class="dick-grau">'.$n.' → '.$i.'</td>';
for ($t = 1; $t<=10; $t++) {
echo '<td>'.$t.' = '.decbin($t).'<sub>2</sub></td>'."\n";
}
echo "</tr>";
}
echo '</table>'."\n".'</div>';
?>
In another version the following loop works perfectly, but when I add it to the code above, the if-condition don't work.
for($i=1;$i<=100;$i++){
echo '<td>'.$i.' = '.decbin($i).'<sub>2</sub></td>'."\n";
if($i%10 == 0)
echo '</tr>'."\n";
}
Maybe their are more suggestions for improvements.
This is not a complete example but should get you there.
<?php
$ints = range(1,100);
$ints_grouped_by_10 = array_chunk($ints,10);
foreach($ints_grouped_by_10 as $int_group){
echo "<tr>";
echo '<td class="dick-grau">'.reset($int_group).' → '.end($int_group).'</td>';
foreach($int_group as $int){
echo "<td>{$int} = ".decbin($int)."</td>";
}
echo "</tr>";
}
?>
[Edit] added the legend (1 -> 10, 11 -> 20)
echo '<div class="eme3-left">'."\n".'<table>'."\n";
for ( $n=1 ; $n <= 100 ; $n+=10 )
{
echo '<tr>
<td class="dick-grau">'.$n.' → '.($n+9).'</td>';
for ( $j = 0 ; $j < 10 ; $j++ )
echo '<td>'.($n + $j).' = '.decbin($n+$j).'<sub>2</sub></td>';
echo '<tr>';
}
echo '</table>'."\n".'</div>';

Make a new table row every 2 echoed

I'm trying to make a new table row every time a user is echoed.
Here is what I've tried to zero success.
<?
$i = 0;
while($row = mysql_fetch_array($admin, MYSQL_ASSOC)){
if($i <= 2){
$i++;
echo "<tr><td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' />";
}
else{
echo "<td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' /></td></tr>"
$i=0;
}
}
?>
Any ideas peeps?
This should work:
<?
$i = 0;
echo "<table><tr>";
while($row = mysql_fetch_array($admin, MYSQL_ASSOC)) {
if($i++ % 2 == 0 && $i > 1) {
echo "</tr><tr>";
}
echo "<td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' /></td>"
}
echo "</tr></table>";

breaking long echoed array content at interval

please i need you help with my script.. I'm trying to echo some content of query into an html table, but i want to break it at every 7th parameter. The script below only breaks the first seven, while the rest are not broken up at interval and they were also echoed out of the html table.
How can I do this please. Thank you for your time and help.
echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;
echo "<tr align= \"center\">\n";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<td>" . "Failed: ";
if ($score_count !== 0) {
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>" . $row8['course_code'] . "</th>";
if ($count == 7) {
echo "</tr>\n";
echo "</table>";
}
}
}
Use modulo operator instead of equality
if ( ($count+1) % 7 ){
The +1 is there so that it doesn't break immediately on $count == 0 because 0%n is 0
You need to use the modulus operator %, which returns the remainder after division. $count % 7 ==0 means the current count is on a multiple of 7, and you should break. You also need to increment $count.
echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;
echo "<tr align= \"center\">\n";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<td>"."Failed: ";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>".$row8['course_code']."</th>";
// Increment $coutn
$count++;
// Check the modulus
if ( $count % 7 == 0 ){
echo "</tr>\n";
echo "</table>";
}
}
}

Categories