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

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

Related

Group output of MySql Query

I have the following PHP/MySql:
$sql = "SELECT * from tblDigest";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</table>";
}
I would like to group the results by 'diagram', so that I can display them as an accordion. Is there any easy way to do this?
Failing that, how can I get the value for the first row of the table (so I can have a $diagNo variable and compare $row["diagram"] against that to find out when it has changed.
Sort your results by diagram and watch when its value changes:
$sql = "SELECT * from tblDigest ORDER BY diagram";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
$oldDiagram = '';
while($row = $result->fetch_assoc()) {
if($oldDiagram == '')
{
// this is the first diagram - create the first accordeon
$oldDiagram = $row['diagram'];
}
elseif($oldDiagram != $row['diagram'])
{
// a new group starts right now - create new accordeon
$oldDiagram = $row['diagram'];
}
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</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>";
}

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;

How can I join these two html tables together?

I want to create a compare sort of page. I currently have two tables with the information, but want it to be on table or join both together. The first row would show Name of brand, second would show cost, etc... I am grabbing the values of selected checkboxs and based on what they selected it get compared.
$testName = $_POST['testCompare'];
$rpl = str_replace("_", " ", $testName);
$firstOne = "$rpl[0]";
$secondOne = "$rpl[1]";
echo "$firstOne";
echo "<br/>";
echo "$secondOne";
$query = "SELECT * FROM test_table WHERE test_name = '$firstOne'";
$query2 = "SELECT * FROM test_table WHERE test_name = '$secondOne'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0) {
//if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
if ($firstOne == $row[1]) {
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<tr>";
echo "<td>" . $row[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
}
}echo "</table>";
while($row2 = mysql_fetch_row($result2)) {
if ($secondOne == $row2[1]) {
{ echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row2[0] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[3] . "</td>";
echo "</tr>";
echo "<td>" . $row2[4] . "</td>";
echo "</tr>";
}
}
}
echo "</table>";
}
else {
echo "No Results";
}[/CODE]
Thanks
Remove the </table> after the first loop.
Remove <table border=1> from both loops.
Add <table border=1 before the first loop.
Currently, you're defining a new <table border=1> each time you enter the loop. This will result in this HTML code:
<table ..>
<tr>...
<table ..>
..
<table>
..
Et cetera
</table>
<table ..>
Et cetera
</table>

Categories