CSS, PHP, mySQL Loop - change td colour depending on variable - php

Having an issue with assigning a td style class, depending on a variable from mySQL. I have a field called 'Avail' so when a seat is available, the seat should turn green. However, nothing is happening so far when I run it.
Have created the simple classes on the same html sheet but it doesn't seem to apply to any of the data.
CSS Class Definitions >>
<style>
.available {
background-color: green;
}
.unavailable {
background-color: red;
}
</style>
Code to show when style will be applied >>
<table>
<tr>
<th>Seats</th>
<tr>
<tr>
<?php
$c = 0; // Our counter
foreach($res as $row) {
$Avail = 'Avail';
$seatColour = null;
if($Avail == 0)
{
$seatColour = '.available';
} else
{
$seatColour = '.unavailable';
}
if($c % 20 == 0 && $c != 0) // If $c is divisible by $n...
{
// New row
echo '</tr><tr>';
}
$c++;
?>
<td style="$seatColour">
<form method="POST" name="Seating" action="SWG.php">
<?php echo $row['Seat']; ?> <?php echo $row['Avail']; ?>
<input type="checkbox" name="Seat[]" value="<?php echo $row['Seat'];?>"/>
</td>
<?php
}
?>
</tr>
</table>
Would really appreciate it if someone could point out where I'm wrong / how I'm assigning it incorrectly.

There seem to be several issues with your code:
$Avail = 'Avail';
$seatColour = null;
if($Avail == 0)
In the above code, you assign $Avail a string, and then you check whether it's zero. I'm assuming this is not deliberate. Maybe you want to assign the database value here (instead of a string)?
$seatColour = '.available';
You probably want to do $seatColour = "available". We're not in jQuery so no need for the dot.
td style="$seatColour"
You probably want to do <td class="<?php echo $seatColour ?>"> over here

Use class attribute instead of style in td and remove . from $seatColour as follows:
<table>
<tr>
<th>Seats</th>
<tr>
<tr>
<?php
$c = 0; // Our counter
foreach($res as $row) {
$Avail = 'Avail';
$seatColour = null;
if($Avail == 0)
{
$seatColour = 'available';
} else {
$seatColour = 'unavailable';
}
if($c % 20 == 0 && $c != 0) // If $c is divisible by $n...
{
// New row
echo '</tr><tr>';
}
$c++;
?>
<td class="$seatColour"><form method="POST" name="Seating" action="SWG.php">
<?php echo $row['Seat']; ?> <?php echo $row['Avail']; ?>
<input type="checkbox" name="Seat[]" value="<?php echo $row['Seat'];?>"/>
</td>
<?php } ?>
</tr>
</table>

instead of null try -1 .
or you put name/value for th.

Related

Having issue in my for loop using PHP, I want to create dynamic rows and column in PHP, Each row has 10 column

I am Having an issue in my for loop using PHP, I want to create dynamic row and column, Each row has 10 column after 10 column, the second row also end with 10 column like this up to 5 row, how to do It for a loop.
My for loop code:
<table width="100%" border="1">
<?php
for($i=1; $i<=72; $i++)
{
?>
<tr>
<td width="100%">
<?php echo "Click Here to see Site No.'".$i."'. & Area sqft No" .$i;?></a></td>
</tr>
<?php
}
?>
</tr>
</table>
I tried like this also
<table width="100%">
<tr>
<?php
for($i=1; $i<=72; $i++)
{
$x = 10;
if ($i % $x == 0)
{
?>
<td><?php echo $i;?></td>
<?php
}
}
?>
</tr>
</table>
<?php
echo '<table width="100%" border="1">';
for($i=1; $i<=8; $i++)
{
$y=10;
$y*=($i-1);
echo '<tr>';
for ($x=1; $x <=10; $x++) {
if ($i==1) {
echo '<td>'.$x.'</td>';
}else{
$y+=$x;
echo '<td>'.$y.'</td>';
if ($y==72) {
break;
}
$y-=$x;
}
}
echo '</tr>';
}
echo '</table>';
This will print the bellow table:
As per what I understand you want simple 10 column in each row.
Here is my code which may help you:
<table style="border:1px solid #000">
<tr>
<?php $t=1; for($k=1;$k<=72;$k++){?>
<?php if($t == 10) { $t=0;?><td style="border:1px solid #000"> <?php echo $k; ?> </tr><?php } else {?><td style="border:1px solid #000"> <?php echo $k; ?></td><?php } ?>
<?php $t++;}?>
<?php $x = 10; if ($i % $x == 0) { ?>
....
<?php } ?>
If I am getting the question correct, why don't you do as below:
echo '<table>';
for($i=1; $i<=5; $i++) {
echo '<tr>';
for ($y=1; $y<=10; $y++) {
echo '<td>Row_'.$i.' - Col_'.$y.'</td>';
}
echo '</tr>';
}
echo '</table>';
It will print something like below:

