php loop in a while loop - php

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

Related

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

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;

PHP get sum of SQL table

I have MAMP (local hosted SQL,WEB etc server) the database name is :NKTDEBITS the table name is :Insurance and the column on the table is STATECOV. I know I'm close with this but still get a black in the field that should generate the total, anyone got a idea?
<?php
$con=mysqli_connect("localhost","root","root","KNTDebits");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Insurance");
$result2 = mysqli_query($con,"SELECT * FROM Office");
$result3 = mysqli_query($con,"SELECT * FROM RichmondLocation");
$result4 = mysqli_query($con,"SELECT * FROM DanvilleLocation");
$result5 = mysql_query('SELECT SUM(STATECOV) AS STATECOV_sum FROM Insurance');
echo "<table border='1'>
<tr>
<th>Truck Number</th>
<th>VIN</th>
<th>Make</th>
<th>Model</th>
<th>State Coverage</th>
<th>Comprehinsive Coverage</th>
<th>Property Damage/th>
<th>Personal Injury</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['TNUM'] . "</td>";
echo "<td>" . $row['VIN'] . "</td>";
echo "<td>" . $row['MAKE'] . "</td>";
echo "<td>" . $row['MODEL'] . "</td>";
echo "<td>" . $row['STATECOV'] . "</td>";
echo "<td>" . $row['COMPRE'] . "</td>";
echo "<td>" . $row['PROPDMG'] . "</td>";
echo "<td>" . $row['PRSINJ'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 2 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Richmond</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row3 = mysqli_fetch_array($result3))
{
echo "<tr>";
echo "<td>" . $row3[''] . "</td>";
echo "<td>" . $row3['DATE'] . "</td>";
echo "<td>" . $row3['PAYMENT'] . "</td>";
echo "<td>" . $row3['PAYER'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 4 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Danville</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row4 = mysqli_fetch_array($result4))
{
echo "<tr>";
echo "<td>" . $row4[''] . "</td>";
echo "<td>" . $row4['DATE'] . "</td>";
echo "<td>" . $row4['PAYMENT'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 5 Start
echo "<table border='5'>
<tr>
<th>Total</th>
</tr>";
$result = mysql_query('SELECT SUM(STATECOV) AS value_sum FROM Insurance');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
while($row = mysql_fetch_assoc($result));
{
echo "<tr>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You need a GROUP BY to go along with your SUM. I don;t know enough about you table to let you know what column you should use for this.
You should be handling potential error cases whenn you query the database, as you will quickly see when you are getting database/query errors.
You should also look to use mysqli or PDO instead of the deprecated mysql_* functions.

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