MySQL data from database align horizontally - php

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

Related

PHP: List multiple record grouped by date

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

Tabulating the Data MySQL PHP

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>';

Group rows from SQL database by date in tables

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?

Selecting the highest ID from a database table with PHP

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

PHP display associative array in HTML table

Here is my associative array:
$req_data1[]=array(
'depart1'=>$_REQUEST['to'],
'd_time1'=>$d_time5,
'stop'=>"",
'leave_stop'=>"",
'arrival1'=>$_REQUEST['from'],
'a_time1'=>$end_time5,
'price1'=>intval($final_price),
'air_line'=>"xxxxx");
Here is my sorting algorithm:
foreach ($req_data as $key => $row) {
$depart[$key] = $row['depart'];
$d_time[$key] = $row['d_time'];
$stop[$key] = $row['stop'];
$leave_stop[$key] = $row['leave_stop'];
$arrival[$key] = $row['arrival'];
$a_time[$key] = $row['a_time'];
$price[$key] = $row['price'];
}
array_multisort($price,SORT_ASC, $req_data);
I am displaying the data after sorting:
foreach($req_data as $key=>$row) {
echo "</br>";
foreach($row as $key2=>$row2){
echo $row2;
}
}
My problem now is that I want to put that array into an HTML table but dont know how. This is my code which I tried so far but its not working:
$cols = 5;
echo "<table border=\"5\" cellpadding=\"10\">";
for ($r=0; $r < count($row2); $r++) {
echo "<tr>";
for ($c=0; $c<$cols; $c++) {
?> <td> <?php $input[$r+$c] ?> </td> <?php
}
echo "</tr>";
$r += $c;
}
echo "</table>";
?>
Could any one tell me what is wrong with my code, or how I can display this sorted data into a table? Thanks.
Why not just modify the loop you already use to display the data?
echo "<table>";
foreach($req_data as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
$toOutput = '<table>';
$showHeader = true;
$memberData = $reportObj->getMemberData();
while($row = mysql_fetch_assoc($memberData))
{
$toOutput .= '<tr>';
//Outputs a header if nessicary
if($showHeader)
{
$keys = array_keys($row);
for($i=0;$i<count($keys);$i++)
{
$toOutput .= '<td>' . $keys[$i] . '</td>';
}
$toOutput .= '</tr><tr>';
$showHeader = false;
}
//Outputs the row
$values = array_values($row);
for($i=0;$i<count($values);$i++)
{
$toOutput .= '<td>' . $values[$i] . '</td>';
}
$toOutput .= '</tr>';
}
$toOutput .= '</table>';
echo 'Test page';
echo $toOutput;
Sorry for the necro, but I was actually looking to see if there was a build-in function for this as I was writing this.
function DumpTable($array_assoc) {
if (is_array($array_assoc)) {
echo '<table class="table">';
echo '<thead>';
echo '<tr>';
list($table_title) = $array_assoc;
foreach ($table_title as $key => &$value):
echo '<th>' . $key . '</th>';
endforeach;
echo '</tr>';
echo '</thead>';
foreach ($array_assoc as &$master):
echo '<tr>';
foreach ($master as &$slave):
echo '<td>' . $slave . '</td>';
endforeach;
echo '</tr>';
endforeach;
echo '</table>';
return;
}
}
echo "<table border=\"5\" cellpadding=\"10\">";
for ($r=0; $r < count($row2); $r++) {
echo "<tr>";
for ($c=0; $c<$cols; $c++) { ?>
<td> <?php $input[$r+$c] ?> </td>
<?php }
echo "</tr>";
$r += $c;
}
echo "</table>";?>
Try something like this
echo "<table>";
for($r=0;$r<count($row2);$r++){
echo "<tr>";
for($c=0;$c<$cols;$c++){
echo "<td>".[VARIABLE YOU WANT TO PRINT]."</td>";
}
echo "</tr>";
}
echo "</table>";
you can try the following:
echo "The associative array<br>";
$computer=array("brand"=>"hp", "price"=>"800", "cpu"=>"core i7");
$keys=array_keys($computer);
echo "<table><tr>";
foreach($keys as $row){
echo "<th style=\"border: solid 2px green\">".$row."</th>";
}echo "</tr><tr>";
foreach($computer as $col){
echo "<td style=\"border: solid 2px blue\">".$col."</td>";
}echo "</tr></table>";

Categories