i wanted to ask I would I add up the points of all the drivers in the table produced below by the following code as this only displays points of all the drivers in each row ... There is a table Points which has points, position and a table Drivers having forename, surname.
<?php
session_start();
if ( !isset($_SESSION['loggedin']) ) {
header("Location: login.php");
}
$con=mysqli_connect("xxxxx","xxxxx","xxxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Drivers.forename, Drivers.surname, Teams.name, Teams.engine, Points.points
From Drivers, Teams, Points");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Team Name</th>
<th>Engine</th>
<th>Points</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['forename'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['engine'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
in your code, before the $while
$counter=0
while($row = mysqli_fetch_array($result))
{
$counter += $row['points'];
echo "<tr>";
echo "<td>" . $row['forename'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['engine'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo $counter; //echoes sum of points
Related
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>";
}
this is my php code for sql query its giving me same value multi time
if (isset($_GET["q"]))
{
$q= $_GET["q"];
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"onesports");
$result=mysqli_query($link,"select players.full_name, team.* from players,team where team.team_name='" .$q. " ' and players.team_name=team.team_name ");
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<a href=>" . $row['full_name'] . "</a>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['team_name'] . "</td>";
echo "<td>" . $row['prov'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['captain'] . "</td>";
echo "<td>" . $row['coach'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else {
echo "no result found.";
}
its giving me this output
enter image description here
i want only player name and remaining value want for one time
You should use INNER JOIN in this case.
just update your code with below code.
$result = mysqli_query($link, "SELECT `players`.full_name,`team`.* FROM `players` INNER JOIN `team` ON `players`.team_name= `team`.team_name AND `team`.team_name='".$q."'");
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
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.
im making some application form in PHP.
im putting all info returned from the database into a table.
Now i want to create a button on each line that changes something in the DB of that line.
but i have no idea to do that :S
Thank you!
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td>" . $row['exp'] . "</td>";
echo "<td>" . $row['motivation'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . '<input type="submit" name="submit" value="accept">' . "</td>";
echo "</tr>";
}
echo "</table>";
EDIT: get it working using another script:
echo "<td>Approve</td>";
and edit.php:
<?php
include("dbconnect.php");
$member_id = $_GET['id'];
$status = $_GET['status'];
echo $member_id;
echo $status;
if ($status == 'app')
$query = "update apps set status = 'approved' where id = $member_id";
mysql_query($query) or die (mysql_error());
?>
Create small form with parameters in the cell you want the button to do smth. This is the simpliest approach (approach with refresh).
One more solution is to use AJAX on button click and forward action to some endpoint. This way it would be dynamic and probably what you are want to implement.
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>age</th>
<th>position</th>
<th>experience</th>
<th>motivation</th>
<th>date</th>
<th>status</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td>" . $row['exp'] . "</td>";
echo "<td>" . $row['motivation'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . '<form type="POST"><input type="hidden" name="whatever" value="$row['id']"><input type="submit" name="submit_btn" value="accept"></form>' . "</td>";
echo "</tr>";
}
echo "</table>";
in this way, you could get a form in each row of the table. Now, you just need to use php POST function.
if(isset($_POST['submit_btn']))
{
//whatever u need to do
}