I want to make 2 columns(openday, school_name) into 1 row and print it as a table.
I would like to print it out like a desired table.
Is it possible?
convert rows to columns php is it possible?
If possible, please explain in detail
my code is
<?php
$SQL= "SELECT Concat_ws('\n' openday,'', school_name) AS school_column, supply_name,score from result ";
$grouped = [];
$columns = [];
$resultObject = $result = mysqli_query($conn, $SQL);
foreach ($resultObject as $row) {
$grouped[$row['supply_name']][$row['school_column']] = $row['score'];
$columns[$row['school_column']] = $row['school_column'];
}
sort($columns);
$defaults = array_fill_keys($columns,'-');
array_unshift($columns, 'school_column');
?>
<body>
<table id="example" class="display" style="width:100%" align="center">
<thead>
<?php
While($result = mysqli_fetch_array($resultObject));
{
echo "<tr>
<td>supply_name</td>
<td> ".$row['school_column']." </td>
</tr>";
}
?>
</thead>
<tbody>
<?php
While($result = mysqli_fetch_array($resultObject));
{
echo "<tr>
<td> ".$row['supply_name']." </td>
<td> ".$row['score']." </td>
</tr>";
}
?>
</tbody>
</table>
</body>
<?php
$SQL= "SELECT Concat_ws('\n' openday,'', school_name) AS school_column, supply_name,score from result ";
$grouped = [];
$columns = [];
$resultObject = $result = mysqli_query($conn, $SQL);
foreach ($resultObject as $row) {
$grouped[$row['supply_name']][$row['school_column']] = $row['score'];
$columns[$row['school_column']] = $row['school_column'];
}
sort($columns);
$defaults = array_fill_keys($columns,'-');
array_unshift($columns, 'school_column');
?>
<body>
<table id="example" class="display" style="width:100%" align="center">
<thead>
<?php
while($result = mysqli_fetch_array($resultObject))
{
echo "<tr>
<td>openday</td>
<td> school_name </td>
</tr>";
}
?>
</thead>
<tbody>
<?php
while($result = mysqli_fetch_array($resultObject))
{
echo "<tr>
<td colspan='2'> ".$row['supply_name']." </td>
<td> ".$row['score']." </td>
</tr>";
}
?>
</tbody>
</table>
</body>
Related
I'm using Bootstrap DataTables to display data from database but I have an issue with missing sorting and searching. Values from database are displaying correctly so I'm assuming I have syntax error(missing ' or sth), it allows me to display data but features of DataTables aren't working.
Here is my code:
<div class="table-responsive">
<table id="orders_data" class="table table-striped table-bordered">
<thead>
<tr>
<td> Imię </td>
<td> Nazwisko </td>
<td> Książki </td>
</tr>
</thead>
<?php
while($row = mysqli_fetch_array($result))
{
$sql2 = "SELECT * FROM books_has_authors JOIN books on books_has_authors.books_book_id = books.book_id
WHERE authors_id_author = '".$row["id_author"]."'";
$result2 = mysqli_query($conn, $sql2);
echo '
<tr>
<td>'.$row['fname'].'</td>
<td>'.$row['lname'].'</td>
<td>';
while($row2 = mysqli_fetch_array($result2)){
echo '<p>'.$row2['name'].'</p>';
}
echo '</td>';'
</tr>
';
}
?>
</table>
</div>
I am trying to display data from from a database in a table, with one column for the names and another for the values, instead of the rudimentary layout it currently has:
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "{$row['date']} <br> ".
"--------------------------------<br>".
"Temperature :{$row['temperature']} <br> ".
"Luminosite : {$row['luminosite']} <br> ".
"Humidite : {$row['humidite']} <br> ".
"--------------------------------<br>";
}
I am very much a novice in all things php so any suggestions are welcome.
Try Using below code
echo "<table>
<tr>
<th>Date</th>
<th>Name</th>
<th>luminosite</th>
<th>humidite</th>
</tr>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<tr>
<td>".$row['date']."</td>
<td>".$row['temperature']."</td>
<td>".$row['luminosite']."</td>
<td>".$row['humidite']."</td>
</tr>";
}
echo "</table>";
Try to use this code
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div>
<div class="row">
<div class="table-responsive">
<table class="table table-striped table-hover" id="my_table">
<thead>
<tr>
<th>Temperature</th>
<th>Luminosite</th>
<th>Humidite</th>
</tr>
</thead>
<tbody>
<?php
$query = "select * from table";
$conn = mysqli_connect("localhost", "root", "password", "ecom");
$data = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($data))
{
$temperature = $row['temperature'];
$luminosite = $row['luminosite'];
$humidite = $row['humidite'];
echo "<tr><td>$temperature</td><td>$luminosite</td><td>$humidite</td></tr>";
}
; ?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
Try this code
<table>
<tr>
<td>Date</td><td>Temperature</td><td>Luminosite</td><td>Humidite</td>
</tr>
<?php
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<tr>
<td>$row['date']</td>
<td>$row['temperature']</td>
<td>$row['luminosite']</td>
<td>$row['humidite']</td>
</tr>";
}
?>
</table>
Do not use MySQL.
<table>
<tr>
<?php
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
?>
<td>
<?php echo "{$row['date']};?>
<td>
<td>
<?php echo "{$row['temperature']};?>
<td>
<!-- other fiels -->
<?php
}
?>
</tr>
</table>
I was trying to echo a variable before it was even defined so literally i'm getting this error "UNDEFINED VARIABLE: tpp"
<tr>
<td colspan='6'>
<h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4>
</td>
</tr>
When i set that line down along with echo "" , it works. But i want it on the place where i set it [Top]
Here is the code which i need help with
<table class="table table-bordered">
<thead>
<tr>
<td colspan='6'>
<h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4>
</td>
</tr>
<td>Number</td>
<td>Username</td>
<td>Last Login</td>
</thead>
<?php
$query = $koneksi->prepare("SELECT `user`, `TP`, `LastOnlineDate` FROM `playerdata` WHERE `banned`=0 AND `TP`=1");
$query->execute();
if($query->rowCount() == 0)
{
echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
}
$tpp = 0;
while($data = $query->fetch())
{
$tpp++;
echo "<tr><td>".$tpp."</td>";
echo "<td>".$data['user']."</td>";
echo "<td>".$data['LastOnlineDate']."</td></tr>";
}
?>
</table>
Any help will be so appreciated
thanks in advance.
$tpp is undefined because its being called before its been created, Try this out instead.
<?php
$tpp = 0;
$content = "";
$query = $koneksi->prepare("SELECT `user`, `TP`, `LastOnlineDate` FROM `playerdata` WHERE `banned`=0 AND `TP`=1");
$query->execute();
while($data = $query->fetch())
{
$tpp++;
$content .= "<tr><td>".$tpp."</td>";
$content .= "<td>".$data['user']."</td>";
$content .= "<td>".$data['LastOnlineDate']."</td></tr>";
}
?>
<table class="table table-bordered">
<thead>
<tr>
<td colspan='6'><h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4></td>
</tr>
<td>Number</td>
<td>Username</td>
<td>Last Login</td>
</thead>
<?php
if($query->rowCount() == 0)
{
echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
}
echo $content;
?>
</table>
I've decided to run the loop at the top, placing all the data within $content and then echo $content where you wish to display the content. This allows you to still have your counter run and be displayed at the top of the table.
I have a 2 tables like this when I search the member name.
link :
I wanted the search result to be shown only after search.
my code:
<div id = "subtitle">
View Members
</div>
<div id = "searchbox">
<form method="post">
<center><input type="text" maxlength="100" required placeholder="Enter Full Name" name ="search" autocomplete="off" value="">
<input type="submit" name="btn" value="SEARCH NOW!"></p></center>
</form>
</div>
<?php
if(isset($_POST["btn"]))
{
$search = $_POST["search"];
$sql = "select * from member where Member_Name like '$search%' ";
$result = mysqli_query($conn,$sql);
$rowcount = mysqli_num_rows($result);
if($rowcount==0)
echo "Sorry ,no records found!";
else
{
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result)) //display
{
?> <tr>
<td></td>
<td><?php echo $row["Member_ID"]?></td>
<td><?php echo $row["Member_Name"]?></td>
<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>
</tr>
</table>
</center>
<?php
}
}
}
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
$sql = "select * from legoclub_guesthouse.member";
$result = mysqli_query($conn,$sql);
$rowcount= mysqli_num_rows($result);
while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td></td>";
echo "<td>$row[Member_ID]</td>";
echo "<td>$row[Member_Name]</td>";
echo "<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>";
echo "</tr>";
}
?>
</table><center>
</div>
Please include the php file for me too!
To do it in easiest way just try to use this code:
<div id = "subtitle">
View Members
</div>
<div id = "searchbox">
<form method="post">
<center><input type="text" maxlength="100" required placeholder="Enter Full Name" name ="search" autocomplete="off" value="">
<input type="submit" name="btn" value="SEARCH NOW!"></p></center>
</form>
</div>
<?php
if(isset($_POST["btn"]))
{
$search = $_POST["search"];
$sql = "select * from member where Member_Name like '$search%' ";
$result = mysqli_query($conn,$sql);
$rowcount = mysqli_num_rows($result);
if($rowcount==0)
echo "Sorry ,no records found!";
else
{
?>
<center><table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Member ID</th>
<th>Member Name</th>
<th>Actions</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result)) //display
{
?> <tr>
<td></td>
<td><?php echo $row["Member_ID"]?></td>
<td><?php echo $row["Member_Name"]?></td>
<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>
</tr>
<?php
}
$sql = "select * from legoclub_guesthouse.member";
$result = mysqli_query($conn,$sql);
$rowcount= mysqli_num_rows($result);
while($row=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td></td>";
echo "<td>$row[Member_ID]</td>";
echo "<td>$row[Member_Name]</td>";
echo "<td><img src=../Images/ViewFile.png height=37px title=View>
<img src=../Images/edit.png height=37px title=Edit>
</td>";
echo "</tr>";
}
?>
</table>
</center>
<?php
}
}
?>
</div>
But I would recommend you to use SQL UNION Operator.
It will make your life easy for these type of problems
I am trying to display my table from my MySQL database. It doesn't seem to work, I guess it's most likely something simple. If possible I would like to have the table look like just a normal table with no frills; just a border and to be able to fit in a normal sized page. This is what I have so far:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
echo <tr>
<td>{$row\['id'\]}</td>
<td>{$row\['fname'\]}</td>
<td>{$row\['lname'\]}</td>
<td>{$row\['email'\]}</td>
<td>{$row\['dob'\]}</td>
<td>{$row\['age'\]}</td>
<td>{$row\['city'\]}</td>
<td>{$row\['state'\]}</td>
<td>{$row\['zip'\]}</td>
<td>{$row\['offence'\]}</td>
<td>{$row\['notes'\]}</td>
</tr>\n;
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>
Give this a go:
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?php
require "connect.php";
mysql_select_db("bank");
$result = mysql_query("SELECT * FROM profiles");
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<tr>';
foreach($row as $column) {
echo '<td>', $column, '</td>';
}
echo '</tr>';
}
?>
</table>
WHILE($row = mysql_fetch_array($result))
should be either
WHILE($row = mysql_fetch_array($result, MYSQL_ASSOC))
OR
WHILE($row = mysql_fetch_assoc($result))
Also the mysql_* functions have been deprecated and relying on them is HIGHLY discouraged.
Kindly check the below discussions :-
Deprecated MySql Functions
How to successfully rewrite old mysql-php code with deprecated mysql_* functions?
http://php.net/manual/en/migration55.deprecated.php
Use either MySQLi or PDO.
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
WHILE($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>'; // change here
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>'; // change here
}
mysql_close();
?>
</table></center>
Try this code:
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
$dbconnect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$select=mysql_select_db("bank",$dbconnect) or die("Could not select bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th></tr>
<?php
while($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>';
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>';
}
?>
</table></center>
<?php
mysql_close();
?>
Corrected Code:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?php
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['state'];?></td>
<td><?php echo $row['zip'];?></td>
<td><?php echo $row['offence'];?></td>
<td><?php echo $row['notes'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>