<?php
$arr = range(1,rand(40,120)); ?>
<table>
<?php
foreach ($arr as &$value) {
echo '<tr><td>' . $value . '</td></tr>';
} ?>
</table>
This generate for me for example:
1
2
3
...
111
all in one columns. How can i make - when in first column are 25 rows then create new column, etc. For example:
1 26 51
2 27 ...
3
..
25 50
How can i make it?
I can use DIV instead of table.
<?php
$arr = range(1,rand(40,120));
?>
<div style="width:40px; float:left;">
<?php
foreach ($arr as $value) {
echo $value . '<br />';
if ($value % 25 == 0) {
echo '</div><div style="width:40px; float:left;">';
}
}
?>
Vertically sorted columns of that sort (no pun intended) are a serious pain in html, since this arragement is a "top->bottom, left->right", while HTML tables by their nature are "left->right, top->bottom" instead.
To get around it, you have to do some offset math on the array indexes, so you can output by rows:
$arr = range(1,rand(40,120));
$rows = ceil(count($arr) / 3); // assuming 3 columns
for ($i = 0; $i < $rows; $i++) {
echo <<<EOL
<tr>
<td>{$arr[$i]}</td> 1, 2, 3, etc...
<td>{$arr[$i+rows]}</td> 11, 12, 13, etc...
<td>{$arr[$i+(2*$rows)]}</td> 21, 22, 23, etc...
</tr>
EOL;
}
This code probably won't work as is, but should give you the basic idea.
EDITED
<?php
$rows = 25;
$arr = range(1, rand(40, 120));
$arr = array_merge($arr, array_fill(0, $rows - (count($arr) % $rows), null));
$cols = ceil(count($arr) / $rows);
$render = array();
echo '<table>' . "\n";
foreach ($arr as $i => $value) {
$render[$i % $rows][] = $value;
if (count($render[$i % $rows]) == $cols) {
echo ' <tr>' . "\n" . ' <td>' . implode('</td>' . "\n" . ' <td>', $render[$i % $rows]) . '</td>' . "\n" . ' </tr>' . "\n";
}
}
echo '</table>' . "\n";
?>
Related
Ok i have a string which needs to be splitted 2 times.
First time by whitespaces and second time by commas. So i can put it in a table.
I managed to split the string by whitespaces and put it in the first column of the table but i struggle to split it for the second time and put the values in the right column.
Here the snippets of what i already got:
<?php for ($i = 0; $i < sizeof($volumes); $i++) {
echo "<tr><td>" . $volumes[$i] . "</td></tr>";
} ?>
When you render the table rows you should split each volume by comma. I don't understand exactly all the retrieved rows or what is the logic behind your code but this bunch of code should do what you need:
<?php
for ($i = 0; $i < sizeof($volumes); $i++) {
echo '<tr>';
$volumeData = explode(',', $volumes[$i]);
foreach ($volumeData as $volume) {
echo '<td>' . $volume . '</td>';
}
echo '</tr>';
}
?>
Are you sure the string you provided is accurate? I think there's a coma missing between the size of SystemReserved and the label of the next drive. If that's the case - the code should be something like this:
First we 'explode' the string to create an array, then use array chunk to split it into arrays with seven entries each. And then render it:
$string = 'L,Logs,NTFS,Healthy,OK,9.73,9.77 ,SystemReserved,NTFS,Healthy,OK,0.16,0.49 ,C,LocalDisk,NTFS,Healthy,OK,18.19,29.74';
$array = explode(',', $string);
$results = array_chunk($array, 7, true);
?>
<table id="tbl_basic_volumes">
<tr>
<th>Buchstabe:</th>
<th>Name:</th>
<th>Filesystem:</th>
<th>Health Status:</th>
<th>Operational Status:</th>
<th>Freier Speicherplatz:</th>
<th>Gesamter Speicherplatz:</th>
</tr>
<?php
foreach ($results as $result) {
echo '<tr>';
foreach ($result as $entry) {
echo '<td>'.$entry.'</td>';
}
echo '</tr>';
}
?>
</table>
You can escape the inner foreach loop using implode.
$str = "L,Logs,NTFS,Healthy,OK,9.73,9.77 ,SystemReserved,NTFS,Healthy,OK,0.16,0.49 C,LocalDisk,NTFS,Healthy,OK,18.19,29.74";
$rows = explode(' ', $str);
foreach ($rows as $row) {
echo '<tr><td>' . implode('</td><td>', explode(',', $row)) . '</td></tr>';
}
Or even replacing commas with </td><td> will also work:
$str = "L,Logs,NTFS,Healthy,OK,9.73,9.77 ,SystemReserved,NTFS,Healthy,OK,0.16,0.49 C,LocalDisk,NTFS,Healthy,OK,18.19,29.74";
$rows = explode(' ', $str);
foreach ($rows as $row) {
echo '<tr><td>' . str_replace(',', '</td><td>', $row) . '</td></tr>';
}
How can I generate header for printed 2d array in php?
So my array look like this:
$tab=array(
array(0,1,2,3),
array(1,2,3,4),
array(2,3,4,5)
)
This code:
foreach ($tab as $key => $row){
echo '<b>o<sub>'.($key+1).'</sub></b> ';
foreach ($row as $item) {
echo $item.' ';
}
echo '<br>';
}
Print this:
o1 0 1 2 3
o2 1 2 3 4
o3 2 3 4 5
But I need this:
a1 a2 a3 d
o1 0 1 2 3
o2 1 2 3 4
o3 2 3 4 5
Where lenght of rows may be diferent and last column always must be d
Thanks for help
You can check if this is the first iteration in the first foreach and if so, add the first line.
foreach ($tab as $key => $row) {
// If first iteration, add the header
if ($key === 0)
{
foreach ($row as $i => $item)
{
// Last header must be 'd'
if ($i === count($row) - 1)
echo '<b>d</b>';
else
echo '<b>a<sub>' . ($i + 1) . '</sub></b> ';
}
echo '<br />';
}
// Add the current row
echo '<b>o<sub>' . ($key + 1) . '</sub></b> ';
foreach ($row as $item) {
echo $item . ' ';
}
echo '<br />';
}
If the first row is a static row, then you can simply print it using echo function at first.
But if its a dynamic tab as well, then you can use the following code:
$count = count($tab[0]);
for($i = 1; $i <= $count; $i++)
{
if($i != $count) echo '<b>a<sub>' . ($i + 1) . '</sub></b> ';
else echo '<b>d</b> ';
}
foreach ($tab as $key => $row){
echo '<b>o<sub>'.($key+1).'</sub></b> ';
foreach ($row as $item) {
echo $item.' ';
}
echo '<br>';
}
You can try this, except use <table> or <div> to align your elements.
foreach ($tab[0] as $key => $item)
echo $key === 0 ? title(' ', ' ') : title('a', $key);
echo title('d', '') . '<br>';
function title(string $letter, string $index)
{
return "<b>{$letter}<sub>{$index}</sub></b>";
}
Here is what my code prints:
Even numbers: 2 4 6
Here is my PHP code:
$names = file('file.txt');
echo "Text: ";
foreach($names as $name)
{
echo $name . "</br>";
}
echo "Even numbers: ";
foreach ($names as $name) {
$name = count_chars( $name, 3);
for($i=0; $i<strlen($name);$i++) {
if (is_numeric($name[$i]) && $name[$i]%2==0)
{
echo $name[$i];
}
}
echo "<br>";
}
?>
Could someone tell me how should I count all of these values into the new line (I need to get an answer 3) and get those values sum (I need to get 12)? I know I have to use count and sum functions, i don't know exactly where. Thanks for any help. All the answer should look like this:
Even numbers: 2 4 6
Even numbers are: 3
Even numbers sum are: 2 + 4 + 6 = 12
I don't really understand what your question.
But I think array_sum is what you are looking for. (https://secure.php.net/manual/en/function.array-sum.php)
Below is my code for your reference:
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach($array as $key => $value) {
if (empty($value%2)) {
$even[] = $value;
} else {
$odd[] = $value;
}
}
echo 'Even numbers: '.implode(' ', $even).'<br>';
echo 'Odd numbers: '.implode(' ', $odd).'<br>';
echo 'Even numbers sum are: '.implode(' + ', $even).' = '.array_sum($even);
Try this :
$names = file('file.txt');
echo "Text: ";
foreach($names as $name)
{
echo $name . "</br>";
}
echo "Even numbers: ";
foreach ($names as $name) {
$count = 0
$evens = [];
$name = count_chars($name, 3);
for($i=0; $i<strlen($name);$i++) {
if (is_numeric($name[$i]) && $name[$i]%2==0)
{
$count++;
$evens[] = $name[$i];
echo $name[$i];
}
}
echo "<br>";
}
echo "Even numbers Are: " . $count;
$i=0;
foreach($evens as $e){
$i++;
if($i == count($evens)){
$evens_string .= $e . '=' ;
} else {
$evens_string .= $e . '+' ;
}
}
$even_string .= array_sum($evens);
echo "Even numbers sum are: " . $even_string;
Hope this gives you the idea! I have not tested the code.
I am using PDO to get result from database and then using foreach loop to display data.
my code is
$result1=$objPage->getGallery($id);
foreach($result1 as $row)
{
echo "pic1=" . $row['pic'];
echo "pic2=" . $row['pic'];
echo "pic3=" . $row['pic'];
echo "<br>";
}
Actually, I want to display three pictures names in one line and then next 3 names.
But now its printing one name 3 times.
Do something like this:
$result1=$objPage->getGallery($id);
$count = 0;
foreach($result1 as $row)
{
echo "pic" . $count + 1 . "=" . $row['pic'];
if( $count % 3 == 2 )
{
echo "<br>";
}
$count++;
}
Information
Modulo %
first, prepare your data
$data = $objPage->getGallery($id);
$data = array_chunk($data,3);
then output it
<table>
<? foreach($data as $chunk): ?>
<tr>
<? foreach($chunk as $row): ?>
<td><img src="<?=$row['pic']?>"></td>
<? endforeach ?>
</tr>
<? endforeach ?>
</table>
You can use an external flag, like this:
$flag = 0;
$result1=$objPage->getGallery($id);
foreach($result1 as $row) {
echo "pic" . ($flag + 1) . "=" . $row['pic'];
if( $flag % 3 == 0 )
echo "<br>";
$flag++;
}
You could use modulo as suggested by others, but how about this more compact method?
$result1 = $objPage->getGallery($id);
$separators = array('', '', '<br/>');
foreach ($result1 as $index => $row) {
echo 'pic' . ($index % 3) . '=' . $row['pic'] . $separators[$index % 3];
}
I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}