I am generating a Dynamic table based on user's search result with those searched columns only.
The table is generated in AJAX page and coming back as response in another page.The pagination alone is not working.
please help.
My Code:
<?php
require_once('../Connections/finalkms.php');
$data = json_decode(stripslashes($_POST['data']), true);
$qry = " SELECT AssetId,";
$qry .= $data;
$qry .= " from Completedetails";
mysql_select_db($database_finalkms, $finalkms);
$query_getcolumns = $qry;
$getcolumns = mysql_query($query_getcolumns, $finalkms) or die(mysql_error());
$row_getcolumns = mysql_fetch_assoc($getcolumns);
$totalRows_getcolumns = mysql_num_rows($getcolumns);
if (($getcolumns)||(mysql_errno == 0))
{
echo "<table width='50%' class='table table-striped table-bordered table-hover' align='center' id='sample_2'>
<thead><tr id='vstr'>";
if (mysql_num_rows($getcolumns)>0)
{
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
while ($rows = mysql_fetch_array($getcolumns,MYSQL_ASSOC))
{
echo "<tbody><tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Here's My Java script code:
<script>
$("#sample_2").dataTable();
</script>
This is the solution:
<?php
require_once('../Connections/finalkms.php');
$data = json_decode(stripslashes($_POST['data']), true);
$qry = " SELECT AssetId,";
$qry .= $data;
$qry .= " from Completedetails";
mysql_select_db($database_finalkms, $finalkms);
$query_getcolumns = $qry;
$getcolumns = mysql_query($query_getcolumns, $finalkms) or die(mysql_error());
$row_getcolumns = mysql_fetch_assoc($getcolumns);
$totalRows_getcolumns = mysql_num_rows($getcolumns);
if (($getcolumns)||(mysql_errno == 0))
{
echo "<table width='50%' class='table table-striped table-bordered table-hover' align='center' id='sample_2'>
<thead><tr id='vstr'>";
if (mysql_num_rows($getcolumns)>0)
{
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
while ($rows = mysql_fetch_array($getcolumns,MYSQL_ASSOC))
{
echo "<tr>"; //removed body tag here
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr></tr>";
}
echo "</table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Here's My Java script code:
<script>
$("#sample_2").dataTable();
</script>
Related
I think there is a simple way to align these table cells with the header from another table.
I tried to set the width of every table cell to 6%, this this doesn't align them completely.
This is my table:
https://imgur.com/a/sKUcY
This is how I create the table:
<table class="table table-striped table-bordered turnover_list">
<thead>
<tr class="sorting_1">
<th>Company</th>
<th>Year</th>
<?php
if($_POST['selectall'] == 0) {
$allCompanies = array();
$allCompanies[0] = getCompanyName($_POST['company']);
$allCompanies[1] = getCompanyName($_POST['compare']);
}
if($_POST['frommonth'] != "" && $_POST['tomonth'] != "" && $_POST['fromyear'] != "" && $_POST['toyear'] != "") {
$fromMonth = $_POST['frommonth'];
$toMonth = $_POST['tomonth'];
$fromYear = $_POST['fromyear'];
$toYear = $_POST['toyear'];
} else {
$fromMonth = 0;
$toMonth = 11;
$fromYear = date('Y');
$toYear = date('Y');
}
for($i=$fromMonth; $i<=$toMonth; $i++) {
echo "<th>" . $months[$i] . "</th>";
array_push($selectedMonths, $months[$i]);
}
for($i=$fromYear; $i<=$toYear; $i++) {
array_push($selectedYears, $i);
}
?>
<th>Total</th>
</tr>
</thead>
<?php
foreach($selectedYears as $year) {
$turnoverTotal = 0;
echo "<table class='table table-striped table-bordered turnover_list'>";
echo "<tbody>";
$monthTotal = array("January"=>"0", "February"=>"0", "March"=>"0", "April"=>"0", "May"=>"0", "June"=>"0", "July"=>"0", "August"=>"0", "September"=>"0", "October"=>"0", "November"=>"0", "December"=>"0");
foreach ($allCompanies as $companyName) {
$companyTurnoverTotal = 0;
echo "<tr>";
echo "<td style=''>" . $companyName . "</td>";
echo "<td style=''>" . $year . "</td>";
foreach ($selectedMonths as $month) {
$sql = "SELECT SUM(mob + www) AS 'turnover' FROM turnover WHERE year = '" . $year . "' AND companyID = '" . getCompanyID($companyName) . "' AND month = '" . $month . "'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$companyTurnoverMonth = $row['turnover'];
$monthTotal[$month] += $companyTurnoverMonth;
$companyTurnoverTotal += $companyTurnoverMonth;
echo "<td style=''>" . make_format($companyTurnoverMonth) . "</td>";
}
$turnoverTotal += $companyTurnoverTotal;
echo "<td style=' font-weight: bold; background-color:#d3d3d3;'>" . make_format($companyTurnoverTotal) . "</td>";
echo "</tr>";
}
echo "<tr style='font-weight: bold;'>";
echo "<td style='background-color:#d3d3d3;'>Total</td>";
echo "<td style='background-color:#d3d3d3;'>" . $year . "</td>";
foreach($selectedMonths as $month) {
echo "<td style='background-color:#d3d3d3;'>" . make_format($monthTotal[$month]) . "</td>";
}
echo "<td style='background-color:#d3d3d3;'>" . make_format($turnoverTotal) . "</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
?>
</table>
I created a separate table header, because the user is able to compare companies.
Is there a way to create a table header with multiple other tables (without header) underneath it and have it all aligned with the table header?
This would be my desired result:
https://imgur.com/ZFzZluK
I have som trouble finding out what i am doing wrong.
I am using Bootstrap Table in a foreach loop
Foreach($fetchSite as $rows)
{
$sname = $rows['name'];
echo "<div class='well'>"
. "<h4>$sname</h4>";
foreach ($fetchDep as $rowd)
{
$depn = $rowd['dep'];
echo "<h5>$depn:</h5>";
foreach($fetchUserdata as $row)
{
$uname = $row['username'];
$team = $row['team'];
$stime = $row['sttime'];
$depna = $row['dep'];
$siten = $row['name'];
if($sname == $siten)
{
if($depn == $depna)
{
echo "<div class='row'>"
. "<div class='table-responsive'>"
. "<table class = 'table table-striped' style='width: 500px;'>"
. "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>";
echo "<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>";
echo "<tbody>"
. "</tbody>"
. "</table>"
. "</div>"
. "</div>";
}
}
}//end Foreach FetchUSerdata
}// end Foreach Depfetch
echo "</div>";
} // End Foreach FetchSite
The problem is that there are to many
Bruger:, Team: and startet
i only want 1 line of Bruger, Team: and startet and then the users listed below.
I know where the problem is:
if($depn == $depna)
{
echo "<div class='row'>"
. "<div class='table-responsive'>"
. "<table class = 'table table-striped' style='width: 500px;'>"
. "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>";
echo "<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>";
echo "<tbody>"
. "</tbody>"
. "</table>"
. "</div>"
. "</div>";
}
But i dont no how to solve it.
Sorry for my bad english and stupid question.
Merry Christmas
The only thing you would want to leave inside the foreach is the <tr> and <td> sections. Leave the <table..> and <th> (table headers outside) the foreach that way it will generate a table.
Example
echo "<div class='row'>"
. "<div class='table-responsive'>"
. "<table class = 'table table-striped' style='width: 500px;'>"
. "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>";
foreach($fetchUserdata as $row)
{
$uname = $row['username'];
$team = $row['team'];
$stime = $row['sttime'];
$depna = $row['dep'];
$siten = $row['name'];
if($sname == $siten)
{
if($depn == $depna)
{
echo "<tbody>"
."<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>";
. "</tbody>"
}
}
}
echo "</table>"
. "</div>"
. "</div>";
//end Foreach FetchUSerdata
Foreach($fetchSite as $rows)
{
$sname = $rows['name'];
echo "<div class='well'>"
. "<h4>$sname</h4>";
foreach ($fetchDep as $rowd)
{
$depn = $rowd['dep'];
echo "<h5>$depn:</h5>";
echo "<div class='row'>"
. "<div class='table-responsive'>"
. "<table class = 'table table-striped' style='width: 500px;'>"
. "<thead><tr><th>Bruger:</th><th>Team:</th><th>startet:</th></tr></thead>";
echo "<tbody>";
foreach($fetchUserdata as $row)
{
$uname = $row['username'];
$team = $row['team'];
$stime = $row['sttime'];
$depna = $row['dep'];
$siten = $row['name'];
if($sname == $siten)
{
if($depn == $depna)
{
echo "<tr><td>".$uname."</td><td>".$team."</td><td>".$stime."</td></tr>";
}
}
}//end Foreach FetchUSerdata
echo "</tbody>"
. "</table>"
. "</div>"
. "</div>";
}// end Foreach Depfetch
echo "</div>";
} // End Foreach FetchSite
It's really difficult to understand what you're asking, but I think I got the gist. The following code example takes an array
$fetchSite = [
[
'something' => 'a',
'something_else' => 'b'
],
...
]
and turns it into a table
something | something_else
----------+---------------
a | b
code:
<table>
<thead>
<tr>
<?php foreach(array_keys($fetchSite[0]) as $column) { ?>
<th><?=$column?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($fetchSite as $row) {
foreach($row as $column) { ?>
<td><?=$column?></td>
<?php }
} ?>
</tbody>
</table>
perhaps that will help you
Maybe someone can help me out. Any help is appreciated! I'm trying to Combine the "???" row with "unknown files" row. So the total would be 4245. Just one row.
I'm using a while loop. Here is my code
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";
$query = "SELECT company, username, COUNT(company), username FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company ORDER BY company ASC";
$result = mysql_query($query) or die(mysql_error());
echo "<div style='margin-top:100px;'><center><h2>";
echo date(' \ F jS Y - l');
echo "<br />";
echo "</h2><center></div>";
echo '
<center> <table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Company</th>
<th>Total</th>
<th>Users</th>
</tr>
</thead>
<tbody>
';
// Print out result
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}
echo '
</tbody>
</table> </center>
';
?>
You will have to calculate total of ??? and unknown file outside the while loop
$total = 0;
while($row = mysql_fetch_assoc($result))
{
if($row['company'] == NULL || $row['company'] == "unknown file")
$total += $row['COUNT(company)'];
}
Then you can use that total in the main output loop
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
if($row['company'] == "unknown file")
echo "<td>" . $total . "</td>";
else
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}
When i load it it comes up with this error : Notice: Trying to get property of non-object in /public_html/php/application/views/admin/index.php on line 29 and says it is not active when it is active.
Please help
<div class="content">
<?php
$con = mysql_connect("","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
$result = mysql_query("SELECT * FROM users ORDER by user_id");
echo "<link rel='stylesheet' href='<?php echo URL; ?>public/css/style.css' /><div class='CSSTableGenerator' >
<table >
<tr>
<td>ID</td>
<td >Username</td>
<td>User Active</td>
</tr>";
while($row = mysql_fetch_array($result))
{
if ($row->user_active == "1")
{
$mark = 'Yes';
}
else
{
$mark = 'No';
}
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $mark . "</td>";
echo "</tr>";
}
echo "</table></div>";
mysql_close($con);
?>
</div>
Thanks josh_24_2
while($row = mysql_fetch_array($result))
{
if ($row['user_active'] == "1")
{
$mark = 'Yes';
}
else
{
$mark = 'No';
}
echo "<tr>";
echo "<td>{$row['user_id']}</td>";
echo "<td>{$row['user_name']}</td>";
echo "<td>{$mark}</td>";
echo "</tr>";
}
echo "</table></div>";
I would like to display two picture variables in a single cell
http://i.imgur.com/9ZuRhnD.gif
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n";
}
echo "<td align='center' width='140'>" . "<img src=\"{$row['Limgt']}\">" ."</td>";
echo "<td align='center' width='40'>" . "<img src=\"{$row['Limg']}\">" ."</td>";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
What am I missing
You're putting them each in their own cell () - try this:
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n<td>";
}
echo "<img src=\"{$row['Limgt']}\">";
echo "<img src=\"{$row['Limg']}\">";
$i++;
if ($i == $items) {
echo "</td></tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
you are making them in two TDs so you have to make them in one TD like that
echo "<td align='center' width='140'>
<img src=\"{$row['Limgt']}\">
<img src=\"{$row['Limg']}\">
</td>";