This php code shows me the content of a directory on my website:
<?php
$dir = opendir(getcwd());
?>
<body>
<table>
<tbody>
<tr>
<?php
while (($file = readdir($dir)) !== false) {
{
echo "<td>". $file ."</td>";
}
}
closedir($dir);
?>
</tr>
</tbody>
</table>
</body>
It puts the results in a table.
The problem is that the PHP code generates a <td> tag and store the results in it. So the final table has one <tr> and as many <td> tags as there are results.
What I want to have is a table with 3 columns (3 td) per each line (tr tag).
Is there a way to make the table dynamic and for each third <td> tag turns to be a <tr> tag
so the results look like this: (click here)
Instead of looking like this: (click here)
try this:
<?php
$dir = opendir(getcwd());
?>
<body>
<table>
<tbody>
<?php
$n = 0;
while (($file = readdir($dir)) !== false) {
{
if($n%3 == 0){echo "<tr>";}
echo "<td>". $file ."</td>";
$n++;
}
}
closedir($dir);
?>
</tbody>
you can use modulus to keep track of where you are in the loop.
Then, when you've hit a multiplication of 3, you restart the table row:
<?php
$dir = opendir(getcwd());
?>
<body>
<table>
<tbody>
<tr>
<?php
$counter = 0;
while (($file = readdir($dir)) !== false) {
{
if($counter % 3 == 0 && $counter != 0) echo "</tr><tr>";
echo "<td>". $file ."</td>";
$counter++;
}
}
closedir($dir);
?>
</tr>
</tbody>
</table>
</body>
Related
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>";
I want to display a .csv file neatly in a table using php. Some content of the file are empty field.
Here is my first approach, got an error but displaying the datas in one row. Supposedly they should display in their respectively fields.
The code output here:
Should be like this:
<table border = "1">
<tr>
<th>NAME</th>
<th>Email</th>
<th>Address</th>
<th>Payment</th>
<th>Datepaid</th>
</tr>
<?php
$data = file("data/payment.csv");
foreach ($data as $line){
$lineofarray = explode("\t", $line);
list($name, $email, $address, $payment, $datepaid) = $lineofarray;//error here
?>
<tr>
<td>
<?= $name?>
</td>
<td>
<?= $email?>
</td>
<td>
<?= $address?>
</td>
<td>
<?= $payment?>
</td>
<td>
<?= $datepaid?>
</td>
</tr>
<? }?>
</table>
My code now had an error on the part of list. And displaying the datas in one field. Hope you can help me. Thanks
Please try this.
$fp = fopen("data/payment.csv", "r");
while (($line = fgetcsv($fp)) !== false) {
echo "<tr>";
foreach ($line as $field) {
echo "<td>" . htmlspecialchars($field) . "</td>";
}
echo "</tr>\n";
}
This is a safe way to read csv lines as it is unicode safe and also supports fields that are wrapped in double quotes.
You should use fgetcsv:
$fp = #fopen('data/payment.csv', 'r');
$tbl = '<table><thead><tr><th>NAME</th><th>Email</th><th>Address</th><th>Payment</th><th>Datepaid</th></tr></thead><tbody>';
while($r = fgetcsv($fp)){
$tbl .= '<tr>';
foreach($r as $v){
$tbl .= '<td>'.htmlentities($v, ENT_QUOTES, 'UTF-8').'</td>';
}
$tbl .= '</tr>';
}
$tbl .= '</tbody></table>';
echo $tbl; // echo $tbl wherever you want
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'm trying to display a two column, four row CSV file in an HTML table. I have the code below, but it only displays the first row and I don't understand why.
<html>
<head>
<title>test</title>
</head>
<body>
<table border=1>
<?PHP
$file_handle = fopen("oee1.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td></tr>';
}
fclose($file_handle);
?>
</table>
</body>
</html>
The csv file looks like:
test1, 1
test2, 2
test3, 3
test4, 4
You were not iterating through every line.
Try something like this:
<!DOCTYPE html>
<html>
<head>
<title>+test</title>
</head>
<body>
<?php
if (($file_handle = fopen("data.csv", "r")) !== false) {
$str = '';
$str .= '<table>';
while (($data = fgetcsv($file_handle, 1024, ",")) !== false) {
$str .= '<tr>';
foreach ($data as $key => &$value) {
$str .= "<td>$value</td>";
}
$str .= '</tr>';
}
fclose($file_handle);
$str .= '</table>';
echo $str;
}
?>
</body>
</html>
output:
<table>
<tbody>
<tr>
<td>test1</td>
<td>1</td>
</tr>
<tr>
<td>test2</td>
<td>2</td>
</tr>
<tr>
<td>test3</td>
<td>3</td>
</tr>
<tr>
<td>test4</td>
<td>4</td>
</tr>
</tbody>
</table>
PS: make sure you have priviledges and your csv is in the smae directory as your php file.
Reference : fgetcsv
try adding a call to fgets($file_handle); in each iteration of the loop, you have to advance the file handle pointer somehow.
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>