I'm trying to populate a Bootstrap table in order to have a table like this:
Table Example
The final result should be that i can see which student will be present on that day.
Now I've tried to cycle the result of my database queries and put them inside the table tr and td but I'm missing how to do it properly...and the code looks very confusing...
If i do this way:
<?php
if($usersMON) {
foreach ($usersMON as $userlun){ ?>
<tr>
<td class="text-center maiuscolo"><?=$userlun['name']?></td>
<?php
}
} ?>
<?php
if($usersTUE) {
foreach ($usersTUE as $usermar){ ?>
<td class="text-center maiuscolo"><?=$usermar['name']?></td>
<?php
}
}
?>
<?php
if($usersWED) {
foreach ($usersWED as $usermer){ ?>
<td class="text-center maiuscolo"><?=$usermer['name']?></td>
<?php
}
}
?>
<?php
if($usersTHU) {
foreach ($usersTHU as $usergio){ ?>
<td class="text-center maiuscolo"><?=$usergio['name']?></td>
<?php
}
}
?>
<?php
if($usersFRI) {
foreach ($usersFRI as $userven){ ?>
<td class="text-center maiuscolo"><?=$userven['name']?></td>
<?php
}
}
?>
</tr>
I obtain this:
Table result
Could you suggest me a proper way to do it?
The prerequisite is that the user quantity is the same.
You could put the $usersMON ~ $usersFRI into an array.
array_push($allUsers, $usersMON, usersTUE, usersWED, usersTHU, usersFRI);
Then, show each student's name
<?php for ($i=0; $i < $userQuantity; $i++) { ?>
<tr>
<?php foreach ($allUsers as $users) { ?>
<td><?=$users[$i];?></td>
<?php } ?>
</tr>
<?php } ?>
$users = [
$usersTUE,
$usersWED,
$usersTHU,
$usersFRI
];
$col_count = count($users);
$row_count = 0;
foreach ($users as $u) {
$row_count = max($row_count, count($u));
}
echo "<table>";
for ($i=0; $i<$row_count; $i++) {
echo "<tr>";
for ($t=0; $t<$col_count; $t++) {
$user = $users[$t][$i] ?? [];
echo "<td class='text-center maiuscolo'>" . $user['name'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
Related
I am Having an issue in my for loop using PHP, I want to create dynamic row and column, Each row has 10 column after 10 column, the second row also end with 10 column like this up to 5 row, how to do It for a loop.
My for loop code:
<table width="100%" border="1">
<?php
for($i=1; $i<=72; $i++)
{
?>
<tr>
<td width="100%">
<?php echo "Click Here to see Site No.'".$i."'. & Area sqft No" .$i;?></a></td>
</tr>
<?php
}
?>
</tr>
</table>
I tried like this also
<table width="100%">
<tr>
<?php
for($i=1; $i<=72; $i++)
{
$x = 10;
if ($i % $x == 0)
{
?>
<td><?php echo $i;?></td>
<?php
}
}
?>
</tr>
</table>
<?php
echo '<table width="100%" border="1">';
for($i=1; $i<=8; $i++)
{
$y=10;
$y*=($i-1);
echo '<tr>';
for ($x=1; $x <=10; $x++) {
if ($i==1) {
echo '<td>'.$x.'</td>';
}else{
$y+=$x;
echo '<td>'.$y.'</td>';
if ($y==72) {
break;
}
$y-=$x;
}
}
echo '</tr>';
}
echo '</table>';
This will print the bellow table:
As per what I understand you want simple 10 column in each row.
Here is my code which may help you:
<table style="border:1px solid #000">
<tr>
<?php $t=1; for($k=1;$k<=72;$k++){?>
<?php if($t == 10) { $t=0;?><td style="border:1px solid #000"> <?php echo $k; ?> </tr><?php } else {?><td style="border:1px solid #000"> <?php echo $k; ?></td><?php } ?>
<?php $t++;}?>
<?php $x = 10; if ($i % $x == 0) { ?>
....
<?php } ?>
If I am getting the question correct, why don't you do as below:
echo '<table>';
for($i=1; $i<=5; $i++) {
echo '<tr>';
for ($y=1; $y<=10; $y++) {
echo '<td>Row_'.$i.' - Col_'.$y.'</td>';
}
echo '</tr>';
}
echo '</table>';
It will print something like below:
I'm trying create loop in my table, there is 4 item, when column is 3 then create new row . The current output is like this:
x
x
x
x
Here's my code:
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if ($i <= 3) { ?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php }
}
echo "</tr>";
?>
</table>
What i expected is like this:
x|x|x
x
Thank you.
In the comment section of your question, Sirko is right.
Anyway you can do this like below;
<?php
$i = 0;
foreach ($list_items as $item) {
if($i % 3 == 0)
echo '<tr>';
echo '<td> bla bla bla </td>';
if($i % 3 == 0)
echo '</tr>';
$i++;
}
Change your code to below, it should work.
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if($i%3==0) echo echo "</tr><tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
</td>
<td>
<?php echo $item['qty'] ?>
</td>
<?php
}
if($i%3!=0)
echo "</tr>";
?>
</table>
use array_chunk()
<?php
foreach (array_chunk($list_items,3) as $items) {
echo '<tr>';
foreach($items as $item){
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
}
echo '</tr>';
}
?>
try this ==>
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item) { // there is 4 item
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will open
echo "<tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will close
echo "</tr>";
$i++;
}
?>
</table>
The course name php is available in the table one time but when I retrieve it from table it printing multiple time. I have tried the following code - please help me what's wrong with the code? (if I remove the drop down which is available below the heading it is printing single time in the table)
<?php
include("session.php");
?>
<?php
include("../view/common/head.php");
?>
<?php
include('../view/common/tab.php');
?>
<body class="bg-color">
<div class="container">
<h1> List Of Courses</h1>
<br/>
<?php
if (isset($_SESSION['message1']))
{
echo $_SESSION['message1'];
unset($_SESSION['message1']);
}
?>
<?php
if (isset($_SESSION['courseupdated']))
{
echo $_SESSION['courseupdated'];
unset($_SESSION['courseupdated']);
}
?>
<?php
if (isset($_SESSION['deletecourse']))
{
echo $_SESSION['deletecourse'];
unset($_SESSION['deletecourse']);
}
?>
<?php
if (isset($_SESSION['notdelete']))
{
echo $_SESSION['notdelete'];
unset($_SESSION['notdelete']);
}
?> <div class="col-md-6 form-group">
<select class="form-control">
<?php
include('../model/functions.php');
$table="courses";
$condition="";
$drop=Selectdata($table,$condition);
foreach($selectarray as $drop)
{
echo '<option value="'.$drop['course_id'].'">'.$drop['course_name'].'</option>';
}
?>
</select>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>Sno</th>
<th>Course Name</th>
<th>Course Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$per_page=5;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$table="courses";
$per_page=5;
$start_from = ($page-1) * $per_page;
$condition="limit ".$start_from.",".$per_page." ";
$limit=Selectdata($table,$condition);
if($rowcount==0)
{
echo "no records found";
}
else
{
$sno=($page-1) * $per_page;
foreach($selectarray as $course){
$sno++;
echo '<tr>';
echo '<td>'.$sno.'</td>';
echo '<td>'.$course['course_name'].'</td>';
echo '<td>'.$course['course_description'].'</td>';
echo '<td><button type="submit">edit</button><button Onclick="return ConfirmDelete();" type="submit">Delete</button></td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
Click here to add new course
<br/>
<br/>
Toggle Menu
</div>
<?php
$table = "courses";
$condition=" ";
$select=Selectdata($table,$condition);
$total_records = $rowcount;
$total_pages = ceil($total_records / $per_page);
echo '<div class="text-center">';
echo '<ul class="pagination">';
echo "<li><a href='courselist.php?page=1'>".'<<'.'</a></li>';
for ($i=1; $i<=$total_pages; $i++) {
echo "<li><a href='courselist.php?page=".$i."'>".$i.'</a></li>';
}
echo "<li><a href='courselist.php?page=$total_pages'>".'>>'.'</a></li>';
echo "</ul>";
echo '</div>';
?>
<?php
include("../view/common/footer.php");
?>
functions.php
function Selectdata($table,$condition="")
{
global $conn,$result,$selectarray,$rowcount;
$sql="SELECT * from ".$table." ".$condition." ";
$result=$conn->query($sql);
$rowcount=$result->num_rows;
while($row=$result->fetch_assoc())
{
$selectarray[]=$row;
}
return $result;
}
I hope this code is not intended to be used in a real project.
Just add
$selectarray = array();
before
while
in Selectdata function definition.
What happens is that each time you call that function it just amends the full rowset to the same array as it is global.
Try this :
foreach($selectarray as $row)
{
echo '<option value="'.$row['course_id'].'">'.$row['course_name'].'</option>';
}
Im using tables to store content that's dynamically loaded. It's for a reservation form which will be responsive. What I'm looking to do is break each table row into two if there are more than 5 columns in order for the mobile version to fit on screen.
I'm sure this can be achieved by extending what I already have but can't get it to work.
Here's my current code:
<table>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach ?>
</tr>
<tr>
<?php foreach ($hostel->getAvailableDates() as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach ?>
</tr>
</table>
I'd need to break the loop for each row tr after 5 loops, then add a new row underneath.
I've been experimenting with
$max_loop = 5;
$count = 0;
But no luck so far.
I prefer to reorganize data:
<?php
$availDates = array();
foreach ($hostel->getAvailableDates() as $date) {
$availDates[] = $date;
}
$maxCols = 5;
$chunked = array_chunk( $availDates, $maxCols );
?>
<table>
<?php
foreach ($chunked as $chunk) {
?><tr>
<?php foreach ($chunk as $date): ?>
<th><?php echo $date->getDayOfTheWeek(); ?></th>
<?php endforeach; ?>
</tr>
<tr>
<?php foreach ($chunk as $date): ?>
<td>
<?php if($date->getAvailable()) { ?>
<b class="avail tick">Available</b>
<?php } else { ?>
<b class="avail cross">Unavailable</b>
<?php }?>
</td>
<?php endforeach; ?>
</tr><?php
}
?>
</table>
Look at the mod operator. It should give you what you need.
if($count % $max_loop == 0)
I hope this may help you. thanks.
<?php
$avDates = $hostel->getAvailableDates();
echo "<table><tr>";
foreach($avDates as $i=>$date){ {
if ($i == $max_loop) {
echo "</tr><tr>";
}
echo "<td>".($date->getAvailable() ? '<b class="avail tick">Available</b>' : '<b class="avail cross">Unavailable</b>')."</td>";
}
echo "</tr></table>";
?>
If the value returned by getAvailableDates is an array, you could use a for loop instead of a foreach, and check if the current index is a multiple of five, so you don't have to keep track of the count variable
$avDates = $hostel->getAvailableDates();
for ($i = 0; $i < count($avDates); $i++) {
$date = $avDates[$i];
//do your staff
//if multiple of five add another tr
if ($i % 5 == 0) {
}
}
I am trying to make a table with 4 rows from a foreach-call.
My problem is, that in the result I get each ID twenty times in the same column.
I'm using this code:
<table width="80%" border="0" cellpadding="10px">
<?php foreach (array_chunk($items, 4) as $row) { ?>
<?php
$i = 0;
foreach ($items as $item):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
} ?>
<tr
<?php echo $class;?>
>
<?php foreach ($row as $item){ ?>
<td>
<?php echo htmlentities ($item['Item']['id']); ?>
</td>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
</table>
Any idea how I could get each ID just once?
You are incrememnting $i at every $item as opposed to every $row
Is this the fix you are looking for?
Edit: Mikel has your fix, add this to fix the row bug (Typical of me to notice that first eck!)
<table width="80%" border="0" cellpadding="10px">
<?php
$i = 0;
$chunkedarray = array_chunk($items, 4);
foreach ($chunkedarray as $row) {
$class = null;
if ($i++ % 2 == 0)
$class = ' class="altrow"';
echo "<tr ".$class.">";
foreach ($row as $item){
echo "<td>";
echo htmlentities ($item['Item']['id']);
echo "</td>";
}
echo "</tr>";
}?>
</table>