Jump to content

PHP fwrite thumbnail gallery sorting


imperium2335

Recommended Posts

Hi, I am trying to make something that updates galleries when a new thumbnail is added, and if the last gallery is full it creates a new gallery page.

 

This is no problem, but I would like it to move every image down one space and add the new thumbnail on the first page. I'm finding this quite baffling.

 

First it explodes all the content of each gallery, and saves the code for each thumbnail into a variable, or would it be better to use arrays?

 

Then I want it to write all the information back into the files with the new thumb on the first gallery page, but with all the others "pushed along" by one (there are 9 thumbs per page).

 

Here is the code, the last bit is wrong but i have left it in to help you see what I'm trying to do:

while($currentfile = readdir($openzone)) {

$parts = explode(".", $currentfile);
if (is_array($parts) && count($parts) > 1)
    $ext = end($parts);//Check for files without extensions

if(stristr($currentfile, "home-made-cakes") && $ext != NULL) {
	array_push($gallerys, $currentfile) ;
	$galcount ++ ;
	echo "Counted $galcount galleries. $currentfile<br />" ;
}
$ext = NULL ;
sort($gallerys) ;
}
//Create thumbnail html template...
$newthumb = "<div id=\"thumb\"><a href=\"home-made-cakes/$pagename\"><img src=\"$thumbloc\" alt=\"homemade cake\" /></a><p><a href=\"home-made-cakes/$pagename\">$cakename</a></p><p><!--Awaiting Text--></p></div>" ;

//Check each gallery too see how many pictures they have each.
for($i = 0; $i < $galcount; $i ++) {
$handle = fopen("../" . $gallerys[$i], "r") ;
$galcontents = fread($handle, filesize("../" . $gallerys[$i])) ;
$thumbcount = preg_match_all("<div id=\"thumb\">", $galcontents, $cheese) ;
fclose($handle) ;
$totalthumbs += $thumbcount ;
echo "<br />there are $thumbcount thumbs in $gallerys[$i] total being $totalthumbs<br />" ;

}
if(($thumbcount / $galcount) >= 9){ //Are the existing galleries full? If so, make a new gallery page...
for($i = 1; $i < $galcount +2;) {//Find a new name and check if its available.
	$newgal = "home-made-cakes-" . "$i" . ".html" ;
	echo "is now $newgal<br />" ;
	if(in_array($newgal, $gallerys)){echo "increasing i is now $i" ; $i++ ;}
	elseif(!in_array($newgal, $gallerys)){echo "New name found, stopping New gallery is called $newgal" ; $i = 100000000000 ; $galcount++} //Gallery name ready, but gallery not created yet, galcount increased for use in next routine.
}}
else{echo "Gallerys are not full yet" ;}

for($i = 1; $i < $galcount + 1 ; $i++) {
$handle = fopen("../" . $gallerys[$i], "r") ;
$galcontents = fread($handle, filesize("../" . $gallerys[$i])) ;
$thumbcount = preg_match_all("<div id=\"thumb\">", $galcontents, $cheese) ;
for($i = 0; $i < $thumbcount ; $i++) {
	$chunk = explode("<div id=\"thumb\"">, $galcontents) ;
	$thumb1 = explode("<div id=\"thumb\"">, $chunk[1]) ;
	$thumb1 = "<div id=\"thumb\">" . $thumb1[0] ;
	$thumb2 = explode("<div id=\"thumb\"">, $thumb1[1]) ;
	$thumb2 = explode("<div id=\"thumb\"">, $thumb2[0]) ;
	$thumb2 = "<div id=\"thumb\">" . $thumb2[0] ;
	$thumb3 = explode("<div id=\"thumb\"">, $thumb2[1]) ;
	$thumb3 = explode("<div id=\"thumb\"">, $thumb3[0]) ;
	$thumb3 = "<div id=\"thumb\">" . $thumb3[0] ;
	$thumb4 = explode("<div id=\"thumb\"">, $thumb3[1]) ;
	$thumb4 = explode("<div id=\"thumb\"">, $thumb4[0]) ;
	$thumb4 = "<div id=\"thumb\">" . $thumb4[0] ;
	$thumb5 = explode("<div id=\"thumb\"">, $thumb4[1]) ;
	$thumb5 = explode("<div id=\"thumb\"">, $thumb5[0]) ;
	$thumb5 = "<div id=\"thumb\">" . $thumb5[0] ;
	$thumb6 = explode("<div id=\"thumb\"">, $thumb5[1]) ;
	$thumb6 = explode("<div id=\"thumb\"">, $thumb6[0]) ;
	$thumb6 = "<div id=\"thumb\">" . $thumb6[0] ;
	$thumb7 = explode("<div id=\"thumb\"">, $thumb6[1]) ;
	$thumb7 = explode("<div id=\"thumb\"">, $thumb7[0]) ;
	$thumb7 = "<div id=\"thumb\">" . $thumb7[0] ;
	$thumb8 = explode("<div id=\"thumb\"">, $thumb7[1]) ;
	$thumb8 = explode("<div id=\"thumb\"">, $thumb8[0]) ;
	$thumb8 = "<div id=\"thumb\">" . $thumb8[0] ;
	$thumb9 = explode("<div id=\"thumb\"">, $thumb8[1]) ;
	$thumb9 = explode("<div id=\"thumb\"">, $thumb9[0]) ;
	$thumb9 = "<div id=\"thumb\">" . $thumb9[0] ;

Link to comment
https://forums.phpfreaks.com/topic/176529-php-fwrite-thumbnail-gallery-sorting/
Share on other sites

Hi, thanks for your reply. I used arrays like you said and did this, its untested at the mo:

for($i = 1; $i < $galcount + 1 ; $i++) {
$handle = fopen("../" . $gallerys[$i], "r+") ;
$galcontents = fread($handle, filesize("../" . $gallerys[$i])) ;
$thumbcount = preg_match_all("<div id=\"thumb\">", $galcontents, $cheese) ;
$header = explode("<!--XR1112-->", $galcontents) ;
$header = $header[0] ;
for($x = 0; $i < $thumbcount ; $x++) {

	${thumb.$x} = explode("<div id=\"thumb\"">, $galcontents) ;
	${thumb.$x} = explode("<div id=\"thumb\"">, ${thumb.$x}[1]) ;
	${thumb.$x} = "<div id=\"thumb\">" . ${thumb.$x}[0] ;

	array_push($thumbbuild, ${thumb.$x}) ;
}
if($i == 1) {//If its the first gallery...
	array_reverse($thumbbuild) ;
	array_push($thumbbuild, $newthumb) ;
	array_reverse($thumbbuild) ; //New thumbnail is now at the start.
}
else{
	array_reverse($thumbbuild) ;
	array_push($thumbbuild, $carryover) ;
	array_reverse($thumbbuild) ; //carried over thumbnail is now at the start.
}
	//Pop off the last array thumb, remember it if the gallery has 9 thumbs, because we will add it to the start of the next gallery.
	if(count($thumbbuild == 10)) { //If our gallery has overflowed...
	$carryover = array_slice($thumbbuild, 9) ; //Cut off the last array item (last thumb).
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.