str_replace?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eggoz
Forum Commoner
Posts: 43
Joined: Fri Dec 27, 2002 8:51 pm

str_replace?

Post by eggoz »

I am working on a picture gallery and I have ran into a small problem. I admit, I took part of this code from a book and modified it. Now I having trouble with it and it's twice as hard to fix.

http://php.imagiro-1.com/Pictures You can see what I have so far. When you click on a link, it should show you thumbnails of the pictures. And some pages have errors.

And here my code:

Code: Select all

<html>
<head><title>Process all files</title></head>
<body>
<?php
$exit_test = "$exit";
//echo $exit;
$full_name = "$exit_test";
$dir_new = basename($full_name);
//session_register ("dir_new");
print "<h1>$dir_new</h1><br><hr>";
//echo $dir_final;
//Open Dir
//$dir = opendir('./') or die($php_errormsg);
$dir = opendir($exit_test) or die($php_errormsg);
        while (false !== ($files =readdir($dir))) {
	if ('.' == ($files)) {
		continue;
	}
	if ('..' == ($files)) {
		continue;
	}
	if (is_file($files)) {
		continue;
	}
        $file[$x++] = $files;
       }
closedir($dir);

foreach($file as $filenum => $filename)
{
	//print "$filenum  $filename <p>\n";

}


//Create Array 15
$files = array();

//Create Table
function pc_grid_horizontal($array, $size, $dir_final) {

$table_width = 100;
$width = intval($table_width / $size);

$tr = '<tr align="center">';
$td = "<td width="$width%%"><a href="pictures/Olympus Camedia/$dir_final/%s" target="window"><img="pictures/Olympus Camedia/$dir_final/%s"><br>pic</a></td>";

$grid = "<table width="$table_width%" style="table_style">$tr";

$i = 0;
foreach ($array as $e) {
	$grid .= str_replace('%s', $e, $td);
	$i++;

	if (!($i % $size)) {
        	$grid .= "</tr>$tr";
        }
}

while ($i % $size) {
        $grid .= sprintf($td, '&nbsp;');
        $i++;
}

$end_tr_len = strlen($tr) * -1;
if (substr($grid, $end_tr_len) != $tr) {
        $grid .= '</tr>';
} else {
        $grid = substr($grid, 0, $end_tr_len);
}

$grid .= '</table>';

return $grid;
}

$grid = pc_grid_horizontal($file, 5, $dir_new);

print $grid;

?>


?>
This all started when I changed the str_replace. Before it was sprintf, but I need to replace all the %s with $e. Where am I going wrong?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$td as written, is intended for the *printf() functions.

change

Code: Select all

str_replace('%s', $e, $td);
to

Code: Select all

sprintf($td,$e,$e);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

btw
this is an executable.

gorgeous cat! :)
eggoz
Forum Commoner
Posts: 43
Joined: Fri Dec 27, 2002 8:51 pm

Post by eggoz »

I think I had tried that before. Still doesn't work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well.. hmm.. you can always do:

Code: Select all

$grid .= "<td width="$width%"><a href="pictures/Olympus Camedia/$dir_final/$e" target="window"><img="pictures/Olympus Camedia/$dir_final/$e"><br>pic</a></td>";
Post Reply