PHP echo without a loop. SQL Query - php

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

Related

Displaying the color equivalent of the hex code retrieved in a table cell

I need to retrieve the color hex code stored in a database and display it as seen on screen within a table cell. i'm trying to find a way to convert the hex code the db query returns for that specific table cell into the actual color that the hex denotes. Here's my code...
<?php
// Include config file
require_once "config.php";
$pdo = new PDO($dsn, $username, $password, $options);
// Attempt select query execution
$sql = "SELECT * FROM sales";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>S/N</th>";
echo "<th>Transaction Date</th>";
echo "<th>Customer Name</th>";
echo "<th>Address</th>";
echo "<th>Phone Number</th>";
echo "<th>Vehicle Model</th>";
echo "<th>Vehicle Chassis Number</th>";
echo "<th>Vehicle Registration Number</th>";
echo "<th>Vehicle Color</th>";
echo "<th>Amount Paid</th>";
echo "<th>Advance</th>";
echo "<th>Balance</th>";
echo "<th>Balance Due Date</th>";
echo "<th>Paid To</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['transactiondate'] . "</td>";
echo "<td>" . $row['customername'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['vehiclemodel'] . "</td>";
echo "<td>" . $row['vehiclechassisnumber'] . "</td>";
echo "<td>" . $row['vehicleregistrationnumber'] . "</td>";
echo "<td>" . $row['vehiclecolor'] . "</td>";
echo "<td>" . $row['amountpaid'] . "</td>";
echo "<td>" . $row['advance'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td>" . $row['balancedate'] . "</td>";
echo "<td>" . $row['paidto'] . "</td>";
echo "<td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
It is a bit unclear about where you wish to use the colour but I'm assuming it is here. And also my assumption is the retrieved value for vehiclecolor column is a HEX value
echo "<td>" . $row['vehiclecolor'] . "</td>";
You can use tag property 'background-color' for this.
$temp = $row['vehiclecolor'];
echo "<td style='background-color:" . $temp ."'></td>";
Pretty self-explanatory I guess. String concatenation.

How to add "book button to each car product"?

I want to add "book" button to each car product.But it only display only one button for first car only.
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "</tr>";
}
There is no error.But i just want "book" button display for each car products.
This is simply because your button is out of the while loop !
Also you did not close first tr tag .
Correct code :
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>action</th>";<!-- Added this line -->
echo "</tr>";<!-- Added this line -->
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";<!-- Replaced This line -->
echo "</tr>";
}
echo "</table>";
I hope this helps you :)

How to stop a ++$i auto increment after a certain number

I'm making this table for a contest, as you can see I'm fetching data from a database as from a array, the thing is there's only 3 prizes and like 40 participants, I want to show every participant and the prizes he's winning so far, but after the third position I want to show the "No Aplica" status next to the name, the thing is I can't stop the counter from going up nor I can make it a set number so it stays on the "No Aplica" option
<table style="display:inline;" class='table-personal table-striped'>
<thead>
<tr>
<th class="line-header">PosiciĆ³n</th>
<th>Premio</th>
<th>Nombre</th>
<th>Puntos Jul</th>
<th>Puntos Ago</th>
<th>Puntos Sep</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
$e = -1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
echo "<tr class='posicion'>";
echo "<td class='line-rows'>" . $premio[++$i] . "</td>";
echo "<td>" . $row["nombre"] . "</td>";
echo "<td>" . $row["puntos_julio"] . "</td>";
echo "<td>" . $row["puntos_agosto"] . "</td>";
echo "<td>" . $row["puntos_septiembre"] . "</td>";
echo "</tr>";
if ($i >= 3) {
$i = -1;
}
}
?>
</tbody>
</table>
You need to increase the value $i if the value of $i is less than 3. Otherwise no need to increase the value of $i.
Do like this:
<?php
$i = 0;
$e = -1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
echo "<tr class='posicion'>";
echo "<td class='line-rows'>" . $premio[$i] . "</td>";
echo "<td>" . $row["nombre"] . "</td>";
echo "<td>" . $row["puntos_julio"] . "</td>";
echo "<td>" . $row["puntos_agosto"] . "</td>";
echo "<td>" . $row["puntos_septiembre"] . "</td>";
echo "</tr>";
if ($i < 3) {
$i++;
}
}
?>
You can try this
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
echo "<tr class='posicion'>";
if ($i < 3) {
echo "<td class='line-rows'>" . $premio[$i] . "</td>";
} else {
echo "<td class='line-rows'>" . $premio[3] . "</td>";
}
echo "<td>" . $row["nombre"] . "</td>";
echo "<td>" . $row["puntos_julio"] . "</td>";
echo "<td>" . $row["puntos_agosto"] . "</td>";
echo "<td>" . $row["puntos_septiembre"] . "</td>";
echo "</tr>";
$i++;
}

php loop in a while loop

How to do a for loop in a while loop , as the coding below. The table need to show a list of number of how many data columm had got in the
<?php
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("registration",$server);
$query = mysql_query("select * from user");
?>
<table class="striped">
<tr class="header">
<td>No</td>
<td>Id</td>
<td>Name</td>
<td>Title</td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {
for($x =1 ; $x <= $row ; $x++);
echo "<tr>";
echo "<td>"'$x'"</td>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['position']."</td>";
echo "</tr>";
}
?>## Heading ##
$count = 1;
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "</tr>";
++$count;
}
OR
$count = 1;
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $count++ . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "</tr>";
}
<?php
$num_rows = mysql_num_rows($query);
echo "<tr>";
echo "<td> Total Rows".$num_rows."</td>";
echo "</tr>";
$i = 1;
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['password']."</td>";
echo "<td>".$row['position']."</td>";
echo "</tr>";
$i++;
}
?>

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