PHP get value from associative array by variable from loop - php

I'm not sure my question is right.
I have made a html table and filled it with test data from the database.
Image from the table: https://imgur.com/a/UVH1e
This is my table:
<table class="table table-striped table-bordered turnover_list">
<thead>
<tr class="sorting_1">
<th>Company</th>
<th>Year</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
$currentYear = date('Y');
$check = "SELECT * FROM turnover WHERE year = '" . $currentYear . "'";
$result = mysql_query($check) or die(mysql_error());
foreach ($allCompanies as $companyName) {
$companyTurnoverTotal = 0;
$turnoverTotal = 0;
echo "<tr>";
echo "<td style='width:7%;'>" . $companyName . "</td>";
echo "<td style='width:7%;'>" . $currentYear . "</td>";
$monthTotal = array("January"=>"0", "February"=>"0", "March"=>"0", "April"=>"0", "May"=>"0", "June"=>"0", "July"=>"0", "August"=>"0", "September"=>"0", "October"=>"0", "November"=>"0", "December"=>"0");
foreach ($months as $month) {
$sql = "SELECT SUM(mob + www) AS 'turnover' FROM turnover WHERE year = '" . $currentYear . "' AND companyID = '" . getCompanyID($companyName) . "' AND month = '" . $month . "'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$companyTurnoverMonth = $row['turnover'];
$monthTotal[$month] += $companyTurnoverMonth;
echo "<script>alert('" . $month . ": " . $monthTotal[$month] . "')</script>";
$companyTurnoverTotal += $companyTurnoverMonth;
$turnoverTotal += $companyTurnoverTotal;
echo "<td style='width:7%;'>" . make_format($companyTurnoverMonth) . "</td>";
}
echo "<td style='width:7%; font-weight: bold; background-color:#d3d3d3;'>" . make_format($companyTurnoverTotal) . "</td>";
echo "</tr>";
}
echo "<tr style='font-weight: bold; background-color:#d3d3d3;'>";
echo "<td style='width:7%;'>" . "Total" . "</td>";
echo "<td style='width:7%;'>" . $currentYear . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['January']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['February']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['March']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['April']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['May']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['June']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['July']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['August']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['September']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['October']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['November']) . "</td>";
echo "<td style='width:7%;'>" . make_format($monthTotal['December']) . "</td>";
echo "<td style='width:7%;'>" . make_format($turnoverTotal) . "</td>";
echo "</tr>";
?>
</tbody>
I want a table column at the right that shows the total of every company.
At the bottom of the table I want a row with columns per month that shows the total of every month.
I was trying to make an array with all the months and increase the value with the next column in the same month. $monthTotal[$month] += $companyTurnoverMonth;
foreach ($months as $month) {
$sql = "SELECT SUM(mob + www) AS 'turnover' FROM turnover WHERE year = '" . $currentYear . "' AND companyID = '" . getCompanyID($companyName) . "' AND month = '" . $month . "'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$companyTurnoverMonth = $row['turnover'];
$monthTotal[$month] += $companyTurnoverMonth;
$companyTurnoverTotal += $companyTurnoverMonth;
$turnoverTotal += $companyTurnoverTotal;
echo "<td style='width:7%;'>" . make_format($companyTurnoverMonth) . "</td>";
}

Related

foreach loop highlight cells from database

