I have a simple working PHP script to write an HTML table from a text file that contains a single column of data that contains HTML links. This writes the data horizontally in a row across 5 or 6 columns as I want it to. But I am looking to set up a script with a loop that will take this list of data and input it into the table until it finishes the data list, so that I do not have to hard code each table cell individually. Just let the script create each table cell, at 5 or 6 columns across (whichever I need for this specific table), go to the next row, etc., until it runs out of data. The data in the data file will be added to on a regular basis, so the table will not be of a certain fixed length forever. I am using the echo command so that I can add some more HTML formatting later on.
Even though my existing script is simple and it works, if you can think of a better way of doing what I am trying to do, all suggestions are appreciated.
Thanks in advance, Stan...
PHP code follows:
<?php
$item = #fopen('linklist.txt', "r");
if ($item) { while (!feof($item)) { $lines[] = fgets($item, 4096); } fclose($item); }
echo'
<TABLE border="1">
<TR>
<TD>'.($lines[1]).'</td>
<TD>'.($lines[2]).'</td>
<TD>'.($lines[3]).'</td>
<TD>'.($lines[4]).'</td>
<TD>'.($lines[5]).'</td>
<TD>'.($lines[6]).'</td>
</tr>
<TR>
<TD>'.($lines[7]).'</td>
<TD>'.($lines[8]).'</td>
<TD>'.($lines[9]).'</td>
<TD>'.($lines[10]).'</td>
<TD>'.($lines[11]).'</td>
<TD>'.($lines[12]).'</td>
</tr>
<!-- And So On, And So On, ETC -->
</table>'
?>
<?php
echo '<table border="1"><tr>';
for($i=0; $i<sizeof($lines); $i++) {
echo '<td>'.$lines[$i].'</td>';
if(($i+1)%6==0 && $i!=sizeof($lines)-1) echo '</tr><tr>';
}
echo '</tr></table>';
?>
Explanation:
Repeats through each "line" and writes the <td>value</td>
If a line is a multiple of 6, after writing the value, then close the row, and open another (unless it's the last one, since it will close the row after the loop as well.
(I assume you meant to start on $line[0], but if you really meant to start on $line[1], just change the $i=0; to $i=1;, remove the +1 in the row check, and change $i<sizeof to $i<=sizeof
$lines = chunk_split($lines,6);
?>
<TABLE border="1">
<? foreach ($lines as $row): ?>
<TR>
<? foreach ($row as $value): ?>
<TD><?=$value?></td>
<? endforeach ?>
<TR>
<? endforeach ?>
</TABLE>
In pseudo code...
x = 0
echo '<TABLE border="1">'
For each $line in $lines {
x = x + 1
if x = 1 {
echo '<TR>'
}
echo <TD>'.($line).'</TD>
if x = 6 {
echo '</TR>'
x = 0
}
}
echo '</TABLE>'
Note the use of the $line object to hold the value of the $line array element.
hth
If you use a loop, you can also avoid reading the complete file into memory, which might be beneficial:
<?php
$item = #fopen('linklist.txt', "r");
if ($item) {
echo'<TABLE border="1">';
$i=0;
$lines=array();
while (!feof($item)) {
$line[] = fgets($item, 4096);
$i++;
if ($i==6) {
echo "<tr>";
echo '<TD>'.($lines[0]).'</td>';
echo '<TD>'.($lines[1]).'</td>';
echo '<TD>'.($lines[2]).'</td>';
echo '<TD>'.($lines[3]).'</td>';
echo '<TD>'.($lines[4]).'</td>';
echo '<TD>'.($lines[5]).'</td>';
echo "<tr>";
$i=0;
$lines=array();
}
}
echo '</table>';
fclose($item);
}
?>
Related
I have been googling like crazy to find an answer to what I thought would have been a very common question to no avail...
I am using "\n" to break a table into a more readable structure when viewing source. Is there any way to get it to respect current indentation levels? For example:
Turn this:
<table>
<tbody>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</tbody>
</table>
Into this:
<table>
<tbody>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</tbody>
</table>
I am printing the tbody rows and cells out with the following code:
<?php
$first = true;
foreach ($data->rows as $row) {
if ($first) {
print "<tr class='highlight'>";
$first = false;
} else {
print "<tr>";
}
foreach ($row as $cell) {
print "<td>";
printf('%s', $cell);
print "</td>\n";
}
print "</tr>\n";
}
?>
<?php
$first = true;
foreach ($data->rows as $row) {
if ($first) {
echo "\t\t<tr class='highlight'>";
$first = false;
} else {
print "\t\t<tr>";
}
foreach ($row as $cell) {
print "\t\t\t<td>";
printf("\t\t\t\t%s", $cell);
print "</td>\n";
}
print "\t\t</tr>\n";
}
?>
I recommend you to write an identation counter, if you would like to do it dynamically. And remember to use double quotes for escape sequences, single quotes won´t work here (because it´s treated like a variable).
This is what google gave me.
You should concatinate the entire table in one php string, parse it through the function that can be found here:
http://www.daveperrett.com/articles/2007/04/05/format-xml-with-php/
and then simply echo $xml;
I just found it, I give no guarantee that this works as itended but it looks reliable.
The final HTML source have not to respect an indentation... Nobody see it (or you but don't get crazy with this).
Ok, sometimes this is also an obsession for me (one among many others ?), so you can do:
echo '
<tr>';
//Loop starts
echo '
<td></td>';
// Loop ends
echo '
</tr>';
Here you get a newline and your HTML indentation is independant from the PHP one.
Is it what you would ?
I'm importing CSV files into my database. I want to display the first 5 rows with columns, so I can get an idea of what's inside the file.
So below I crafted a quick script to open the file. I count the number of columns, loop through the number of columns, create for each, and then I throw a while() loop below that, to display the rows. Of course, I loop through the number of columns to match.
I'm successfully getting data from the file, and I'm even getting the correct number of columns. But it's displaying result from somewhere in the middle of the file, rather than the first 5 records.
The first row helps me know what the columns are called, (if there are column names).
The script is dirty, and I'm curious how it can be optimized.
<?
$filerow = $numcols =0;
if(is_file($targetDirectoryFile)){
$file_opened = fopen($targetDirectoryFile, "r"); // open the file
?>
<table class="itemstable" style="width:100%;">
<?
$getfileInfo = fgetcsv($file_opened);
$numcols = count($getfileInfo); // count columns
?>
<tr>
<? for($cols = 0; $cols < $numcols; $cols++ ){ // loop thru # of columns ?>
<th>Column <?=$cols+1;?></th>
<? } ?>
</tr>
<?
// feels like this could be done better.
while ($getfileInfo2 = fgetcsv($file_opened)){
$filerow++;
?>
<tr>
<? for($col = 0; $col < $numcols; $col++ ){ // loop thru # of columns ?>
<td><?=trim($getfileInfo2[$col]);?></td>
<? } ?>
</tr>
<?
if($filerow > 4){ break; fclose($file_opened); // stop and close after 5 loops }
}
echo '</table>';
//fclose($file_opened);
}
?>
As to optimization, you can get rid of the if statement in your loop by just using:
while ( ($getfileInfo2 = fgetcsv($file_opened)) && ($filerow < 5) )
{
I also don't think your file gets closed correctly as you put the statement after the break statement, but I don't see how that could lead to the problem you are having.
About the output from the middle; what do the input file and the generated html look like?
I'm trying to make Drupal print a list with two different fields in the same array. So first comes field A, then field B and it prints in this way until the whole array is printed.
The result I'm trying to get is something like
<tr>
<td>Field_1[value1]</td>
<td>Field_2[value1]</td>
</tr><tr>
<td>Field_1[**value'n'**]</td>
<td>Field_2[**value'n'**]</td>
</tr>
Until all values are printed.
EDIT.
Figured out one way of achieving this directly in node--testnode.tpl.php.
<table>
<?php if ($content['field_test'][1]): ?>
<tr><td><?php print render($content['field_test'][0])?></td><td><?php print render($content['field_test'][1])?></td></tr>
<?php endif; ?>
<?php if ($content['field_test'][3]): ?>
<tr><td><?php print render($content['field_test'][2])?></td><td><?php print render($content['field_test'][3])?></td></tr>
<?php endif; ?>
<?php if ($content['field_test'][5]): ?>
<tr><td><?php print render($content['field_test'][4])?></td><td><?php print render($content['field_test'][5])?></td></tr>
<?php endif; ?>
</table>
Second fix, only manual work is to say how many repetitions you want.
<dl class="My Class">
<?php
$i = 0;
$counter = 2 * render ($content['field_counter_slides'][0]) -1;
while ($i <= $counter):
if ($content['field_test'][1]){
echo "<dt>";
print render ($content['field_test'][$i]);
$i++;
echo "</dt><dd>";
print render ($content['field_test'][$i]);
echo "</dd>";
$i++;
}
endwhile;
?>
</dl>
It's quite hard to answer without knowing more but something like this would probably do it:
$header = array();
$rows = array();
foreach ($node->field_my_field[$langcode] as $key => $field_entry) {
$rows[] = array(
$field_entry['value'],
$node->field_my_second_field[$langcode][$key]['value']
);
}
$output = theme('table', array('header' => $header, 'rows' => $rows));
You'd have to make sure yourself that there are equal numbers of entries for each field so that there will always be a field_my_second_field entry for each run through the foreach loop.
how can a list of values that select of database, put in two columns together by PHP ?
EXAMPLE:
values select of database:
Internet
Game Notes
Internet
Pool
Coffee
Game Notes
i want like this:
Row-first order
<table>
<?php
$left = true;
foreach ($values as $value){
if ($left)
echo "<tr>";
echo "<td>$value</td>";
if (!$left)
echo "</tr>";
$left = !$left;
}
?>
</table>
With column-first order (as in your sample) you'll have to involve CSS and it's much complex. Something like
<div class='inline_div'>
<?php
$middle = count($values)/2+1;
$count = 0;
foreach ($values as $value){
if ($count==$middle)
echo "</div><div class='inline_div'>";
echo "$value<br/>";
++$count;
}
?>
</div>
inline_div is something like .inline_div {display:inline; float:left}. But that will definitely not work as expected, I'm no CSS master. IE does not support display:inline for sure.
If you want it exactly like you want in your example then you are going to have to read the whole table into memory, calculate the mid-point element and then build your table from that using a base point, an offset and a check to ensure that you've not repeated anything which involves a whole load of calculation.
A way around this would be to build two separate tables (each containing a single column) and then enclose them in a single table with two columns:
<?php
$list=array('a','b','c','d','e','f');
$midpoint=floor(count($list)/2);
$tableHeader='<table width="100%">';
$tableFooter='</table>';
$leftTable=$tableHeader;
$rightTable=$tableHeader;
for ($c=0; $c<$midpoint; $c++)
{
$leftTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$leftTable.=$tableFooter;
for ($c=$midpoint; $c<count($list); $c++)
{
$rightTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$rightTable.=$tableFooter;
$mainTable='<table><tr><td width="50%">'.$leftTable.'</td><td width="50%">'.$rightTable.'</td></tr></table>';
echo $mainTable;
?>
Or something along those lines. I haven't tested this code but it would be pretty close (may have to adjust the values in the "for" sections
The simple solution would be to use two divs. As a previous poster commented, you first need to count the elements. Suppose the items you want to display are in an array $items You can use this kind of code
<?php
$divItemCount = (count($items)%2) ? count($items)/2 + 1 : count($items)/2;
?>
<div id="leftdiv" style="width: 30%;">
<ul>
<?php
for($i=0; $i<$divItemCount; $i++) {
echo '<li>'. $items[$i] .'</li>';
}?>
</ul></div><div id="rightdiv" style="width: 30%; float:left"><ul>
<?php
for($j=$i; $j<count($items); $j++) {
echo '<li>'. $items[$j] .'</li>';
}?>
</ul></div>
I have not tested the code so there may be errors. You can use the border property in the divs to create a custom separator between the two lists.
I need to make an array in php that will output an html table so I can grab the source.
##id:1## ##site:1##
##id:2## ##site:2##
etc. 500 times over.
<table>
<?
for($i = 0; $i < 500; ++$i) {
echo '<tr><td>',$i,'</td><td>',$i,'</td></tr>';
} ?>
</table>
people are really lazy these days …
i dont really know what u need that for but here is my solution (not tested)
$table_array=array(
{id}=>'{text}',
{id2}=>'{text2}'
);
echo '<table>';
foreach($id=>$row in $table_array){
echo '<tr id="'.$id.'">';
echo '<td>'.$row.'</td>';
echo '</tr>';
}
echo '</table>';
You just have to generate the Array with the right text and id, then it should work fine.