I am trying to make a calendar:
<table border="1">
<tr><th colspan="7"><?php echo $current_month ?></th></tr>
<tr>
<?php foreach ($days as $day) {
echo "<th>" . $day . "</th>";
} ?>
</tr>
<tr>
<?php
foreach($keys as $row => $value) {
echo "<td>" . $value . "</td>";
}
?>
</tr>
</table>
How can I do to echo 7 values per row? AS you can see in the image, it displays all days in the same row. (of course, because I have put it in the same , but, how can I make that starts a new every 7 echo values?
Thank you!!!
You can use modulus to check if it has reached the seventh element and end the row.
<?php
foreach($keys as $row => $value) {
if ($value % 7 == 0) {
echo "<td>" . $value . "</td></tr><tr>";
} else {
echo "<td>" . $value . "</td>";
}
}
?>
An iteration counter is a pretty simple method.
<table border="1">
<tr><th colspan="7"><?php echo $current_month ?></th></tr>
<tr>
<?php foreach ($days as $day) {
echo "<th>" . $day . "</th>";
} ?>
</tr>
<tr>
<?php
$i = 0; // Begin at 0 days written
foreach($keys as $row => $value) {
if ($i == 7){
// Create new table row after every 7th iteration
print "</tr><tr>"; // Add \n or \t for output formatting
$i = 0;
}
echo "<td>" . $value . "</td>";
$i++; // Increment $i each iteration
}
?>
</tr>
</table>
Try the below code,
You can split the array into chunks of particular sizes, See more information about this from here array chunk
<?php
$current_month = 'Oct';
$days = array('1',2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
$chunk = array_chunk($days,7);
?>
<table border="1">
<tr>
<th colspan="7"> <?php
echo $current_month; ?>
</th>
</tr>
<?php
foreach($chunk as $day) {
echo '<tr>';
foreach($day as $key) {
echo "<th>" . $key . "</th>";
}
echo '</tr>';
} ?>
</table>
Related
I have two date filters (from and to) which will represent days in the current month and I declare them as two variables.
How do I run a for loop in each field from start_date to end date?
I want to fetch the data in the selected date range. The table header is done, it show current selected date.
Here is my code:
$start_date = date('d', strtotime($from));
$end_date = date('d', strtotime($to));
?>
<div>
<div>
<table>
<thead>
<tr>
<th>Type</th>
<?php
$dates = range($end_date, $start_date);
foreach ($dates as $fpi) {
echo '<th>' . $fpi . '</th>';
}
?>
</tr>
</thead>
<tbody class="text-center">
<tr>
<td class="font-weight-bold text-no-wrapping">man</td>
<?php
foreach ($data['man'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">women</td>
<?php
foreach ($data['women'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">children</td>
<?php
foreach ($data['children'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">as</td>
<?php
foreach ($data['as'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">ot define</td>
<?php
foreach ($data['not_define'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">old</td>
<?php
foreach ($data['old'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
</tbody>
</table>
</div>
</div>
this will loop through the dates range, feel free to change $date->format('Y-m-d) to what you need
$date = $start_date
while($date <= $end_date){
echo '<th>' . $date->format("Y-m-d"). '</th>';
$date->modify('+1 day');
}
I've been struggling for weeks just to analyze the problem of my code, the JQuery Pagination table didn't show the PHP value. I thought it would be PHP that causes the problem.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>No.</th>
<th>Nama Mitra</th>
<th>Jenis Kelamin</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
//I think the problem starts here
while ($rowmitra = mysqli_fetch_array($mitra)) {
echo '<tr>';
echo "<th scope='row'>" . $i . "</th>";
echo "<td>" . $rowmitra['id_mitra'] . "</td>";
echo "<td>" . $i . "</td>";
echo "<td>" . $rowmitra['mitra'] . "</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/' . $rowmitra["logo"] . '"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
?>
</tbody>
</table>
And here's what I do to fetch Mitra table
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$rowmitra = mysqli_fetch_array($mitra);
I put it in a different file that I use include 'head.php'; command to merge both. When I fetch it in outside while command it works.
It didn't fetch an array of my PHP when I put it in while. Am I clumsy enough not to know where is the problem exactly? I've tried to match the variable and also search for alternative ways to fetch array. But it didn't work.
Thank you for your help.
You should not declare the result fetch array outside while loop. Remove this $rowmitra = mysqli_fetch_array($mitra); and try with mysqli_fetch_assoc to get database column name.
<?php
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$i = 1;
//I think the problem starts here
if (mysqli_num_rows($mitra) > 0) {
while ($rowmitra = mysqli_fetch_assoc($mitra)) {
echo '<tr>';
echo "<th scope='row'>".$i."</th>";
echo "<td>".$rowmitra['id_mitra']."</td>";
echo "<td>".$i."</td>";
echo "<td>".$rowmitra['mitra']."</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/'.$rowmitra["logo"].'"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
}
?>
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
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>";
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);
?>