Forum Moderators: coopster

Message Too Old, No Replies

Looping through string and putting result in a table

         

cookie2

8:08 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



I need a little guidance putting a script together. I'm trying to read a list of images from a text file 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 written so far:

template.inc

<tr>
<td><? echo $result;?></td>
<td><? echo $result;?></td>
<td><? echo $result;?></td>
<td><? echo $result;?></td>
</tr>

test.php

<?php
$result = file_get_contents("image.txt");
?>
<table border="2">
<?php
for ($i=0, $n=sizeof($result); $i<$n; $i++) {
include("template.inc");
}
?>
</table>

image.txt
<A href="Aticket-73.jpg"><img src="./thumbnails/tn_Aticket-73.jpg" width="152" height="71" alt="A ticket 1973"></a>
<A href="Aticket.jpg"><img src="./thumbnails/tn_Aticket.jpg" width="152" height="75" alt="A ticket"></a>
<A href="Bticket-73.jpg"><img src="./thumbnails/tn_Bticket-73.jpg" width="152" height="71" alt="B ticket 1973"></a>
<A href="Bticket.jpg"><img src="./thumbnails/tn_Bticket.jpg" width="152" height="71" alt="B ticket"></a>
<A href="Cticket-73.jpg"><img src="./thumbnails/tn_Cticket-73.jpg" width="152" height="71" alt="C ticket 1973"></a>
<A href="Cticket.jpg"><img src="./thumbnails/tn_Cticket.jpg" width="152" height="87" alt="C ticket"></a>
<A href="Dticket-73.jpg"><img src="./thumbnails/tn_Dticket-73.jpg" width="152" height="71" alt="D ticket 1973"></a>
<A href="Dticket.jpg"><img src="./thumbnails/tn_Dticket.jpg" width="152" height="93" alt="D ticket"></a>
<A href="Eticket-73.jpg"><img src="./thumbnails/tn_Eticket-73.jpg" width="152" height="71" alt="E ticket 1973"></a>

Can anyone help me? Thanks.

jatar_k

8:45 pm on Jan 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe this thread will help
[webmasterworld.com...]

cookie2

12:25 am on Jan 21, 2006 (gmt 0)

10+ Year Member



Well, I read the thread and came up with this. It reads the text file and outputs it 4 to a line but it doesn't put it in a table form. What needs to be changed?

<?php
$result = file_get_contents("image.txt");

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

$arr=array($result);
foreach ($arr as $value)
{
if ($tdcount == 1) echo "<tr>";
echo "$value"; // display as you like
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
} // time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";
?>

cookie2

8:58 pm on Jan 21, 2006 (gmt 0)

10+ Year Member



In case anyone is following this thread for their own use, I got the code to work with the help of some friends. The following is what they came up with:

<?php
$file = "filename.txt"; //specify your filename
$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' width='100%'>\n";

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

jatar_k

5:39 pm on Jan 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice work cookie2