|
Posted by Steve on 01/22/06 00:56
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> </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";
?>
Navigation:
[Reply to this message]
|