I am making a soccer site. I have the following table structure:
season1 (name of the table)
id | player1 | player2 | score
I want to put the scores into a html table. This table is only for player called nickname1 vs every other player. I have this:
$query = mysql_query("SELECT * FROM season1 WHERE player1='nickname1'") or die(mysql_error());
while($row = mysql_fetch_array( $query )) {
echo "<tr><td>";
echo "";
echo "</td><td>";
echo $row['score2'];
echo "</td><td>";
echo $row['score3'];
echo "</td><td>";
echo $row['score4'];
echo "</td><td>";
echo $row['score5'];
echo "</td></tr>";
}
echo "</table> <br>";
Instead of $row['score2'] I need the score between nickname1 vs
nickname2
Instead of $row['score3'] I need the score between nickname1 vs
nickname3
Instead of $row['score4'] I need the score between nickname1 vs
nickname4
Instead of $row['score5'] I need the score between nickname1 vs
nickname5
I tried SELECT * FROM season1 WHERE player1='nickname1' AND player2='nickname2' but then it will show only player1 vs player2 in the table. I want to show all 4 players vs player1 in the same table.
Assuming score1 it's player 1 score:
$query = mysql_query("SELECT * FROM season1 WHERE player1='nickname1'") or die(mysql_error());
while($row = mysql_fetch_array( $query )) {
echo "<tr><td>";
echo "";
echo "</td><td>";
echo $row['player1']." vs ".$row['player2'].", with a score of: ".$row['score'];
echo "</td></tr>";
}
echo "</table> <br>";
I hope it helps.
Saludos ;)
You need to focus your loop only on making one row:
echo "<table><tr>";
while($row = mysql_fetch_array( $query )) {
echo "<td>{$row['score']}</td>";
}
echo "</tr></table>";
This will ONLY output the scores of the players. You can modify it to have nickname headers and so forth.
Take note that the $row array resembles your table structure. $row['score'] will give you the scores of the players where as since score1 is not a column in the table, it does not exist in $row.
Possible design:
echo "<table><tr><th>Player 1</th><th>Player 2</th><th>Score</th></tr><tr>";
while($row = mysql_fetch_array( $query )) {
echo "<td>{$row['player1']}</td>";
echo "<td>{$row['player2']}</td>";
echo "<td>{$row['score']}</td>";
}
echo "</tr></table>";
will output (depending on ID's in database)
Player 1 | Player 2 | Score
p1 p2 0
p1 p3 0
NOTICE: MySQL_* has been deprecated in PHP 5.5. Use MySQLi or PDO instead
Related
I am busy creating a website that allows people to host dinners and allows users to join those dinners. I have two different database tables called gebruikers (users) and diner (dinner). I am working on a search page that makes it possible for a user to search on a woonplaats (city) and then shows the users living in that city. That works right now, but I want to make sure that only the users are shown who have added a dinner. There is a common key in both tables: diner.gebruikersid = gebruikers.id
So in other words, I only want the users with their ID in both the diner and the gebruikers table to be displayed in the search results. How can I do that?
I have this code at the moment:
$zoeken = $_POST['woonplaats'];
$sql = "SELECT id, voornaam, straatnaam FROM gebruikers WHERE woonplaats LIKE '$zoeken'";
$result = mysql_query($sql);
echo "<div class=res>";
echo "<h3>Resulaten voor " .$zoeken. "</h3>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>Voornaam</th>";
echo "<th>Straatnaam</th>";
echo "<th></th>";
echo "</tr>";
echo "</thead>";
while($row = mysql_fetch_assoc($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td>".$row['voornaam']."</td>";
echo "<td>".$row['straatnaam']."</td>";
echo "<td>";
echo "<form action='eetprofiel.php' method='post'>";
echo "<input type='hidden' name='id' value='".$row['id']."'>";
echo "<input type='submit' value='Profiel bekijken'>";
echo "</form>";
echo "<td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
echo "</div>";
?>
Edit:
These are all the users in my database (in table gebruikers) with woonplaats Amersfoort
These are all the diners submitted by those users.
As you see, the user with gebruikers.id and diner.gebruikersid 23 has no dinner submitted, so I don't want him to be shown in the search results.
You should join the 2 tables. With JOIN take a look at this
https://www.tutorialspoint.com/mysql/mysql-using-joins.htm
example of a join
select tableA.user ,tableB.user from tableA JOIN tableB on tableA.id = tableB.id
note: don't use mysql it is deprecated use mysqli or PDO instead
I'm trying to display information from two queries in one single table, but can't figure out how to make it work.
This is what I got working with one query:
SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;
Company Employees
ABC 45
DEF 15
GHI 5
Now beneath that I'd like to have another query that simply counts all rows, giving me the total amount of Employees.
SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;
SELECT COUNT(*) AS Total FROM Employee_Table;
Company Employees
ABC 40
DEF 15
GHI 5
Total 60
This is what my code looks like right now. I defined two extra variables for my extra query that I want to echo out, but believe this is not the proper way to do it as I get an sqlsrv_fetch_array error.
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$query2 = "SELECT COUNT(*) AS Totaal FROM Employee_Table;";
$result1 = sqlsrv_query($conn, $query1);
$result2 = sqlsrv_query($conn, $query2);
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=sqlsrv_fetch_array($result1, $result2)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
echo "</td><td>";
echo $row["Total"];
echo "</td></tr>";
}
echo "</table>";
How can this be achieved? I'd appreciate any help
First correct
sqlsrv_fetch_array()
see here http://php.net/manual/de/function.sqlsrv-fetch-array.php
Use just a single query and use mysqli_num_rows() function to count
$count = mysqli_num_rows($result);
I think you can achieve this by using only first query
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$result1 = mysql_query($conn, $query1);
$total_employee = 0;
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=mysql_fetch_array($result1)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
$total_employee += $row["count"];
echo "</td><td>";
echo "</td></tr>";
}
echo "<tr><td>Total</td><td>$total_employee</td></tr>";
echo "</table>";
You can use only one query:
$result = sqlsrv_query($conn, "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC");
?>
<table id='total'>
<tr><th>Company</th><th>Amount of employees</th></tr>
<?php
$total = 0;
while ($row=sqlsrv_fetch_array($result)) {
$total += $row["count"];
?>
<tr><td><?= $row["Company"] ?></td><td><?= $row["count"] ?></td></tr>
<?php
}
?>
<tr><td></td><td><?= $total ?></td></tr>
</table>
<?php
Try the following code
$query1 = "SELECT Company,COUNT(*) as count FROM Employee_Table GROUP BY Company ORDER BY count DESC;";
$query2 = "SELECT COUNT(*) AS Totaal FROM Employee_Table;";
$result1 = sqlsrv_query($conn, $query1);
$result2 = sqlsrv_query($conn, $query2);
echo "<table id='total'>";
echo "<tr><th>Company</th><th>Amount of employees</th></tr>";
while ($row=sqlsrv_fetch_array($result1)) {
echo "<tr><td>";
echo $row["Company"];
echo "</td><td>";
echo $row["count"];
echo "</td><td>";
echo "</td></tr>";
}
$rows = sqlsrv_fetch_array($result1)
echo "<tr><td>";
echo $rows["Total"];
echo "</td></tr>";
echo "</table>";
I have 2 tables
1)students
students table looks like
stu_slno stu_gr stu_name
1 1 ABCDESDFDGFJ
2 3 KJJJHJILKJBJB
3 5 HAHAHAHAHKJHKJH
4 1 BBJHBAHJBHJBAJHK
2)groups
groups table looks like
sl_no pg_groups
1 01-M.A HISTORY
3 03-M.A SOCIOLOGY
5 04-M.A ECONOMICS
i have inserted data into students with groups serial numbers
when i am retrieving data from students i will get the groups serial number but what i want is to group names corresponding to the serial number
my code is of retrieving
<?PHP
$sql="SELECT * FROM students";
if ($us=$conn->query($sql)){;
if ($us->num_rows > 0) {
echo '<table border="2">';
echo "<tr>";
echo "<th>Slno</th>";
echo "<th>Name</th>";
echo "<th>Group</th>";
echo "</tr>";
while($row = $us->fetch_assoc()) {
echo "<tr>";
echo "<td>" .$i++. "</td>";
echo "<td>" .$row['stu_name']. "</td>";
echo "<td>" .$row['stu_gr']. "</td>";
echo "</table>";
}
}
}
?>
use join in query like this:
$sql="SELECT stu_slno, pg_groups, stu_name
FROM students s
INNER JOIN groups g ON g.pg_groups = s.stu_gr";
$sql = "SELECT s.*, g.pg_groups from students AS s LEFT JOIN groups as g ON g.sl_no = s.stu_gr";
TO display in the HTML table, use the below code
echo "<td>" .$row['pg_groups']. "</td>";
How to create a table with data retrieved from MySQL with PHP
-------------------------
| albumID | trackID |
-------------------------
| | 1990 |
- 1 -------------
| | 1991 |
-------------------------
| | 1992 |
- -------------
| 2 | 1993 |
- -------------
| | 1994 |
-------------------------
I can generate a table from the database, but the result will be only like each result in one row
$query = "SELECT albumID, trackID
FROM songs";
$result = mysqli_query($db, $query);
echo "<table>";
echo "<tr>\n";
echo "<th>albumID</th>\n";
echo "<th>trackID</th>\n";
echo "</tr>\n";
while ($data = mysqli_fetch_array($result)) {
echo "<tr>\n";
echo "<td>$data[0]</td>\n";
echo "<td>$data[1]</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
Any help given is really appreciated!
You can do this by using a variable to keep track of the current album and only output the album once as shown below.
$current_album = ''; # initialize tracking var
while ($data = mysqli_fetch_array($result)) {
echo "<tr>\n";
if ($current_album != $data[0]) {
echo "<td>$data[0]</td>\n";
$current_album = $data[0];
} else {
echo "<td> </td>\n"; # insert empty cell
}
echo "<td>$data[1]</td>\n";
echo "</tr>\n";
}
This will have the album ID appearing in the first row for the album. If you want it to be vertically centered you can use rowspan on the td element, but in order to know how many rows each album has you will either need to update your sql query to return that or you can process the results into a multidimensional array and then loop through that to generate the output.
Maybe not the most elegant solution, but should do the trick.
$query = "select albumID as album, group_concat(trackID) as tracks from songs group by albumID";
$result = mysqli_query($db, $query);
echo "<table>";
echo "<tr>\n";
echo "<th>albumID</th>\n";
echo "<th>trackID</th>\n";
echo "</tr>\n";
while ($data = mysqli_fetch_array($result)) {
$tracks = explode(",", $data[1]);
echo "<tr>\n";
echo "<td rowspan=\"".count($tracks)."\">$data[0]</td>\n";
foreach ($tracks as $key => $value) {
echo "<td>$value</td>\n"
}
echo "</tr>\n";
}
echo "</table>\n";
Edit your query to return the numbers of tracks of the album and use that number to create a rowspan only on the first track row. I wrote a little example but I havn't tried if it works.
$query = "SELECT s.albumID, s.trackID,
( SELECT COUNT(*)
FROM songs s2
WHERE s2.albumID = s.albumID )
FROM songs s";
$result = mysqli_query($db, $query);
echo "<table>";
echo "<tr>\n";
echo "<th>albumID</th>\n";
echo "<th>trackID</th>\n";
echo "</tr>\n";
$tmpAlbum = '';
while ($data = mysqli_fetch_array($result)) {
echo "<tr>\n";
echo "<td";
if($data[2] == 1){
echo "<td>$data[0]</td>\n";
}else{
if($data[0] != $tmpAlbum){
echo "<td rowspan=\"".$data[2]."\">$data[0]</td>\n";
}
}
$tmpAlbum = $data[0];
echo "<td>$data[1]</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
I want to show table like :
| | qty | S rank | A rank | B rank | C rank |
+--------------------------------------------------------+
| TODAY | 12 | 0 | 0 | 0 | 0 | //THIS CURRENT DATA
+--------------------------------------------------------+
|MONTHLY TOTAL| 200 | 1 | 0 | 0 | 0 | //THIS MONTHLY DATA
+--------------------------------------------------------+
The table above will show after echoing mysql result use PHP. How to combine tWo case become one table after echo? using double $sql or something else use PHP ?
So far, I'm success during echo current data, but for Monthly data i've become confuse how to deciding which way that should I try to do.
CURRENT DATA script:
..........
$sql = " SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C
FROM inspection_report WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A' AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' GROUP BY Range_sampling";
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
echo "<table border='1' width='500'>";
echo "<caption>INSPECTION REPORT</caption>";
echo "<thead><tr>";
echo "<td colspan='3'></td>";
echo "<th>n</th><th>S</th><th>A</th><th>B</th><th>C</th>";
echo "</tr></thead>";
echo "<tbody>";
while($row=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>JUDGE</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>TODAY</th><td>";
echo $row['n'];
echo "</td><td>";
echo $row['S'];
echo "</td><td>";
echo $row['A'];
echo "</td><td>";
echo $row['B'];
echo "</td><td>";
echo $row['C'];
echo "</td></tr>";
}
echo "</tbody></table>";
mysql_close($dbc);
?>
This query for count MONTHLY data :
SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C
FROM inspection_report WHERE Inspection_datetime <=
( SELECT DATE(MAX(Inspection_datetime)) FROM inspection_report
WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A'
AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' )
AND MONTH(Inspection_datetime)=MONTH(CURRENT_DATE) AND Line ='FA 01'
could I get the result like above table ?
From your suggested structure, it contain 3 rows and 6 fields. Now, let's get back to Basic HTML 101.
To create a table, you need:
<table></table>
To create row and columns, you need:
<tr><!-- start of row -->
<td><!-- start of field 1 -->
{table's field 1 content}
</td><!-- end of field 1 -->
<td><!-- start of field 2 -->
{table's field 2 content}
</td><!-- end of field 2 -->
</tr><!-- end of row -->
For more information about how to create HTML table.
Now, back to your case. You already create the first and second rows and by this, you should already understand how to do it.
So, when you need to append another table row, all you have to do is insert the table row code.
Just before:
echo "</tbody></table>";
You do the second (monthly) query and echo it's value, eg:
$sql = "{your sql query}";
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
while($row=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>JUDGE</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>MONTHLY TOTAL</th><td>";
echo $row['{field_name}'];
echo "</td><td>";
.
.
.
echo "</td></tr>";
}
echo "</tbody></table>";
Try it like below code :-
$sql = " SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C
FROM inspection_report WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A' AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' GROUP BY Range_sampling";
$result=mysql_query($sql) or die(_ERROR26.": ".mysql_error());
echo "<table border='1' width='500'>";
echo "<caption>INSPECTION REPORT</caption>";
echo "<thead><tr>";
echo "<td colspan='3'></td>";
echo "<th>n</th><th>S</th><th>A</th><th>B</th><th>C</th>";
echo "</tr></thead>";
echo "<tbody>";
while($row=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>JUDGE</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>TODAY</th><td>";
echo $row['n'];
echo "</td><td>";
echo $row['S'];
echo "</td><td>";
echo $row['A'];
echo "</td><td>";
echo $row['B'];
echo "</td><td>";
echo $row['C'];
echo "</td></tr>";
}
$sql2 = "SELECT COUNT(Serial_number) AS n, SUM(S) AS S,SUM(A) AS A, SUM(B) AS B, SUM(C) AS C FROM inspection_report WHERE Inspection_datetime <=
( SELECT DATE(MAX(Inspection_datetime)) FROM inspection_report
WHERE Model LIKE 'KD-R306TUND' AND Lot_no LIKE '066A'
AND Line LIKE 'FA 01' AND Range_sampling ='087X0001-087X0400' )
AND MONTH(Inspection_datetime)=MONTH(CURRENT_DATE) AND Line ='FA 01'";
$result_sql2 =mysql_query($sql2) or die(_ERROR26.": ".mysql_error());
while($rw=mysql_fetch_array($result)){
echo "<tr><th rowspan='2'>SECOND</td><td id='acc' bgcolor='grey'>ACCEPT</td><th>MONTHLY</th><td>";
echo $rw['n'];
echo "</td><td>";
echo $rw['S'];
echo "</td><td>";
echo $rw['A'];
echo "</td><td>";
echo $rw['B'];
echo "</td><td>";
echo $rw['C'];
echo "</td></tr>";
}
echo "</tbody></table>";
mysql_close($dbc);
?>