Re: Loops all messed up



paul wrote:
I've been struggling with this for days. It prints all the images in a folder followed by annotation from a text file in the folder with matching name.txt and allows editing of those text files. The problem is the second one gathers annotation from the first & when editing, it also applies down one & doesn't update the current one till refreshed again.

Anyways I can't see what the heck is going wrong, I've looked at it too long. Here is this simplified version as it runs on the server:
http://www.triteleia.com/plants/Adiantum-jordanii/

Sure enough I found most of the problem right after posting. I was repeating the comment[] setting part twice. It's still not updating with a refreshed comment after changes though. I'll upload the latest version (and probably figure it out in a minute<g>):


<?php
$imagedir = getcwd();
$key = 0;
$fh=opendir($imagedir);
# process folder
while ($file = readdir($fh)){
  if (ereg('.jpg|.JPG.|.jpeg|.JPEG|.gif|.GIF|.png|.PNG', $file)){
      $basename = substr($file, 0, (strrpos($file, ".jpg")));
      if (file_exists ("$basename.txt")){
         $comments[] = file_get_contents("$basename.txt");
      } else {$comments[] = "";}
      if (isset ($_POST["comment"])){
        if ($key == $_POST["count"]){
          $handle = fopen ("$basename.txt", "w");
          $comment = stripslashes($_POST["comment"]);
          fwrite($handle, $comment);
          fclose ($handle);
          unset ($_POST["comment"]);
        }
      }
        $pic_name[] = $file;
        $getimagesize[] = getimagesize($file);
        $imagesize[] = $getimagesize[0][3];
        unset ($getimagesize);
        $key++;
  }# if a jpeg
}# while read dir

###### begin printing #######
$cnt= 0;
while ($cnt < $key){
  $image = $pic_name[$cnt];
  $basename = substr($image, 0, (strrpos($image, ".jpg")));
  print "<center><img src=\""
        .$pic_name[$cnt]."\""
        .$imagesize[$cnt]
        ." title=\"".$pic_name[$cnt]."\"";
  print "\"><br></center>";
  if (isset ($_POST["comment"])){
    if ($cnt == $_POST["count"]){
      $comment = $_POST["comment"];
    }
  } else {
    $comment = $comments[$cnt];
  }
  print "<br>" . $comment. "<br><br>";
  ?>
    <form method="post">
      <input type="hidden" name="edit_comment">
        <input type="hidden" name="count"
          <?
          print "value=\"" . $cnt . "\"";
          ?>
          >
      <button type="submit">edit comment</button>
    </form>
  <?
  if (isset ($_POST["edit_comment"])){
  if ($cnt == $_POST["count"]){
  ?>
      <form method="post">
        <?# <input type="hidden" name="edit_flag2">
        ?>
        <input type="hidden" name="count"
          <?
          print "value=\"" . $cnt . "\"";
          ?>
        <textarea name="comment" ROWS=6 COLS=60>
       <?
          if (isset ($comment)){
            print $comment;
          }
       ?>
       </textarea><br>
       <button type="submit">submit changes</button>
      </form>
    <?php
    unset ($_POST["edit_comment"]);
    unset ($_POST["count"]);
   }# if count matches
  }# gather new comments
  $cnt++;
} # end foreach processing image
?>
.


Quantcast