Display Data in two column in Datatable - php

I have managed to display data in one column table, but would like to have two column instead. Is there a way of doing it? Here's the code I currently have. Although, it works, it prints in one long column and would like to break it into two columns.
As you can tell, I'm using jQuery Datatable.
<?php
include('config.php');
mysql_connect($host, $username, $password) or die(mysql_error()) ;
mysql_select_db('people') or die(mysql_error()) ;
$data = mysql_query("SELECT * FROM names ORDER BY RAND() LIMIT 20") or die(mysql_error());
?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="datatables/dt/media/js/jquery.dataTables.js">
</script>
<style type="text/css">
/* #import "datatables/dt/media/css/demo_table.css";
.result_container{
width: 553;
} */
</style>
<script>
$(document).ready(function(){
$('#the_table').dataTable();
});
</script>
</head>
<body>
<?php
echo "<table id=\"the_table\">
<thead>
<tr>
<th>Latest names</th>
</tr>
</thead>
<tbody> ";
while($info = mysql_fetch_array( $data )){
echo"<tr> <td>" . $info['name'] . "</td>
</tr>";
}
echo" </tbody> ";
echo "</table> ";
?>
</body>
</html>
Any help will be appreciated very much.

echo "<table id=\"the_table\">
<thead>
<tr>
<th>Latest names</th>
</tr>
</thead>
<tbody>
<tr>";
while($info = mysql_fetch_array( $data )){
// if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row:
if( ($c % 2) == 0 && $c != 0){
echo "</tr><tr>";
}
echo "<td>" . $info['name'] . "</td>";
$c++;
} // while..
// in case you need to fill a last empty cell:
if ( ( $i % 2 ) != 0 ){
// str_repeat() will be handy when you want more than 2 columns
echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
}
echo "</tr>
</tbody>
</table>";
?>

Just add another cell in HTML?
<?php
echo "<table id=\"the_table\">
<thead>
<tr>
<th>Latest names</th>
<th>ANOTHER_FIELD</th>
</tr>
</thead>
<tbody> ";
while($info = mysql_fetch_array( $data )){
echo"<tr> <td>" . $info['name'] . "</td><td>".$info['ANOTHER_FIELD']."
</tr>";
}
echo" </tbody> ";
echo "</table> ";
?>

Related

how to connect php echo with html table

I want to show mysql data in a html table... php echo shows me the content, but how can I transfer this into the html-table ? I've already added some dummy data manually into the html-table ..
<?php
$sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
}
} else {
echo "00 results";
}
$conn->close();
?>
<h1>Bestellungen</h1>
<div class="countdown" data-role="countdown" data-days="2"></div>
<div class="container" style="padding: 25px 0px;">
<table class="table striped hovered border" data-role="datatable" data-searching="true">
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<tr><td>2</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Filet</td><td>50</td></tr>
<tr><td>3</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Frisch</td><td>50</td></tr>
<tr><td>4</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Lebend</td><td>50</td></tr>
</tbody>
</table>
</div>
You should iterate inside the table and add the html come in echo
<div class="container" style="padding: 25px 0px;">
<table class="table striped hovered border" data-role="datatable" data-searching="true">
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["id"]. "</td>
<td>". $row["Datum"]. "</td>
<td>". $row["Kunde"] . "</td>
<td>". $row["Produkt"] . "</td>
<td>". $row["Produktversion"] . "</td>
<td>". $row["Menge"] ."</td>
</tr>";
}
?>
</tbody>
</table>
</div>
Remeber you have to loop each to create rows for the table.
$sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table>
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
//echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
?>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["Datum"] ?></td>
<td><?php echo $row["Kunde"]; ?></td>
<td>---</td>
<td>---</td>
<td>---</td>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo "00 results";
}
$conn->close();
You need to create each and every row inside the while loop.

Query data from multiple database tables into form table (MYSQL,PHP)

