BOOTSTRAP Table in a PHP Foreach loop - php

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

Related

HTML table align cells with header from another table

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

Mysql Database table error

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

Pagination not working when replacing Table by Dynamic DataTable in JQuery

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>

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

How to give alternating table rows different background colors using PHP

I have a table of data that is generated dynamically based on the contents stored in a mysql database.
This is how my code looks:
<table border="1">
<tr>
<th>Name</th>
<th>Description</th>
<th>URL</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM categories");
while ($row = mysql_fetch_assoc($query)) {
$catName = $row['name'];
$catDes = $row['description'];
$catUrl = $row['url'];
echo "<tr class=''>";
echo "<td>$catName</td>";
echo "<td>$catDes</td>";
echo "<td>$catUrl</td>";
echo "</tr>";
}
?>
</table>
Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
and that would be the end of that. However since the table rows are dynamically generated, how can I achieve this?
Or you could just use CSS:
table tr:nth-child(odd) {
background-color: #ccc;
}
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';
echo "<tr class='$class'>";
?>
It basically checks to see if $x is divisible evenly by 2. If it is, it is even.
P.S. if you haven't seen that style of if else query, it is called a ternary operator.
Set a variable to true/false or a number and then back again during each iteration. Or use the modulus operator such as $i%2==0 in a while loop where $i is a number and use this condition in a ternary statement or something that sets the class value of the <tr>
Easiest way to alternate row colors in PHP/HTML?
$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
<?
$color="1";
while ($line = mysql_fetch_array($result)) {
if($color==1){
echo '<tr bgcolor="">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc">';
$color="1";
}
?><td align="left" width="40"><?= $line[name] ?></td>
<?
}
?>
This is my working code !
Here is my working part !
`
$i=1;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
{
echo '<tr bgcolor="#FFFF00">';
}
else
{
echo '<tr bgcolor="#99FFCC">';
}
$i++;
echo "<td>" . $row['blah'] . "</td>";
echo "<td>" . $row['blah_blah'] . "</td>";
echo "</tr>";
}
echo "</table>";
`
This is how I did it. I declared a css class called "even" with all the styling i wanted. Then looped through the scenario. Hope it helps!
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i%2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>

Categories