Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text being truncated / How to create multiple pages??

Status
Not open for further replies.

Programz8

Programmer
Mar 27, 2007
33
US
How are you guys? I'm trying to display some images with text under them and for some reason only the first letter of text appears and the rest of the line is truncated? Does anyone know what the problem is? Also the image file name appears on the page. And finally does anyone know how to modify my code to allow for multiple pages of images after the first page is full?

Here is the code from the text file and the php file.

PHP file:

<?php

$images = "image_gallery/"; # Location of small versions
$big = "big/"; # Location of big versions (assumed to be a subdir of above)
$cols = 2; # Number of columns to display
$text = "text.txt";

$fh=fopen($text, "r");

while(!feof($fh))
{
$temp = explode(",", $line);
$description[$temp[0]] = $temp[1];
$line=fgets($fh);
unset($temp);
}

if ($handle = opendir($images)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != rtrim($big,"/")) {
$files[] = $file;
}
}
closedir($handle);
}

$colCtr = 0;

echo '<table width="100%" cellspacing="3"><tr>';

foreach($files as $file)
{
if($colCtr %$cols == 0)
echo '</tr><tr><td colspan="' . $cols . '"><hr /></td></tr><tr>';
$allfiles .= $file . ',<br />';
echo '<td align="center"><a href="' . $images . $big . $file . '"><img src="' . $images . $file . '" /></a><br />' . $description[$file][0] . '</td>';
$colCtr++;
}

echo '</table>' . "\r\n";
echo $allfiles;
?>


and now for the text file text.txt

camronxxl.jpg,Balloon Fiesta!
anna_nicole.jpg,Some Anna Nicole Text
dontcare.jpg,My little ghoulish helpers
beyonce.jpg,Our Halloween Pumpkin

 
You have a logical error in this section of code:

[tt]$fh=fopen($text, "r");

while(!feof($fh))
{
$temp = explode(",", $line);
$description[$temp[0]] = $temp[1];
$line=fgets($fh);
unset($temp);
} [/tt]

the first time your script runs through that loop, it will attempt to use $line before it any value has been put in it.

One way to rewrite it is:

[tt]$fh=fopen($text, "r");

$description = array();

while($line = fgets($fh))
{
$temp = explode(",", $line);
$description[$temp[0]] = $temp[1];
} [/tt]



Also, this line:

[tt]foreach($files as $file) [/tt]

uses $files without initializing it.


This probably hasn't, though, caused the error you've reported.



Want the best answers? Ask the best questions! TANSTAAFL!
 
could you not simplify your code?
Code:
<?php 

$imageDir = "image_gallery/"; # Location of small versions 
$big    = "big/"; # Location of big versions (assumed to be a subdir of above) 
$text   = "text.txt"; 

$fh=fopen($text, "rb"); 

while(!feof($fh)) { 
  $line = fgets($fh);
  $images[] = explode(",", $line); 
} 
echo <<<HTML
<style type="text/css">
.floated {font-family:Verdana, Arial, Helvetica, sans-serif; font-size:9px; color:#000066; margin: 2px; width:100px; height: 150px; background-color:#FEF8E2; float:left; text-align:center;}
</style>

HTML;

foreach ($image as $image){
	if (file_exists($imageDir.$image[0])){
		echo <<<HTML
<div class="floated">
	<a href="{$imageDir}{$big}{$image[0]}">
		<img src="{$imageDir}{$image[0]}">
	</a><br/>
	{$image[1]}
</div>

HTML;
	} //end the internal if
} //end the foreach
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top