UPDATE - Added query
I am developing a system which gathers results from games from a MySQL database. I'd like the results to be 'grouped' by the date (e.g. results from a certain date will be in one table).
I'm nearly there, but struggling to get the results from the same date to display as the next row of the table.
.
Here's the code as it stands:
$result = mysqli_query($con, "SELECT `MatchDate`, t1.`TeamName` AS HomeTeam, `MatchHomeScore`, `MatchAwayScore`, t2.`TeamName` AS AwayTeam `FROM `match` INNER JOIN `team` t1 ON `match`.`MatchHomeTeamID` = t1.`TeamID` INNER JOIN `team` t2 ON `match`.`MatchAwayTeamID` = t2.`TeamID` ORDER BY `MatchDate` ASC ");`
$currentDate = false;
while($row = mysqli_fetch_array($result))
{
echo '<div class="col-lg-4">';
echo '<div class="panel panel-default-fixtures">';
if ($row['MatchDate'] != $currentDate){
echo '<div class="panel-heading">';
echo '<h4 class="panel-title">';
echo $row['MatchDate'];
echo '</h4>';
echo '</div>';
$currentDate = $row['MatchDate'];
}
echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">';
echo '<thead>';
echo '<tr>';
echo '<th class="all">';
echo 'Home';
echo '</th>';
echo '<th colspan="2">';
echo 'Score';
echo '</th>';
echo '<th class="all">';
echo 'Away';
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>'.$row['HomeTeam'].'</td>';
echo '<td>'.$row['MatchHomeScore'].'</td>';
echo '<td>'.$row['MatchAwayScore'].'</td>';
echo '<td>'.$row['AwayTeam'].'</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
}
2nd update
Used the code Shamil Omarov provided and fixed the issue with a few changes. Here's the working code:
$result = mysqli_query($con, "
SELECT `MatchDate`, t1.`TeamName` AS HomeTeam, `MatchHomeScore`, `MatchAwayScore`, t2.`TeamName` AS AwayTeam
FROM `match`
INNER JOIN `team` t1 ON `match`.`MatchHomeTeamID` = t1.`TeamID`
INNER JOIN `team` t2 ON `match`.`MatchAwayTeamID` = t2.`TeamID`
-- WHERE YEAR(`MatchDate`) =2017
ORDER BY `MatchDate` ASC ");
$currentDate = false;
while($row = mysqli_fetch_array($result))
{
if ($row['MatchDate'] != $currentDate){
if ($currentDate != false){
echo '</tbody>';
echo '</table>';
// /.table-responsive
echo '</div>';
// /.panel
echo '</div>';
// /.col-lg-4
}
echo '<div class="col-lg-4">';
echo '<div class="panel panel-default-fixtures">';
echo '<div class="panel-heading">';
echo '<h4 class="panel-title">';
echo $row['MatchDate'];
echo '</h4>';
echo '</div>';
echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">';
echo '<thead>';
echo '<tr>';
echo '<th class="all">';
echo 'Home';
echo '</th>';
echo '<th colspan="2">';
echo 'Score';
echo '</th>';
echo '<th class="all">';
echo 'Away';
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$currentDate = $row['MatchDate'];
}
if ($row['MatchHomeScore'] != null && $row['MatchAwayScore'] != null){
echo '<tr class="success">';
} else {
echo '<tr>';
}
echo '<td>'.$row['HomeTeam'].'</td>';
echo '<td>'.$row['MatchHomeScore'].'</td>';
echo '<td>'.$row['MatchAwayScore'].'</td>';
echo '<td>'.$row['AwayTeam'].'</td>';
echo '</tr>';
}
This new code now outputs:
check this.
$currentDate = false;
while($row = mysqli_fetch_array($result))
{
echo '<div class="col-lg-4">';
echo '<div class="panel panel-default-fixtures">';
if ($row['MatchDate'] != $currentDate){
if($currentDate != false){
echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
}
echo '<div class="panel-heading">';
echo '<h4 class="panel-title">';
echo $row['MatchDate'];
echo '</h4>';
echo '</div>';
echo '<table width="100%" class="table table-striped table-bordered table-hover dataTables">';
echo '<thead>';
echo '<tr>';
echo '<th class="all">';
echo 'Home';
echo '</th>';
echo '<th colspan="2">';
echo 'Score';
echo '</th>';
echo '<th class="all">';
echo 'Away';
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$currentDate = $row['MatchDate'];
}
echo '<tr>';
echo '<td>'.$row['HomeTeam'].'</td>';
echo '<td>'.$row['MatchHomeScore'].'</td>';
echo '<td>'.$row['MatchAwayScore'].'</td>';
echo '<td>'.$row['AwayTeam'].'</td>';
echo '</tr>';
}
I think you need to change the query. Could you show it in order to find out what is needed to be changed?
Related
I'm trying to list multiple record from MySQL Database using PHP but when grouping it returns only one record from database, below is my PHP code I'm using.
<?php
include "connection.php";
$query = "select * from lectureupload GROUP BY submittedOn";
$result=mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$row['submittedOn'].'</h3>';
echo '</div>';
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
echo '</table>';
}
mysqli_close($conn);
?>
The current Result it gives me is somewhat like that,
My Database table is this.
Solution to this problem will be highly appreciated.
Regards.
You can't use GROUP BY clause to retrieve all records from that table. GROUP BY will always return one row per GROUP BY CONDITIONAL_COLUMN. I will suggest you below modification:
<?php
include "connection.php";
//ORDER BY submittedOn will make sure the records retrieved in ordered as per submittedOn.
$query = "select * from lectureupload ORDER BY submittedOn";
$result = mysqli_query($conn, $query);
$submitted_on_keys = array();
while ($row = mysqli_fetch_array($result)) {
if (!in_array($row['submittedOn'], $submitted_on_keys)) {
if (count($submitted_on_keys) > 0) {
//It means we have already created <table> which needs to be closed.
echo '</table>';
}
$submitted_on_keys[] = $row['submittedOn'];
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>' . $row['submittedOn'] . '</h3>';
echo '</div>';
}
echo '<tr >';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['semester'] . '</td>';
echo '<td>' . $row['teacherName'] . '</td>';
echo '<td>' . $row['lectureLink'] . '</td>';
echo '<td>' . $row['submittedOn'] . '</td>';
echo '</tr>';
}
if (count($submitted_on_keys) > 0) {
//There is last <table> which needs to be closed.
echo '</table>';
}
mysqli_close($conn);
?>
You should group it in your PHP code rather than your SQL.
Something like this:
$lectures = [];
$result = mysqli_query($conn, "select * from lectureupload");
while ($row = mysqli_fetch_array($result)) {
$lectures[$row['submittedOn']][] = $row;
}
foreach ($lectures as $submittedOn => $rows) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$submittedOn.'</h3>';
echo '</div>';
foreach ($rows as $row) {
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
}
echo '</table>';
}
Anyone can help me out in tabulating this data on PHP/HTML as I am only able to get to raw data which is not formulated. This is the code I am using:
// Price and Flight Select Statements
$pricesql = "select PriceID,RouteID,ClassID,Price from price;";
$printpricesql=mysqli_query($connect,$pricesql);
while($row=mysqli_fetch_array($printpricesql))
{
echo $row['PriceID'];
echo $row['RouteID'];
echo $row['ClassID'];
echo $row['Price'];
}
$flightsql = "select flightid,routeid,departuredate,arrivaldate from
flightschedule;";
$printflightsql=mysqli_query($connect,$flightsql);
while($row=mysqli_fetch_array($printflightsql))
{
echo $row['flightid'];
echo $row['routeid'];
echo $row['departuredate'];
echo $row['arrivaldate'];
}
// Price and Flight Select Statements
$pricesql = "select PriceID,RouteID,ClassID,Price from price";
$printpricesql=mysqli_query($connect,$pricesql);
echo '<table>';
// headers
echo '<tr>';
echo '<th>Price Id</th>';
echo '<th>Route Id</th>';
echo '<th>Class Id</th>';
echo '<th>Price</th>';
echo '</tr>';
while($row=mysqli_fetch_array($printpricesql)) {
echo '<tr>';
echo '<td>'.$row['PriceID'].'</td>';
echo '<td>'.$row['RouteID'].'</td>';
echo '<td>'.$row['ClassID'].'</td>';
echo '<td>'.$row['Price'].'</td>';
echo '</tr>';
}
echo '</table>';
$flightsql = "select flightid,routeid,departuredate,arrivaldate from flightschedule";
$printflightsql=mysqli_query($connect,$flightsql);
echo '<table>';
// headers
echo '<tr>';
echo '<th>Flight Id</th>';
echo '<th>Route Id</th>';
echo '<th>Departure Date</th>';
echo '<th>Arrival Date</th>';
echo '</tr>';
while($row=mysqli_fetch_array($printflightsql)) {
echo '<tr>';
echo '<td>'.$row['flightid'].'</td>';
echo '<td>'.$row['routeid'].'</td>';
echo '<td>'.$row['departuredate'].'</td>';
echo '<td>'.$row['arrivaldate'].'</td>';
echo '</tr>';
}
echo '</table>';
I'm creating a contact based system, i have a contact list and want it to go to a full contact page when i click on a button, but the variable is being detected as a function?
<div id="po2" style="Margin:10% ">
<h1>Contacts</h1>
<?php
$sql = "SELECT * FROM `contacts`";
$query = mysqli_query($conn, $sql);
echo '<table class="data-table">';
echo'<thead>';
echo'<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo'</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td>'.$row['Forename'].'</td>';
echo '<td>'.$row['Surname'].'</td>';
echo $var==$row['Forename']("<td><a href='View.php?ID= " . urlencode($var) ."'>
<button type='button'>link</button>
</a></td>");
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
</div>
You are using a comparison of $var and the $row. Try setting $var to the $row each loop iteration.
echo '<thead>';
echo '<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
$var = $row['Forename'];
echo '<tr>';
echo '<td>' . $var . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo "<td><a href='View.php?ID=" . urlencode($var) . "'><button type='button'>link</button></a></td>";
echo '</tr>';
}
echo '</tbody>';
I am trying to select one row from the database with the highest ID and echo it out to the page in a table.
I can run the query manually and it works correctly but when I try to do it dynamically via PHP; it does not work. What is wrong with my code?
<?php
$sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
echo '<div class="row" style="margin-top: 30px;">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 1) {
echo '<tr>';
echo '<th>Order ID</th>';
echo '<th>Status</th> ';
echo '<th>Order Total</th>';
echo '<th>Order Description</th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['order_id'] .'</td>';
echo '<td>'. $row['status'] .'</td> ';
echo '<td>'. $row['order_total'] .'</td>';
echo '<td>'. $row['order_description'] .'</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
You are using while loop for fetching data again where as there is no need to fetch data again as it is already been fetched using $row = mysqli_fetch_array($result, MYSQLI_ASSOC); at the beginning.
Try following
<?php
$sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);
echo '<div class="row" style="margin-top: 30px;">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if ($count >= 1)
{
echo '<tr>';
echo '<th>Order ID</th>';
echo '<th>Status</th> ';
echo '<th>Order Total</th>';
echo '<th>Order Description</th>';
echo '</tr>';
// Removed while loop as data is already fetched
// you just need to echo the values now
echo '<tr>';
echo '<td>' . $row['order_id'] . '</td>';
echo '<td>' . $row['status'] . '</td> ';
echo '<td>' . $row['order_total'] . '</td>';
echo '<td>' . $row['order_description'] . '</td>';
echo '</tr>';
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
while loop is not required here as only one row is being retried by using LIMIT 1. if more than one row is being retrieved, in that case while loop will do the trick for you.
Take a look on
$sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
and
if($count > 1) {
You always will be have only 1 row. Try to change conditions
You only show the table when MORE than 1 record is selected. Your query will return maximum one result, so the table will never show.
Use if($count >= 1) instead of if($count > 1).
Please note the 'Is greater OR Equal' notation: >=
update your code with this as there are two errors
1) count >= 1
2) while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) { //add MYSQLI_ASSOC
}
Your result set contains only 1 row .
So, mysqli_num_rows($result) will return 1 in count. So you should check
if($count==1) {
...
}
<?php
$sql_query = "SELECT * FROM single_user_orders ORDER BY order_id DESC LIMIT 1";
$result = mysqli_query($dbconfig, $sql_query);
$count = mysqli_num_rows($result);
echo '<div class="row" style="margin-top: 30px;">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count >= 1) {
echo '<tr>';
echo '<th>Order ID</th>';
echo '<th>Status</th> ';
echo '<th>Order Total</th>';
echo '<th>Order Description</th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['order_id'] .'</td>';
echo '<td>'. $row['status'] .'</td> ';
echo '<td>'. $row['order_total'] .'</td>';
echo '<td>'. $row['order_description'] .'</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
Try this for 1 or more orders
Im creating a basic website that will show 10 different tv programmes.
I have the 10 different programmes stored in the database. Im able to retriev the 10 programmes but they all appear in a column.
I was wondering if theres a way to have then appear 5 in a row?
I have tried basic CSS but i cant seem to get it working
Here is the code i have so far:
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
while($obj = $results->fetch_object())
{
echo '<br>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
}
}
?>
I was wondering if theres a way to achive that i want or will i have to try something else?
anything will help.
Thanks!
Try to put them in a table:
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
$i=0;
echo '<table><tr>';
while($obj = $results->fetch_object())
{
echo '<td>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
echo '</td>';
$i++;
if ($i == 5) {
echo '</tr><tr>';
}
}
echo '</tr></table>';
}
?>
You can start from:
$i=0;
echo '<br>';
while($obj = $results->fetch_object())
{
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
if (($i++) == 5) { echo '<br>'; $i=0; }
}
UPDATE CSS
.tvProgs {
float:left;
width:200px;
display:block;
}
This will put them in a table 5 in each row just like you asked for.
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
$i = 0;
echo '<table>';
while($obj = $results->fetch_object())
{
if ($i == 0) {
echo '<tr>';
}
echo '<td>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
echo '</tr>';
$i++;
if ($i == 5) {
echo '</tr>';
$i = 0;
}
}
if ($i != 0) {
echo '</tr>';
}
echo '</table>';
}
?>