Only single row displayed in table - php

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>";

Related

How should I change the cell color according to a specific price?

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 >";
}

Adding autonumber column to php table that reads from mysql

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

borderColor if null value

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>

Pass multiple variables through link (reads only one)

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>";

mysql_fetch_array with dates separated

I am trying to loop through MySQL results and return same dates & agents in a separate . The table containing this data has the number of tickets each agent works on a specific day. Each group of dates should be separated by a blank row in the table. Below is the code I am working with. I believe I have to do a foreach, but not sure how to get it working.
Here is a screenshot to a final table layout I am looking to achieve.
if($res && mysql_num_rows($res))
{
while($row = mysql_fetch_array($res))
{
if ($row['total_updated'] > 0) {
print "<tr>";
print "<td align=center>" . $row['date_added'] . "</td>";
print "<td nowrap>" . $row['agent'] . "</td>";
print "<td nowrap>" . $row['agent_location'] . "</td>";
print "<td align=center>" . number_format($row['total_updated']) . "</td>";
print "<td align=center>" . number_format($row['total_notes']) . "</td>";
print "<td align=center>" . number_format($row['total_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app1_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app2_closed']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_updated']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_notes']) . "</td>";
print "<td align=center>" . number_format($row['ticket_app3_closed']) . "</td>";
print "</tr>";
}
}
}
print "</table>";
If you sort your results by date_added you don't need any foreach, just compare previous date with current one:
while($row = mysql_fetch_array($res))
{
if (!isset($lastdate))
$lastdate = $row['date_added'];
if ($lastdate != $row['date_added']) {
?><tr><td colspan="15">---blank line---</td></tr> <?php
}
//paste all of your prints here
$lastdate = $row['date_added'];
}

Categories