while($row = mysql_fetch_array($query2testing)) {
echo "<tr>";
echo "<td><center>$i</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['matricNo'] . "</td>";
echo "<td><center>" . $row['LC'] . "</td>";
echo "<td>" . $row['Code'] . "</td>";
echo "<td>" . $row['Subject'] . "</td>";
if(!empty($row['Assignment1'])) {
echo "<td><center><font color='red'>" . $row['Assignment1'] . "</td>";
}
echo "<td><center>" . $row['Quiz'] . "</td>";
echo "<td><center>" . $row['Participation'] . "</td>";
echo "<td><center>" . $row['Attendance'] . "</td>";
echo "<td><center>" . $row['Exam'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
This is my simple code.
How to make my column red if assigment1 is null
and assigment1 is not null to blue
style="background-color:red;" and background-color:blue; is possibly what you are looking for. You did not specify how the columns should be painted so you might have to adjust this.
Just insert this to your columns or rows you want to apply the color:
e.g. for a row (guess this is your header):
echo "<td style="background-color:red;"><center><font color='blue'>" . $row['Assignment1'] . "</td>";
use this in the first echo
echo "<tr style='background-color:" . (($row['Assignment1'] === NULL) ? "red" : "blue") . "'>";
Please replace your code with below code:
while($row = mysql_fetch_array($query2testing))
{
echo "<tr>";
echo "<td><center>$i</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['matricNo'] . "</td>";
echo "<td><center>" . $row['LC'] . "</td>";
echo "<td>" . $row['Code'] . "</td>";
echo "<td>" . $row['Subject'] . "</td>";
if(!empty($row['Assignment1']))
{
echo "<td><center><font color='blue'>" . $row['Assignment1'] . "</td>";
}
else
{
echo "<td><center><font color='red'>" . $row['Assignment1'] . "</td>";
}
echo "<td><center>" . $row['Quiz'] . "</td>";
echo "<td><center>" . $row['Participation'] . "</td>";
echo "<td><center>" . $row['Attendance'] . "</td>";
echo "<td><center>" . $row['Exam'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
Try this:
while($row = mysql_fetch_array($query2testing))
{
echo "<tr>";
echo "<td><center>$i</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['matricNo'] . "</td>";
echo "<td><center>" . $row['LC'] . "</td>";
echo "<td>" . $row['Code'] . "</td>";
echo "<td>" . $row['Subject'] . "</td>";
if(!empty($row['Assignment1']))
{
echo "<td><center><font color='blue'>" . $row['Assignment1'] . "</td>";
}
else {
echo "<td><center><font color='red'>" . $row['Assignment1'] . "</td>";
}
echo "<td><center>" . $row['Quiz'] . "</td>";
echo "<td><center>" . $row['Participation'] . "</td>";
echo "<td><center>" . $row['Attendance'] . "</td>";
echo "<td><center>" . $row['Exam'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
Three (3) things:
Let's convert your MySQL to MySQLi. Just put else on to your if(empty($row['Assignment1'])). AND Border Color is what you want to change color, right?
<html>
<body>
<?php
$con=mysqli_connect("host","username","password","database");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
$i=1;
$query2testing=mysqli_query($con,"SELECT * FROM yourTable");
echo "<table>";
while($row = mysqli_fetch_array($query2testing))
{
echo "<tr>";
echo "<td><center>$i</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['matricNo'] . "</td>";
echo "<td><center>" . $row['LC'] . "</td>";
echo "<td>" . $row['Code'] . "</td>";
echo "<td>" . $row['Subject'] . "</td>";
if(empty($row['Assignment1']))
{
echo "<td style='border: 1px solid red;'><center>" .$row['Assignment1'] . "</td>"; /* YOU WANT THE BORDER TO CHANGE COLOR, RIGHT? */
}
else {
echo "<td style='border: 1px solid blue;'><center>" .$row['Assignment1']."</td>"; /* YOU WANT THE BORDER TO CHANGE COLOR, RIGHT? */
}
echo "<td><center>" . $row['Quiz'] . "</td>";
echo "<td><center>" . $row['Participation'] . "</td>";
echo "<td><center>" . $row['Attendance'] . "</td>";
echo "<td><center>" . $row['Exam'] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
?>
</body>
</html>
Related
I want to change the color of the entire cell according to a specific value. But what should I do when the grammar is wrong and it doesn't work?
in this my code
echo "<tr if( $row['treatment_fees_check_division'] == 'error') : style='yellow' >";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['chart_num'] . "</td>";
echo "<td>" . $row['chart_name'] . "</td>";
echo "<td>" . substr($row['visit'], 0,10) . "</td>";
echo "<td >" . number_format($row['total_medical_bills']) . "</td>";
echo "<td >" . number_format($row['total_amount']) . "</td>";
echo "<td>" . number_format($row['amount_asked']) . "</td>";
echo "<td>" . number_format($row['medical_bills_payment']) . "</td>";
echo "<td>" . number_format($row['personal_liability_amount']) . "</td>";
echo "<td >" . number_format($row['non_payment']) . "</td>";
echo "<td >" . number_format($row['non_payment_sales']) . "</td>";
echo "<td>" . $row['insurance_division'] . "</td>";
echo "<td>" . $row['division'] . "</td>";
echo "<td>" . number_format($row['cash_amount_received']) . "</td>";
echo "<td>" . number_format($row['cash_receipt']) . "</td>";
echo "<td>" . number_format($row['cash_receipt_non_payment']) . "</td>";
echo "<td>" . number_format($row['cash_receipt_payment']) . "</td>";
echo "<td>" . number_format($row['card_amount_received']) . "</td>";
echo "<td>" . number_format($row['card_non_payment']) . "</td>";
echo "<td>" . number_format($row['card_payment']) . "</td>";
echo "<td>" . number_format($row['treatment_fees_difference']) . "</td>";
echo "<td>" . $row['treatment_fees_check_division'] . "</td>";
echo "</tr>";
Try replacing your first line with something like this.
if ($row['treatment_fees_check_division'] == 'error') {
echo "<tr style='yellow' >";
} else {
echo "<tr >";
}
Here's my code:
foreach ($grabdata as $row) {
echo "<tr>";
echo "<td class='editableColumns'>" . $row['EmployeeName'] . "</td>";
echo "<td class='editableColumns'>" . $row['EmployeeID'] . "</td>";
echo "<td class='editableColumns'>" . $row['ContactNumber'] . "</td>";
echo "<td class='editableColumns'>" . $row['Gender'] . "</td>";
echo "<td class='editableColumns'>" . $row['Email'] . "</td>";
echo "<td class='editableColumns'>" . $row['DOB'] . "</td>";
echo "<td class='editableColumns'>" . $row['AreaOfExpertise'] . "</td>";
echo "<td class='editableColumns'>" . $row['State'] . "</td>";
echo "<td class='editableColumns'>" . $row['Status'] . "</td>";
echo "<td><input class='editValues' type='button' value='Edit'></td>";
echo "</tr>";
echo "</table>";
}
I expected all data in the db to be displayed in a table. Unfortunately only the first result appears in the table and the following appear without the table. What am I doing wrong?
You need to put the </table>-Tag outside of the foreach loop:
echo "<table>";
foreach ($grabdata as $row) {
echo "<tr>";
echo "<td class='editableColumns'>" . $row['EmployeeName'] . "</td>";
echo "<td class='editableColumns'>" . $row['EmployeeID'] . "</td>";
echo "<td class='editableColumns'>" . $row['ContactNumber'] . "</td>";
echo "<td class='editableColumns'>" . $row['Gender'] . "</td>";
echo "<td class='editableColumns'>" . $row['Email'] . "</td>";
echo "<td class='editableColumns'>" . $row['DOB'] . "</td>";
echo "<td class='editableColumns'>" . $row['AreaOfExpertise'] . "</td>";
echo "<td class='editableColumns'>" . $row['State'] . "</td>";
echo "<td class='editableColumns'>" . $row['Status'] . "</td>";
echo "<td><input class='editValues' type='button' value='Edit'></td>";
echo "</tr>";
}
echo "</table>";
I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down into the table. Unfortunately the table is adding an additional row to the bottom of the table making 4 rows when in fact there is only 3 rows displaying in the database.
Any idea as to why?
<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('******');
$query = mysql_query("SELECT * FROM tablename");
echo "<table border='1'>
<tr>
<td>Date</td>
<td>Region</td>
<td>Cameraman</td>
<td>Livestream?</td>
<td>Event Title</td>
<td>Lecturer</td>
<td>Time</td>
<td>Speaker</td>
<td>ICE Contact</td>
<td>Venue Address</td>
<td>Venue Contact</td>
<td>Additional Comments</td>
<td>On App?</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
endwhile;
echo "</table>";
?>
I have added the link to the page below.
http://cpdonline.tv/spreadsheet/spreadsheet.php
Update your while loop with the following:
WHILE($rows = mysql_fetch_array($query)):
$rows = array_filter($rows);
if (!empty($rows)) {
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
}
endwhile;
echo "</table>";
array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false.
It seems that you have empty data in your database, as many lines are empty
Check your database for empty records or run the query
DELETE FROM table_name WHERE column='';
I am trying to add a column before my first column that starts numbering each record, starting from 1. I was trying to add autoincrement to this line echo "" . $row['ss'] . "";, but that does not seem to want to work. Any ideas how to do this?
My code looks like this:
$result = mysqli_query($con,"SELECT * FROM `results` WHERE Event='100' AND Gender='M' ORDER BY Performance ASC");
echo "<table border='0'>
<tr>
<th align='left'>Pos</th>
<th align='left'>Event</th>
<th>Performance</th>
<th>Wind</th>
<th>Place</th>
<th align='left'>Name</th>
<th align='left'>Surname</th>
<th>Age</th>
<th>Team</th>
<th>Meet</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Add this line in header
<th align='left'>#</th>
And here php code
$count = 1;
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $row['ss'] . "</td>";
echo "<td>" . $row['Event'] . "</td>";
echo "<td>" . $row['Performance'] . "</td>";
echo "<td>" . $row['Wind'] . "</td>";
echo "<td>" . $row['Pos'] . "</td>";
echo "<td width='100' align='left'>" . $row['Surname'] . "</td>";
echo "<td Width='100'>" . $row['Name'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['Meet'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
//other code
$count=$count+1;
}
A better solution would be to use a variable counter to "number" your rows:
$counter = 1;
while($row = mysqli_fetch_array($result))
echo "<td>" . $counter . "</td>";
$counter++;
}
This is the right way to do it since if that ss column is the mysql auto increment, then your rows will not be numbered by the SORT but rather from the order in which they were inserted into the database regardless of any sorting you apply.
Try to change fetch_array with fetch_assoc
I'm trying to pass some variables from a link but for some reason it only reads one of them. Below is the link and the code. roundNo shows up fine.
archery/viewround.php?gameNo=1&roundNo=7
echo $_GET['gameNo'];
echo $_GET['roundNo'];
echo "<table border><tr><th>Round ID</th><th>Match Name</th><th>Player Name</th><th>Shot 1</th><th>Shot 2</th><th>Shot 3</th><th>Shot 4</th><th>Shot 5</th><th>Shot 6</th><th>Shot 7</th><th>Shot 8</th><th>Shot 9</th><th>Shot 10</th></tr>";
while ($row = mysql_fetch_array($sql)) {
$tempPlayerName = getnamewithid($row["player_id"]);
$tempMatchName = getmatchnamewithid($_GET['gameNo']);
echo "<tr>";
echo '<td>' . $row["round_id"].'</td>';
echo "<td>" . $tempMatchName. "</td>";
echo "<td>" . $tempPlayerName. "</td>";
echo "<td>" . $row["shot_1"] . "</td>";
echo "<td>" . $row["shot_2"] . "</td>";
echo "<td>" . $row["shot_3"] . "</td>";
echo "<td>" . $row["shot_4"] . "</td>";
echo "<td>" . $row["shot_5"] . "</td>";
echo "<td>" . $row["shot_6"] . "</td>";
echo "<td>" . $row["shot_7"] . "</td>";
echo "<td>" . $row["shot_8"] . "</td>";
echo "<td>" . $row["shot_9"] . "</td>";
echo "<td>" . $row["shot_10"] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";