I'm wondering if my code could be easier with a foreach loop. my code thusfar:
the purpose is to read the values in the MYSQL table, and if the anwser is "NEE" display the background color in RED. my code works but it is very long..
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is
the password
mysql_select_db('heijsDB');
$query = "SELECT * FROM hygieneaanvoer"; //You don't need a ; like you do in
SQL
$result = mysql_query($query);
//List the Columns for the Report
if(! $result ) {
die('Could display data: ' . mysql_error());
}
echo "<table border='1' class='w3-panel'>
<fieldset>hygiëne/GMP aduit Aanvoer</fieldset>
<tr>
<th>Datum</th>
<th>Controleur</th>
<th>controleur</th>
<th>Revisie</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['controleur'] . "</td>";
echo "<td>" . $row['codering'] . "</td>";
echo "<td>" . $row['revisie'] . "</td>";
if($row['q1']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q1']."</td>";
else echo "<td>".$row['q1']."</td>";
if($row['q2']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q2']."</td>";
else echo "<td>".$row['q2']."</td>";
if($row['q3']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q3']."</td>";
else echo "<td>".$row['q3']."</td>";
if($row['q4']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q4']."</td>";
else echo "<td>".$row['q4']."</td>";
if($row['q5']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q5']."</td>";
else echo "<td>".$row['q5']."</td>";
if($row['q6']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q6']."</td>";
else echo "<td>".$row['q6']."</td>";
if($row['q7']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q7']."</td>";
else echo "<td>".$row['q7']."</td>";
if($row['q8']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q8']."</td>";
else echo "<td>".$row['q8']."</td>";
if($row['q9']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q9']."</td>";
else echo "<td>".$row['q9']."</td>";
if($row['q10']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q10']."</td>";
else echo "<td>".$row['q10']."</td>";
if($row['q11']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q11']."</td>";
else echo "<td>".$row['q11']."</td>";
if($row['q12']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q12']."</td>";
else echo "<td>".$row['q12']."</td>";
if($row['q13']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q13']."</td>";
else echo "<td>".$row['q13']."</td>";
if($row['q14']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q14']."</td>";
else echo "<td>".$row['q14']."</td>";
if($row['q15']=='NEE') // [val1] can be 'approved'
echo "<td style='background-color: #e21010;'>".$row['q15']."</td>";
else echo "<td>".$row['q15']."</td>";
echo "</tr>";
}
echo "</table>";
?>
Here is solution
$result = mysql_query("SELECT * FROM hygieneaanvoer")or die('Could display data: ' . mysql_error());;
echo "<table border='1' class='w3-panel'>
<fieldset>hygiëne/GMP aduit Aanvoer</fieldset>
<tr>
<th>Datum</th>
<th>Controleur</th>
<th>controleur</th>
<th>Revisie</th>";
for ($i=1;$i<=15;$i++){
echo '<th>'.$i.'</th>';
}
echo "</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['controleur'] . "</td>";
echo "<td>" . $row['codering'] . "</td>";
echo "<td>" . $row['revisie'] . "</td>";
for ($j=1;$j<=15;$j++){
echo "<td ".(($row['q'.$j] == 'NEE') ? "style='background-color: #e21010;'" : "" ).">".$row['q'.$j]."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
What you could do, is just create a single variable that you'd use for setting the style of all rows:
while($row = mysql_fetch_array($result))
{
$style_string = "";
if($row['q1']=='NEE') {
$style_string = "style='background-color: #e21010;'";
}
echo "<tr>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['controleur'] . "</td>";
echo "<td>" . $row['codering'] . "</td>";
echo "<td>" . $row['revisie'] . "</td>";
echo "<td " . $style_string . ">" . $row['q1'] . "</td>";
echo "<td " . $style_string . ">" . $row['q2'] . "</td>";
echo "<td " . $style_string . ">" . $row['q3'] . "</td>";
echo "<td " . $style_string . ">" . $row['q4'] . "</td>";
echo "<td " . $style_string . ">" . $row['q5'] . "</td>";
echo "<td " . $style_string . ">" . $row['q6'] . "</td>";
echo "<td " . $style_string . ">" . $row['q7'] . "</td>";
echo "<td " . $style_string . ">" . $row['q8'] . "</td>";
echo "<td " . $style_string . ">" . $row['q9'] . "</td>";
echo "<td " . $style_string . ">" . $row['q10'] . "</td>";
echo "<td " . $style_string . ">" . $row['q11'] . "</td>";
echo "<td " . $style_string . ">" . $row['q12'] . "</td>";
echo "<td " . $style_string . ">" . $row['q13'] . "</td>";
echo "<td " . $style_string . ">" . $row['q14'] . "</td>";
echo "<td " . $style_string . ">" . $row['q15']. "</td>";
echo "</tr>";
}
(Or just set the background color of the <tr>)
Best of luck!
You can simply add the class to the td like this
echo "<td class='". $row['q15'] . "'>".$row['q15']."</td>";
Add css like this
.NEE {background-color: #e21010;}

return one records from database

Thanks for answer. Now it's work, but I have next problem.
I remade a method to display records from the database:
public $id, $nazwa, $quantity, $data, $godzina, $sn, $added, $kategoria, $numrows;
public function magazyn() {
$resultmag = $this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `id`");
$resultmag = $this->conn->set_charset("utf8");
$resultmag = $this->magazyn->execute();
$resultmag = $this->magazyn->get_result();
if ($isset = $resultmag->num_rows > 0) {
while ($row = $resultmag->fetch_object()) {
$this->id = $row->id;
$this->nazwa = $row->nazwa;
$this->quantity = $row->quantity;
$this->data = $row->data;
$this->godzina = $row->godzina;
$this->sn = $row->sn;
$this->added = $row->added;
$this->kategoria = $row->kategoria;
}
$this->numrows = $resultmag->num_rows;
} else {
echo "No results database";
}
}
function __toString() {
return (string) $this->numrows;
}
//-------------------------------
$mag = new Magazyn();
$mag->magazyn();
?>
<table class="table table-bordered">
<tr><th style='text-align:center'>ID</th><th style='text-align:center'>DODAŁ</th><th style='text-align:center'>NAZWA</th><th style='text-align:center'>KATEGORIA</th><th style='text-align:center'>SERIAL</th><th style='text-align:center'>ILOŚĆ</th><th style='text-align:center'>DATA</th><th style='text-align:center'>GODZINA</th><th style='text-align:center' colspan='2'>AKCJA</th></tr>
<?php
$color = NULL;
$color1 = '#99bbff';
$color2 = '#b3ccff';
$color == $color1 ? $color = $color2 : $color = $color1;
echo "<tr class='active'>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->id . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=userpanel&user=" . $mag->added . "'>" . $mag->added . "</a></td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->nazwa . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=category&cat=" . $mag->kategoria . "'>" . $mag->kategoria . "</a></td>";
echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=editserial&sn=" . $mag->sn . "'>" . $mag->sn . "</a></td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->quantity . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->data . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->godzina . "</td>";
echo "<td align='center'><a href='panel.php?page=magazyn&action=edit&sn=" . $mag->sn . "&nazwa=" . $mag->nazwa . "&kategoria=" . $mag->kategoria . "&id=" . $mag->id . "'</a><button class='btn btn-default'>Edytuj</button></td>";
echo "<td align='center'><a href='panel.php?page=magazyn&action=delete&sn=" . $mag->sn . "'</a><button class='btn btn-default'>Usuń</button></td>";
echo "</tr>";
The problem is that it displays ONLY ONE RECORD from the database.
When I add to while echo:
while ($row = $resultmag->fetch_array()) {
echo $this->id = $row['id'];
echo $this->nazwa = $row['nazwa'];
echo $this->quantity = $row['quantity'];
echo $this->data = $row['data'];
echo $this->godzina = $row['godzina'];
echo $this->sn = $row['sn'];
echo $this->added = $row['added'];
echo $this->kategoria = $row['kategoria'];
}
Displays all of the records but by:
$mag->magazyn();
As Dagon's comment above says, add your values to a multidimensional array. Instead of
public $id, $nazwa, $quantity, $data, $godzina, $sn, $added, $kategoria, $numrows;
Just do
public $records = array();
public $numrows;
Then you are going to append all of the records to this array:
public function magazyn() {
$resultmag = $this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `id`");
$resultmag = $this->conn->set_charset("utf8");
$resultmag = $this->magazyn->execute();
$resultmag = $this->magazyn->get_result();
if ($isset = $resultmag->num_rows > 0) {
while ($row = $resultmag->fetch_object()) {
$record['id'] = $row->id;
$record['nazwa'] = $row->nazwa;
// etc... Add all like above
$this->records[] = $record;
}
$this->numrows = $resultmag->num_rows;
} else {
echo "No results database";
}
}
Then in your HTML you loop over $mag->records:
<table class="table table-bordered">
<?php foreach ($mag->records as $record) : ?>
<tr><th style='text-align:center'>ID</th><th style='text-align:center'>DODAŁ</th><th style='text-align:center'>NAZWA</th><th style='text-align:center'>KATEGORIA</th><th style='text-align:center'>SERIAL</th><th style='text-align:center'>ILOŚĆ</th><th style='text-align:center'>DATA</th><th style='text-align:center'>GODZINA</th><th style='text-align:center' colspan='2'>AKCJA</th></tr>
<?php
$color = NULL;
$color1 = '#99bbff';
$color2 = '#b3ccff';
$color == $color1 ? $color = $color2 : $color = $color1;
echo "<tr class='active'>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $record['id'] . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=userpanel&user=" . $record['added'] . "'>" . $record['added'] . "</a></td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $record['nazwa'] . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=category&cat=" . $record['kategoria'] . "'>" . $record['kategoria'] . "</a></td>";
echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=editserial&sn=" . $record['sn'] . "'>" . $record['sn'] . "</a></td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $record['quantity'] . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $record['data'] . "</td>";
echo "<td align='center' style='background-color:" . $color . ";'>" . $record['godzina'] . "</td>";
echo "<td align='center'><a href='panel.php?page=magazyn&action=edit&sn=" . $record['sn'] . "&nazwa=" . $record['nazwa'] . "&kategoria=" . $record['kategoria'] . "&id=" . $record['id'] . "'</a><button class='btn btn-default'>Edytuj</button></td>";
echo "<td align='center'><a href='panel.php?page=magazyn&action=delete&sn=" . $record['sn'] . "'</a><button class='btn btn-default'>Usuń</button></td>";
echo "</tr>";
endforeach;
?>
</table>

PHP echo without a loop. SQL Query

Here is the subject matter: Link, Click a players name to see.
I want to have the players name as a header so that the players name isn't repeated over and over again for each resulting row in the Roster table. I know how to loop through results for rows, but I only want the name once at the top and don't know how to do that. This is how I print the table code wise
<?php
$pid = $_POST['PID'];
$query = "SELECT Rosters.PID, Rosters.Goals, Rosters.Assists, Rosters.PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
(Rosters.Goals + Rosters.Assists) AS Points
FROM Rosters
INNER JOIN Players
ON Rosters.PID = Players.pid
WHERE Rosters.PID = $pid
ORDER BY Points DESC, Goals DESC";
$result = $conn->query($query);
$query2 = "SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
SUM((Rosters.Goals + Rosters.Assists)) AS Points
FROM Rosters
INNER JOIN Players ON Rosters.PID = Players.pid
WHERE Rosters.PID =$pid
ORDER BY Points DESC , Goals DESC";
$result2 = $conn->query($query2);
echo
if (!empty($_POST["PID"])) {
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I just want Players.firstname and Players.lastname printed once before any of the tables are generated.
Try this:
if (!empty($_POST["PID"])) {
$row = $result->fetch_assoc();
echo "<span class='style35'>" . $row["firstname"] . " " . $row["lastname"] . "</span><br />";
$result->data_seek(0);
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I don't know if you wanted the name of the player in the totals, so I left it there, but you can delete it if you want. I also applied to the player name the same style as the total.
You could get the first row of the result before your loop, then just reset the cursor, and completely ignore the players name inside your loop.
$first = $result->fetch_assoc();
// print out first name
echo($first['name'] . " " . $first['surname']);
// reset cursor
$result->data_seek(0);
// do your loops and stuff.
while ($row = $result->fetch_assoc()) {
...
}
You can do like this.If you do not want to change the sql query.
$ctr = 0;
while ($row = $result->fetch_assoc()) {
$ctr++;
if($ctr == 1) {
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td> </tr>";
}
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}

Change properties of table when I send information from MySQL to webpage

I display the information of MySQL table in web, it's easy but I want to change the background color of a cell when it has certain value. In the example, my table has different fields: id, nombre, apellido1, apellido2, curso, e-mail, and direccion. I want to get that when the field curso = primero, the color of that cell was red.
<?php
$connect = mysql_connect("localhost", "root", "") ;
if (!$connect) {
die ("Can not connect: " . mysql_error () ) ;
}
mysql_select_db("modelobdclase", $connect) ;
$sql = "SELECT * FROM datosalumnado";
$myData = mysql_query($sql, $connect) ;
echo "<table border=1>
<tr>
<th> id_alumnado </th>
<th> nombre </th>
<th> apellido1 </th>
<th> apellido2 </th>
<th> curso </th>
<th> fechadenacimiento </th>
<th> e-mail </th>
<th> direccion </th>
</tr>";
[B]
$valor= "primero";
function dame_color($valor) {
if ($valor == 'primero') return 'red';
else ' ';
}
while ($record = mysql_fetch_array ($myData)) {
$color = dame_color($row->[B] 'curso' );
[B]echo "<td bgcolor=$color>";
echo "<tr>";
echo "<td>" . $record ['id_alumnado'] . "</td>";
echo "<td>" . $record ['nombre'] . "</td>";
echo "<td>" . $record ['apellido1'] . "</td>";
echo "<td>" . $record ['apellido2'] . "</td>";
echo "<td>" . $record ['curso'] . "</td>";
echo "<td>" . $record ['fechanacimiento'] . "</td>";
echo "<td>" . $record ['e-mail'] . "</td>";
echo "<td>" . $record ['direccion'] . "</td>";
}
echo "</table>" ;
mysql_close($connect) ;
?>
Try this
$color = ($record ['curso'] == 'primero') ? "style='background-color:#f00;'" : '';
echo "<td $color>" . $record ['curso'] . "</td>";
Thanks user 36.... it was a fantastic answer. It works!!
The complete code would be:
function dame_color($valor) {
if ($valor == 'primero') return 'red';
else return 'white';
}
while ($record = mysql_fetch_array ($myData)) {
$color = ($record ['curso'] == 'primero') ? "style='background-color:#f00;'" : '';
echo "<tr >";
echo "<td>" . $record ['id_alumnado'] . "</td>";
echo "<td>" . $record ['nombre'] . "</td>";
echo "<td>" . $record ['apellido1'] . "</td>";
echo "<td>" . $record ['apellido2'] . "</td>";
echo "<td $color>" . $record ['curso'] . "</td>";
echo "<td>" . $record ['fechanacimiento'] . "</td>";
echo "<td>" . $record ['e-mail'] . "</td>";
echo "<td>" . $record ['direccion'] . "</td></tr>";
}
echo "</table>" ;

How to add up totals using php?

been trying out my new skills in php to build an accounts style system, that logs incoming and out going payments similar to what you would do in excel. thought this would be a good starting point to test my stills.
now ive built a form which submits to a database, and also a page that allows you to view the payments logged via date.
stuck on getting it to display a profit total. This will be worked out from the incoming payments total minus the outgoing payments totals.
ive attached my code below, id really appreciate any help i can get on this.
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
after adding code provided below:
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming .= $row['incoming'];
$totalOutgoing .= $row['outgoing'];
}
echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;
mysqli_close($con);
?>
ok is this right?
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming += $row['incoming'];
$totalOutgoing += $row['outgoing'];
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;
mysqli_close($con);
?>
Try This
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming += $row['incoming'];
$totalOutgoing += $row['outgoing'];
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;

Categories