I'm a novice to PHP. Here, I am getting an empty table while applying the below code for setting the columns name dynamic.
Here, $highestColumm ideally it should be int value of number of columns.
<table>
<?php $objPHPExcel->setActiveSheetIndexByName('Excel Sheet');
$highestColumm = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
$highestRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$highestColumm++;
for ($row = 1; $row < $highestRow + 1; $row++) {
$dataset = array();
for ($column = 'A'; $column != $highestColumm; $column++) {
$dataset[] = $objPHPExcel->setActiveSheetIndex(0)->getCell($column . $row)->getValue();
}
$datasets[] = $dataset;
}
?>
<tbody>
#for($i=1;$i<=$highestColumm;$i++)
#php
#$dataC = $objPHPExcel->getActiveSheet(0)->getCell('A'.#$i)->getCalculatedValue();
#endphp
<tr>
<td #php #$name = $objPHPExcel->getActiveSheet(0)->getCell('A'.#$i)->getCalculatedValue(); #endphp {{#$name}} </td>
<td #if(#$i==1) #endif>#php #$name = $objPHPExcel->getActiveSheet(0)->getCell('B'.#$i)->getCalculatedValue(); #endphp {{#$name}} </td>
<td #if(#$i==1) #endif>#php #$name = $objPHPExcel->getActiveSheet(0)->getCell('C'.#$i)->getCalculatedValue(); #endphp {{#$name}} </td>
</tr>
#endfor
</tbody>
</table>
Can i use HTML inside PHP will that in this case?
Thanks in Advance
Related
I'm trying to display the content of a text file in table format, with two columns and four rows:
Mateo;Pérez
Marcos;Martínez
Lucas;Télez
Juan;Pez
This is the PHP I'm using but the result is not the desired:
<?php
$course = file_get_contents("course.txt");
$line = explode("\n", $course);
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
{echo"
<table border='1' style='width:100%'>
<tr>
<td>".$item[0]."</td>
<td>".$item[1]."</td>
</tr>
</table>
";
}
}
?>
This is what i get:
This might be what you want.
<?php
$course = file_get_contents("course.txt");
$line = explode("\n", $course);
echo "<table border='1' style='width:100%'>";
for($i = 0; $i<count($line); $i++)
{
$item = explode(";", $line[$i]);
{
echo "
<tr>
<td>".$item[0]."</td>
<td>".$item[1]."</td>
</tr>
";
}
}
echo "</table>"
?>
The problem you has is that you were looping the table as well as the TR and TD tags. So basically you had multiple tables, instead of one.
<?php
$course = file_get_contents("course.txt");
$line = explode("\n", $course);
?>
<table border='1' style='width:100%'>
<?php
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
{echo"
<tr>
<td>".$item[0]."</td>
<td>".$item[1]."</td>
</tr>
";
}
}?>
<?php
</table>
?>
<table border='1' style='width:100%'>
<?php
$course = file_get_contents("course.txt");
$line = explode("\n", $course);
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
{echo"
<tr>
<td>".$item[0]."</td>
<td>".$item[1]."</td>
</tr>";
}
}
?>
</table>
Your html was not structured for desired result . Also try to make clear codes .. Try this-
<table border='1' style='width:100%'>
<?php
$course = file_get_contents("course.txt");
$line = explode("\n", $course);
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
{
?>
<tr>
<td><?php echo $item[0]; ?></td>
<td><?php echo $item[1]; ?></td>
</tr>
<?php
}
}
?>
</table>
How can I remove the last element in a do-while generated table?
In my case the last tr/td where div.dividing_line is stored.
The code:
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
++$i;
} while ($i < $ArrayLength+1);
For example: If I have an array with 6 items, normally the do-while will do the job, so finally there will be 6 tr's with data and 6 tr's with the dividing_line.
What I need is 6 tr's of data and 5 tr's of dividing_line. Is that possible?
Try this-
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i != $ArrayLength) {
echo '<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
}
++$i;
} while ($i < $ArrayLength+1);
Use an extra if Statement to check whether you are at the last element:
if (%i < $ArrayLength) { echo '<tr>...dividing_line</tr>'; }
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i < $ArrayLength)
{
echo '
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>';
}
++$i;
} while ($i < $ArrayLength+1);
I think your approach just needs an additional step.
It could be something like this:
$data = null;
foreach ($rows as $row) {
$data[] = "<tr><td valign=\"middle\">Data_Position</td><td valign=\"middle\">Data_Item</td><td valign=\"middle\">Data_Pieces</td><td valign=\"middle\">Data_Price</td></tr>";
}
print implode("<tr><td colspan=\"4\"><div class=\"dividing_line\"></div></td></tr>", $data);
This way, you could accomplish what you want without any more logic. Of course, it can be changed or re-design, but I think this way will provide you with a simple yet elegan solution to your problem.
Hope it helps :P
I am trying to use for loops to create a table which dynamically returns something like this: Note how the td content have been arranged
<table>
<tr>
<td>1</td>
<td>3</td>
<td>5</td>
<td>7</td>
<td>9</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
<td>6</td>
<td>8</td>
<td>10</td>
</tr>
</table>
among What I have tried so far is
$row=2;
$col=20;
$x=0;
echo '<table>';
for($i=0;$i<$row;$i++){
echo '<tr>';
for($k=0;$k<$col;$k++)
{
echo '<td>'.echo $x+=1.'</td>';
}
echo '</tr>';
}
In this case I get something different and which is not what I want.
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
</table>
Kindly someone help me map this.Thanks
<?php
# how high to count
$count = 10;
# how many rows in the table
$rows = 2;
# figure out how many columns
$columns = ceil($count/$rows);
# could also go the other way and declare there to be 5 columns and then
# divide to get the rows, but since you're counting down the columns,
# I thought this made more sense. Either way.
?><table><?php
for ($i=0; $i<$rows; ++$i) {
?><tr><?php
for ($j=0; $j<$columns; ++$j) {
# calculate which number to show in column $j of row $i. Each column adds
# $rows numbers to the total, while each row just adds one more. And we
# want to start at 1 instead of 0.
$n = $j * $rows + $i + 1;
?><td><?= $n ?></td><?php
}
?></tr><?php
}
?></table>
$total_count = 10;
$total_rows = 2;
$table = array();
//building table array
for($i=1;$i<=$total_count;$i++) {
$row = $i % $total_rows;
$row = $row == 0 ? $total_rows : $row;
$table[$row][] = $i;
}
//generate table based on array
echo "<table>";
for($row=1;$row<=$total_rows;$row++) {
echo "<tr>";
foreach($table[$row] as $cell) {
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
echo "</table>";
This isnt as complicated as people are making it seem
Start the inner loop at whatever row you're currently on and add 2 each time.
<?php
$rows=2;
$cols=10;
?>
<table>
<?php for($i=1;$i<=$rows;$i++): ?>
<tr>
<?php for($k=$i;$k<$cols;$k+=2): ?>
<td><?php echo $k ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
Id probably use range and foreach though
<?php
$rows=2;
$cols=10;
?>
<table>
<?php foreach( range( 1, $rows ) as $row ): ?>
<tr>
<?php foreach( range( $row, $cols, 2 ) as $col ): ?>
<td><?php echo $col ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
This approach will split the data itself
function array_chunk_vertical($array = null,$cols = 3, $pad = array(null))
{
if (is_array($array) == true and !empty($array))
{
$rows = ceil(count($array)/$cols);
$array = array_chunk($array,$rows);
if (count($array) < $cols)
{
$array = array_pad($array,$cols,$pad);
}
foreach($array as $key => $value)
{
$array[$key] = array_pad($value,$rows,null);
}
foreach($array as $key => $value)
{
foreach($value as $sub_key => $sub_value)
{
$output[$sub_key][$key] = $sub_value;
}
}
return $output;
}
return $array;
}
$data = array(1,2,3,4,5,6,7,8,9,10,11);
echo '<table border="1">';
foreach(array_chunk_vertical($data,ceil(count($data)/2)) as $row)
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>' . $col . '</td>';
}
echo '</tr>';
}
echo '</table>';
I have a table which echos back some SQL counts for each user using foreach. I want to have a total which will add up all the counts foreach. Any ideas would be appreciated.
$students = get_course_students($course->id, 'lastname');
$toggle=0;
if (!empty($students)) {
foreach ($students as $student) {
$user=get_record('user','id',$student->id);
$atriskcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 2 AND user = $user->id AND course = $course->id");
$personalcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 1 AND user = $user->id AND course = $course->id");
$reportscountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_reports WHERE user = $user->id AND course = $course->id");
echo '<tr class="r'.$toggle.'">
<td class="cell c'.$toggle.'" valign="top" nowrap="true" align="left">'.fullname($user).'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$atriskcountrecords.'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$personalcountrecords.'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$reportscountrecords.'</td>
</tr>';
if($toggle==0)$toggle=1;
else $toggle=0;
}
}
echo'<tr>
<td><strong>Totals</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td></td>
</tr>';
echo'</table>';
If I really understand your question, it's quite easy ; just declare a variable per counter before your foreach loop and increase it with count value.
For example :
$atriskcountrecordsGlobal = 0;
if (!empty($students)) {
foreach ($students as $student) {
...
$atriskcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 2 AND user = $user->id AND course = $course->id");
$atriskcountrecordsGlobal += $atriskcountrecords;
...
}
I need to read a bi-dimensional array and convert the values into an HTML table. For example:
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
Reading this array, I want to dynamically write the <TABLE> syntax where the cells with the same number in the grid are merged horizontally or vertically:
That is, I should be able to create the following syntax from the $grid above:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td colspan="4">4</td>
</tr><tr>
<td colspan="2">3</td>
<td colspan="2" rowspan="2">5</td>
</tr><tr>
<td> </td>
<td> </td>
</tr></table>
I have been able to process the colspan correctly, but am having trouble with the rowspan issue. Any ideas? basically I want to be able to create custom homepages with custom content from the array "format" established by the $grid.
Any help greatly appreciated!
Code:
<?php
$grid = array(
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
);
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
$w = count($grid[0]);
$h = count($grid);
for ($y = 0; $y < $h; ++$y) {
echo "<tr>\n";
for ($x = 0; $x < $w; ++$x) {
$value = $grid[$y][$x];
if ($value === null) {
continue;
}
$colspan = 1;
while ($x + $colspan < $w && $grid[$y][$x + $colspan] === $value) {
$grid[$y][$x + $colspan++] = null;
}
$rowspan = 1;
$rowMatches = true;
while ($rowMatches && $y + $rowspan < $h) {
for ($i = 0; $i < $colspan; ++$i) {
if ($grid[$y + $rowspan][$x + $i] !== $value) {
$rowMatches = false;
break;
}
}
if ($rowMatches) {
for ($i = 0; $i < $colspan; ++$i) {
$grid[$y + $rowspan][$x + $i] = null;
}
++$rowspan;
}
}
$rowspan = $rowspan > 1 ? " rowspan=\"$rowspan\"" : "";
$colspan = $colspan > 1 ? " colspan=\"$colspan\"" : "";
echo " <td$rowspan$colspan>$value</td>\n";
}
echo "</tr>\n";
}
?>
Output:
<tr>
<td colspan="4">4</td>
</tr>
<tr>
<td rowspan="2" colspan="2">3</td>
<td rowspan="3" colspan="2">5</td>
</tr>
<tr>
</tr>
<tr>
<td colspan="2">0</td>
</tr>
<?php
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
foreach($grid AS $rowkey => $row)
{
echo "<tr>\n";
foreach($grid[$rowkey] AS $columnKey => $field)
{
echo "<td>{$field}</td>\n";
}
echo "</tr>\n";
}
?>