How to freeze column 1 and scroll through other columns and rows in html with php and sql fields

I have the following php generated table (drawing from sql database values) in which I want the first column (all the usernames) to be frozen (fixed) while the user can scroll horizontally and 'right' across the page to view the scores for each user.
I cannot figure out how to do this with the mix of php and html going on. Can anyone suggest a viable solution please?
An image for illustration purposes is below
(I wish for the username column (all rows) to be fixed) while scrolling through to the right - there will be many more quizzes)
PHP and HTML Code
<tbody>
<?php foreach(#$result as $record){?>
<tr>
<td><?php echo $record["username"];?></td>
<?php
$counter=0;
while(mysqli_num_rows($all_quizes) > $counter){
$current_td = mysqli_query($con,"SELECT * FROM quiz_takers WHERE username='".$record["username"]."' AND quiz_id=".$quizes[$counter][0]." ORDER BY marks DESC");
$td = mysqli_fetch_array($current_td);
if($td['percentage'] == null){
echo "<td>-</td>";
}else{
if(intval($td["percentage"]) >= 0 && intval($td["percentage"]) <= 30){
$color = 'red';
}elseif(intval($td["percentage"]) > 30 && intval($td["percentage"]) <= 70){
$color = '#ffbf00';
}elseif(intval($td["percentage"]) <= 30){
}else{
$color = 'green';
}
echo "<td style='color:".$color."'>".round($td["percentage"],2)."%</td>";
}
$counter++;
}
/*foreach($current_td as $td){
echo $counter." ".$td['username'] . " - ".$quizes[$counter]['3']."<br>";
if($quizes[$counter]['0'] == $td['quiz_id']){
?>
<td><?php echo $td["percentage"];?></td>
<?php } $counter++;}*/ ?>
</tr>
<?php } ?>
</tbody>
UPDATE:
What I have tried, thanks to the helpful suggestion below regarding CSS is the following: It works, but not quite (messed up formatting)
CSS
<style type="text/css">
.headcol {
position: absolute;
width: 5em;
left: 0;
top: auto;
border-top-width: 1px;
/*only relevant for first row*/
margin-top: -1px;
/*compensate for top border*/
}
.headcol:before {
content: 'Row ';
}
</style>
Added this line below:
<tbody>
<?php foreach(#$result as $record){?>
<tr>
<td><th class="headcol"><?php echo $record["username"];?></th></td>
<?php
$counter=0;
while(mysqli_num_rows($all_quizes) > $counter){
$current_td = mysqli_query($con,"SELECT * FROM quiz_takers WHERE username='".$record["username"]."' AND quiz_id=".$quizes[$counter][0]." ORDER BY marks DESC");
$td = mysqli_fetch_array($current_td);
if($td['percentage'] == null){
echo "<td>-</td>";
}else{
if(intval($td["percentage"]) >= 0 && intval($td["percentage"]) <= 30){
$color = 'red';
}elseif(intval($td["percentage"]) > 30 && intval($td["percentage"]) <= 70){
$color = '#ffbf00';
}elseif(intval($td["percentage"]) <= 30){
}else{
$color = 'green';
}
echo "<td style='color:".$color."'>".round($td["percentage"],2)."%</td>";
}
$counter++;
}
/*foreach($current_td as $td){
echo $counter." ".$td['username'] . " - ".$quizes[$counter]['3']."<br>";
if($quizes[$counter]['0'] == $td['quiz_id']){
?>
<td><?php echo $td["percentage"];?></td>
<?php } $counter++;}*/ ?>
</tr>
<?php } ?>
</tbody>
HTML Result is skewed: comes up with this
instead of the desired:

Keep Page Data on Refresh With $_POST