As I'm new to PHP, I want to know that how to put data from different database tables into one table form on the page.
My codes so far as below,
<?php
include('DBconnect.php');
mysql_query("USE onlinerecruitment");
$username =$_SESSION['user'];
$result = mysql_query("SELECT * FROM application_data_file");
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$pos_id = $rows['Position_ID'];
$resultt = mysql_query("SELECT * FROM position WHERE Position_ID = '".$pos_id."' ");
$resulttt = mysql_query("SELECT * FROM resume_data_file WHERE App_Email = '".$pos_id."' ");
?>
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
I will focus the part here.
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
But if there is any problem in the section that I didn't focused, I still appreciate your solution.
Thank you in advance.
To do this you would need to use a JOIN in the sql statement.
mysql_query("SELECT resume_data_file.App_Email, position.Position_ID FROM position INNER JOIN resume_data_file ON position.Position_ID = position.Position_ID WHERE position.Position_ID = '".$pos_id."' ");
http://www.w3schools.com/sql/sql_join.asp

How to create a table in HTML using PHP

I have 5 pictures stored in a folder and their links stored on the database.
I want to put them in a table of three columns on each row.
<body>
<center>
<table border='1'>
<?php
$host="";
$username="";
$password="";
$db_name="fruits_db";
$tbl_name="fruits_tbl";
$connection=mysqli_connect("$host","$username","$password","$db_name");
if (mysqli_connect_errno())
{
echo "The application has failed to connect to the mysql database server: " .mysqli_connect_error();
}
$result = mysqli_query($connection, "SELECT * FROM fruits_tbl")or die("Error: " . mysqli_error($connection));
$num_rows=mysqli_num_rows($result);
$rows = $num_rows/3;
for($i=1; $i<=$rows ; $i++)
{
echo "<tr>";
for($j=1; $j<=3; $j++)
{
while($row = mysqli_fetch_array($result))
{
echo
("<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
mysqli_close($connection);
?>
</table>
</center>
</body>
</html>
The above code I created, contains two FOR loops. The script should count the number of rows in the table, and then divide by 3(the number of columns on each row in the HTML table).
I wonder where I'm going wrong wit this code.
With your while($row = mysqli_fetch_array($result)){} inside your 1st for loop it will run through all your rows, before the outside loop runs 2nd/3rd time.
Here is another way to do it -
$counter = 1;
// start 1st row
echo "<tr>";
while($row = mysqli_fetch_array($result)){
// if the 4th cell, end last row, and start new row
if ($counter%3==1){
echo "</tr><tr>";
}
echo
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>";
// increase the counter
$counter++;
}
// close the last row
echo "</tr>";
You're looping through all the results in the first table cell.
Try something like this instead:
for($i=1; $i<=$rows ; $i++) {
echo "<tr>";
for($j=1; $j<=3; $j++) {
$row = mysqli_fetch_array($result);
if ($row) {
echo(
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
If you just want to format the display with a new row after every 3 records, you could use the modulus operator:
$cntr = 0;
echo '<tr>';
while($row = mysqli_fetch_array($result)) {
$cntr++;
echo '
<td width="180px" height="200px">
<div class="fruit_image">
<img src="'.$row['fruit_image'].'" />
</div>
<div class="fruit_title">'.$row['fruit_name'].'</div>
</td>';
if ($cntr % 3 == 0 && $cntr != $num_rows)
echo '</tr><tr>';
}
echo '</tr>';
Keep in mind however that all the solutions presented so far may leave you with a last row with one or two td elements. You can fill this if you desire with empty <td> </td> columns.
print "<center><table border=1>
<tr>
<td>id</td>
<td>name</td>
<td>company</td>
<td>branch</td>
<td>job title</td>
<td>contact</td>
<td>email</td>
<td>mgs</td>
</tr>";
while($row=mysql_fetch_array($query))
{
print "<tr>";
for ($i=0;$i<=(count($row)/2);$i++)
{
print "<td>$row[$i]</td>";
} print"</tr>";
}
}
else{echo " <p>No Records Found</p>";}
<?php
function studentTable($name,$grade){
echo "<tr> <td>$name</td><td>$grade</td></tr>";
}
?>
<table style="width:100%" border="solid">
<tr>
<th>Name</th>
<th>Grade</th>
</tr>
<?php studentTable("Sarah", 90) ?>

Ajax responsetext as a table output?

I'm not really familiar with Ajax Response - I have edited the PHP Ajax Search code from W3schools.com as follows :
<?php
require_once('connect_db.php');
$query = "select item_no from items";
$result = mysql_query($query);
$a = array();
while ($row = mysql_fetch_assoc($result)){
$a[] = $row['item_no'];
}
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="No Suggestion";
}
else
{
$response=$hint;
}
//output the response
echo "<table border=1><tr><td>".$response."</td></tr></table>";
?>
The output of the above code works perfectly, but they are all listed like this (2L500BU , 2L500GO , 2L500NA , 2L500RD , 2L802CA , 2L802WH , 2L803GR , 2L804BE , 2L804BK , 2L804CO , 2L805BU , 2L806BE , 2L806GR ) - Those numbers are Item No in mysql table called items.
Now :
1) I want to output the response into table with <tr> for each like this
2l500BU
2L500GO
.
.
.
.
etc.
2) Do you think its possible to output all table records from Mysql based on the hint entered as follows :
$sql="SELECT * FROM items WHERE item_no = '".**$hint**."'";
$result = mysql_query($sql);
echo "<table align='center' cellpadding='3' cellspacing='3' width='800px' border='1' font style='font-family:arial;'>";
echo "
<tr align=center>
<th style=font-size:18px; bgcolor=#20c500>Item Number</th>
<th style=font-size:18px; bgcolor=#20c500>QTY</th>
<th style=font-size:18px; bgcolor=#20c500>Actual Price</th>
<th style=font-size:18px; bgcolor=#20c500>Selling Price</th>
<th style=font-size:18px; bgcolor=#20c500>Difference</th>
<th style=font-size:18px; bgcolor=#20c500>Date</th>
</tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr align=center bgcolor=#e3e3e3>";
echo "<td style='font-size:18px; font-weight:bold;'>" . strtoupper($row['item_no']) . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['qty'] . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['actual_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['discount_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['difference_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . date("d-m-Y",strtotime($row['date'])) . "</td>";
echo "</tr>";
}
echo "<table>";
If you're wanting to fetch items from a database and display a row for each of them, this is how you'd do it with jQuery.
Your PHP script:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$sql = "SELECT item_no FROM items";
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
$rows[] = $row['item_no'];
}
header('Content-Type: application/json');
echo json_encode($rows);
?>
And your HTML with the table:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th scope="col">Item No.</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
<script src="js/lib/jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON('yourscript.php', function(items) {
$.each(items, function(i, item) {
$('tbody').append('<tr><td>' + item + '</td></tr>);
});
});
});
</script>
I've also used MySQLi (MySQL improved) rather than the standard mysql_ functions, as the mysql_ library is deprecated and you should be using either MySQLi or PDO now.

Display MySQL Results into HTML Table with URL

The code below isn't working as intended. I was wanting 3 columns with the first column being a site name, the second column being the category, and the third being the URL for RSS (haven't gotten to that part yet). The data is in the database but when I view the result of the statements below, the site name results link to the current web page I am on. I would greatly appreciate any assistance with getting each column of data to display on the page correctly. Thank you
<?php
$query="SELECT * FROM SOMETABLE";
$result=mysql_query($query);
$num = mysql_numrows($result);
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
$i=0;
while ($i < $num) {
$siteName =mysql_result($result,$i,"siteName");
$category =mysql_result($result,$i,"category");
$category =mysql_result($result,$i,"url");
$rss =mysql_result($result,$i,"rss");
echo "
<tr>
<td><a href='$url'>$siteName</a></td>
<td>$category</td>
<td>$rss</td>
</tr>";
$i++;
}
?>
echo "<table>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>$line ["siteName"]</td>";
echo "<td>$line ["category"]</td>";
echo "<td>$line ["url"]</td>";
echo "<td>$line ["rss"]</td>";
echo "</tr>";
}
echo "</table>";
skip $num, mysql_resuls ( the way how you do it ) open row TR in headers ( I skipped it ) and close table
echo "
<table border='1'>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>";
Should be:
echo "
<table border='1'>
<tr>
<th>Site Name:</th>
<th>Category:</th>
<th>RSS:</th>
</tr>";
To start with.
Or just use some tool like SDTable.com or jqGrid and let them do all job for you =)

Categories