469,945 Members | 2,249 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Need help with my code reading flat file into HTML table

I need a little guidance putting a script together. I'm trying to read a
list of image links from a text file (not a database) and display them
in a table on my page. I want to display them in rows of four. I can get
my script to work to display the images but I can't figure out how to do
two things:
1. How do I get only one image into each table data entry?
2. How do I get it to create blank table data entries for any amount
less than 4 left at the end? (Example: there are 9 images and I want it
in the last table row to put the one image remaining and three blank
table data entries)

Here's what I've got so far:

$file = "links.txt";
$result = file_get_contents("links.txt");
$lines = count(file("$file"));

$tdcount = 1;
$numtd = 4; // number of cells per row
echo "<table border=2>";

$arr=array($result);
foreach ($arr as $value)
{
if ($tdcount == 1) echo "<tr>";
echo "$value";
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
} // close up the table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";
?>
When I echo $result and $lines at the top and exit the script, they both
show as they should so the file is being read properly. When I echo
$arr, all I get is the word "array". And when I run the script, the 9
images appear in rows of 4, 4, and 1 which is good but the table appears
below the image links, not containing them as I want.

Can anyone show me the error of my ways? It's probably something dumb
but I've worked on this for two days now trying all kinds of variations
and can't get it to work. Thanks for any help.
Jan 21 '06 #1
5 1823
Message-ID: <Tv********************@comcast.com> from JackM contained
the following:
Can anyone show me the error of my ways? It's probably something dumb
but I've worked on this for two days now trying all kinds of variations
and can't get it to work. Thanks for any help.

<?php
$file = "file.txt";
$cols=4;
$pos=0;
$result = file($file);
$lines = count($result);
$spaces=($lines%$cols>0)? 1 : 0;
$rows=floor($lines/$cols)+$spaces;
echo "<table border='2'>\n";

for($i=0;$i<$rows;$i++){
echo "<tr>\n";
for($j=0;$j<$cols;$j++){
if(isset($result[$pos])){
echo "\t<td>".trim($result[$pos])."</td>\n";
}
else{
echo "\t<td>&nbsp;</td>\n";
}
$pos++;
}
echo "</tr>\n";
}
echo "</table>";
?>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jan 21 '06 #2
Geoff Berrow wrote:
Message-ID: <Tv********************@comcast.com> from JackM contained
the following:

Can anyone show me the error of my ways? It's probably something dumb
but I've worked on this for two days now trying all kinds of variations
and can't get it to work. Thanks for any help.

<snip Geoff's code that worked>

Thanks Geoff. Does the fact that you rewrote nearly the entire code mean
that I had butchered it completely?

I can follow most of what you did but there are two lines that are
foreign to me:

$spaces=($lines%$cols>0)? 1 : 0;
$rows=floor($lines/$cols)+$spaces;

Can you give me a quick idea of what those do? They are a little beyond
my PHP vocabulary right now. Thanks for your help.
Jan 21 '06 #3
Message-ID: <bp******************************@comcast.com> from JackM
contained the following:
<snip Geoff's code that worked>

Thanks Geoff. Does the fact that you rewrote nearly the entire code mean
that I had butchered it completely?
Heh... dunno really, I've coded similar things before and usually find
nested loops work well. No disrespect to your code, but it was easier
to knock out something I'd done before than try to work through your
logic.
I can follow most of what you did but there are two lines that are
foreign to me:

$spaces=($lines%$cols>0)? 1 : 0; The question mark is the ternary operator. It's a shorter way of
writing if/else. '$lines%$cols' divides $lines by $cols and gives the
remainder. If there is a remainder the result will be greater than 0 and
$spaces is set to 1. If there is no remainder it is set to zero.

I could have written

if($lines%$cols>0){
$spaces= 1;
}
else{
$spaces=0;
}
$rows=floor($lines/$cols)+$spaces; If we divide $lines by $cols we get the number of rows. The function
floor just rounds it down. So if you have 9 lines and 4 columns the
answer would be two. In fact the answer is two until you have 12 lines.
However, if it doesn't divide exactly you need another row. That being
the case, $spaces is already set to one and so we have the correct
number.
Can you give me a quick idea of what those do? They are a little beyond
my PHP vocabulary right now. Thanks for your help.


No problem.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jan 21 '06 #4
On Sat, 21 Jan 2006 12:23:50 -0500, JackM wrote:
I need a little guidance putting a script together. I'm trying to read a
list of image links from a text file (not a database) and display them
in a table on my page. I want to display them in rows of four. I can get
my script to work to display the images but I can't figure out how to do
two things:
1. How do I get only one image into each table data entry?
2. How do I get it to create blank table data entries for any amount
less than 4 left at the end? (Example: there are 9 images and I want it
in the last table row to put the one image remaining and three blank
table data entries)

Here's what I've got so far:

$file = "links.txt";
$result = file_get_contents("links.txt");
$lines = count(file("$file"));

$tdcount = 1;
$numtd = 4; // number of cells per row
echo "<table border=2>";

$arr=array($result);
foreach ($arr as $value)
{
if ($tdcount == 1) echo "<tr>";
echo "$value";
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
} // close up the table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";
?>
When I echo $result and $lines at the top and exit the script, they both
show as they should so the file is being read properly. When I echo
$arr, all I get is the word "array". And when I run the script, the 9
images appear in rows of 4, 4, and 1 which is good but the table appears
below the image links, not containing them as I want.

Can anyone show me the error of my ways? It's probably something dumb
but I've worked on this for two days now trying all kinds of variations
and can't get it to work. Thanks for any help.


This should work, but it untested. You don't need to worry about blank
entries in a table row, unless you want to try and centre them.

$file = "links.txt";
$result = file_get_contents("links.txt"); $lines = count(file("$file"));

$tdcount = 0;
$numtd = 4; // number of cells per row
echo "<table border=2>\n <tr>\n";

$arr=array($result);
foreach ($arr as $value)
{
// End of row if count mod numtd == 0, but ignore the first time round.
if ($tdcount && !($tdcount % $numtd) )
{
echo " </tr>\n <tr>";
}
echo " <td>$value</td>\n;
} // close up the table
echo " </tr>\n</table>\n";
?>
Jan 21 '06 #5
Geoff Berrow wrote:
Message-ID: <bp******************************@comcast.com> from JackM
contained the following:

I can follow most of what you did but there are two lines that are
foreign to me:

$spaces=($lines%$cols>0)? 1 : 0;


The question mark is the ternary operator. It's a shorter way of
writing if/else. '$lines%$cols' divides $lines by $cols and gives the
remainder. If there is a remainder the result will be greater than 0 and
$spaces is set to 1. If there is no remainder it is set to zero.

I could have written

if($lines%$cols>0){
$spaces= 1;
}
else{
$spaces=0;
}

$rows=floor($lines/$cols)+$spaces;


If we divide $lines by $cols we get the number of rows. The function
floor just rounds it down. So if you have 9 lines and 4 columns the
answer would be two. In fact the answer is two until you have 12 lines.
However, if it doesn't divide exactly you need another row. That being
the case, $spaces is already set to one and so we have the correct
number.


Thanks for the explanations. Caught me off guard because I never came
across the ? or floor in code before. Not only did you solve my problem
but you taught me something new at the same time. Your explanations are
now crystal clear to me. Much obliged.
Jan 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by lawrence | last post: by
1 post views Thread by terry | last post: by
3 posts views Thread by Robert Dell | last post: by
9 posts views Thread by terry | last post: by
6 posts views Thread by KevinD | last post: by
8 posts views Thread by knoxautoguy | last post: by
reply views Thread by VBLearner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.