<!DOCTYPE html>
<html lang="en">
<head>
<h1>Table Generator</h1>
</head>
<body>
<center>Refresh</center>
<?php
$rows = (isset($_POST['rows']) ? $_POST['rows'] : null);
$cols = (isset($_POST['cols']) ? $_POST['cols'] : null);
$highlight = (isset($_POST['highlight']) ? $_POST['highlight'] : null);
if ($rows == "")
{
$rows = 10;
}
if ($cols == "")
{
$cols = 10;
}
if ($highlight == "")
{
$highlight = 5;
}
?>
<form method="post">
ROWS <input type="text" name="rows" value = "<?php echo $rows;?>" />
COLUMNS <input type="text" name="cols" value = "<?php echo $cols;?>" />
HIGHLIGHT <input type = "text" name = "highlight" value = "<?php echo $highlight;?>" /><br>
<input type="submit" value="Generate">
</form>
<?php
if(isset($_POST['rows']))
{
$randnumber = rand(0,100);
$rows = $_POST['rows'];
$cols = $_POST['cols'];
$highlight = $_POST['highlight'];
echo '<table border="1" align = "center">';
if (is_numeric($rows) and is_numeric($cols) and is_numeric($highlight))
{
if ($randnumber % 2 == 0)
{
echo '<center>The first number is <div class = "red">even</div></center>';
}
else
{
echo '<center>The first number is <div class = "green">odd</div></center>';
}
for($row = 1; $row <= $rows; $row++)
{
echo '<tr style = "background-color:green">';
for($col = 1; $col <= $cols; $col++)
{
if ($randnumber % $highlight == 0)
{
echo '<td style = "background-color: red">';
echo $randnumber;
$randnumber++;
echo '</td>';
}
else
{
echo '<td>';
echo $randnumber;
$randnumber++;
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
else
{
echo "<center>Rows / Columns / Highlight must ALL be INTEGER values. Re-enter correct value(s).</center>";
}
echo '<pre><center>';
print_r($_POST);
echo '</center></pre>';
}
?>
<style type ="text/css">
h1 {
color: grey;
text-align:center;
}
form {
text-align: center;
padding-bottom: 20px;
}
a:link {
text-decoration: none;
}
.red {
color: red;
}
.green {
color: green;
}
</style>
</body>
</html>
So. I have this PHP code to generate a table based off the user's input and I recently ran into a problem I cant figure out how to fix.
It was working perfectly fine but now whenever I use the Refresh link it resets the entire page to default (i.e. default textbox values instead of keeping the current ones, removing the table).
So, I have 2 questions. How would I keep the data on refresh (with $_POST being used) and how to display the table with the default values when the page first loads.
Refresh
Clicking it will trigger browser's reload mechanism and you'll be asked to resubmit the form action, it will allow you to keep POST data.
You need to re-create the post if you want to keep the parameters. Can be done pretty eaily by looping thru the array.
<form method='POST' id='refresh' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php foreach($_POST as $k=>$v): ?>
<input type='hidden' name='<?php echo $k; ?>' value='<?php echo $v; ?>' />
<?php endforeach; ?>
<a href='#' onclick='document.getElementById("refresh").submit(); return false;'>refresh</a>
</form>
Note: This is a little longer than the other answer, but will not prompt to resend post data.

How to set background-color in only last row of tr which is dynamically generated using php

<table width="100%">
<tr>
<?
$count=0;
$i=1;
foreach($gallery as $key => $list1)
{
$count++;
?>
<td>
<img src='<?echo base_url();?>userfiles/eventsgallery/small/<?=$list1['image'];?>'>
</td>
<? // style='background:red;'
if($count % 4 == 0 )
{
$color = "";
$i++;
if($i % 2 == 0)
{
$color = "style='background:orange;'";
}
else
{
$color = "style='background:black;'";
}
echo "</tr><tr ".$color.">";
}
}
?>
</tr>
You can use CSS :last-child pseudo to achieve that..
table.class_name tr:last-child {
background: #f00;
}
Demo
...in css:
.table1 tr:last-child {
background-color: #0B615E;
}
<table width="100%" class="table1">
<tr>
<?
$count=0;
$i=1;
foreach($gallery as $key => $list1)
{
$count++;
?>
<td>
<img src='<?echo base_url();?>userfiles/eventsgallery/small/<?=$list1['image'];?>'>
</td>
<?
// style='background:red;'
if($count % 4 == 0 )
{
$color = "";
$i++;
if($i % 2 == 0)
{
$color = "style='background:orange;'";
}
else
{
$color = "style='background:black;'";
}
echo "</tr><tr ".$color.">";
}
}?>
</tr>
</tr>
</tr>
</table>
If you would need to do it in your php loop, you can reverse your counter:
<?php
$count = count($gallery);
foreach($gallery as $key => $list1) {
$lastClass = $count == 1?' lastrow':'';
$count--;
?>
html here
<?php
}
?>
Use :last-child
CSS
table tr:last-child {
background-color:#ccc;
}
Or
jQuery
$("table tr").last().css('background','#ccc')
I gave jQuery option too because :last-child CSS selector has compatibility issue
Hope this is what you want
http://jsfiddle.net/HarishBoke/Xen99/
try this
<table width="100%" id="your_table_id">
<?
$count=0;
$i=1;
$size = sizeof($gallery);
foreach($gallery as $key => $list1) {
$count++;
$color = "";
if($size==$count) {
$color = "style='background:orange;'";
}
?>
<tr <?php echo $color;?>>
<td>
<img src='<?echo base_url();?>userfiles/eventsgallery/small/<?=$list1['image'];?>'>
</td>
</tr>
<?php
}
?>
</table>
OR you can Add a CSS Like this as #Mr. Alien answered
#your_table_id tr:last-child {
background: orange;
}

Go to next row when HTML table is full

I am using an HTML table to display data from a MySQL table using PHP. I need it so once the table has 10 columns, it will move on to the next row.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
while($row = mysqli_fetch_array($result))
{
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
How can this be done?
Untested but something like this should work or get you started in a good direction:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
$x=0;
while($row = mysqli_fetch_array($result))
{
if($x==0){
echo "<tr>\n";
}elseif($x%10){
echo"</tr><tr>\n";
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
$x++;
}
echo "</tr></table>";
mysqli_close($con);
?>
Add a counter to your loop starting at one.
Each time through the loop if the remainder after dividing the counter value by 10 is 1
add the <tr>. If the remainder is 0 then add a </tr> Then after the loop a </tr> if the remainder is not evenly divisible by 10.
<?php
echo '<table width="100%" border="1px"><tr width="100%">';
$i = 0;
while($row = mysqli_fetch_array($result))
{
$i++;
?>
<?php if ($i%10 ==1): ?><tr><?php endif; ?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php if ($i%10 ==0): ?></tr><?php endif; ?>
<?php
}
if ($i%10 != 0) echo "</tr>";
echo "</tr></table>";
Using modulo (%)
After each 10th cell, if a new cell is added, the current row is closed and a new row is opened first, before outputting the cell.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = 0;
while($row = mysqli_fetch_array($result))
{
if ($cell++ % 10 == 0 && $cell > 1)
{
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
The extra condition && $cell > 1 seems to be a little odd, but without it, you will get an empty row to start with. Eliminating it by putting ++ before $cell will cause the first row to be 9 cells instead of 10. Putting $cell > 0 && in front of the modulo will cause cell never to be incremented, because the first part of the expression is always false. Moving the if to execute it after outputting the cell, would cause the risk of ending with an empty row. It could be solved using a do..while loop, but you'd have to check up front if you have one row at least.
Long story short: use the code above. :)
Using a simple counter and reset it after each row
I think it's even more readable without the modulo, though you'd have to initialize $cell to -1 to prevent the first row to be 9 cells. Nevertheless, I think this is cleaner:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = -1;
while($row = mysqli_fetch_array($result))
{
if (++$cell == 10)
{
$cell = 0;
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
<table>
<tr>
<?php
$endRow = 0;
$columns = 10; // number of columns
$hloopRow1 = 0;
do {
if($endRow == 0 && $hloopRow1++ != 0) echo "<tr>";
?>
<td>
<?php echo $row['Name']; ?>
</td>
<?php $endRow++; if($endRow >= $columns) { ?>
</tr>
<?php $endRow = 0; }
} while ($row = mysql_fetch_assoc($result));
if($endRow != 0) {
while ($endRow < $columns) {
echo("<td> </td>");
$endRow++;
}
echo("</tr>");
}
?>
</table>
This should work fine. Hope this helps.

Categories