Results 1 to 4 of 4

Thread: [RESOLVED] Looping issue

Threaded View

  1. #1
    Join Date
    Jan 2014
    Boston, Massachusetts

    resolved [RESOLVED] Looping issue

    Okay, so I'm writing a simple fishing game loop but I'm having multiple issues. I have some images in a folder, and I set an array with the filename as key and the type of fish as the value.
    I then set it up to do this:
    echo "Nice one! You caught a " . $fish[$rand] . "!";
    and $rand is picking a random fish from the array.

    First, I need a way to output the image of the fish that goes along with the name. I want this to happen upon the submit input button (which I don't know how to hook up).

    Second, right now I have a loop that outputs each file in the img folder. It's also outputting .DS_Store. I'm trying to exclude that from outputting. I have a foreach loop that is attempting to do just that, but I used var_dump on 3 variables and here's the output:
    A) ($filename)
    string(1) "." string(2) ".." string(9) ".DS_Store" string(11) "catfish.gif" string(10) "lmbass.gif" string(10) "minnow.gif" string(9) "perch.gif" string(12) "pickerel.gif" string(10) "shiner.gif" string(10) "smbass.gif" string(11) "sunfish.gif"

    B) ($filepath)
    string(5) "/img." string(6) "/img.." string(13) "/img.DS_Store" string(15) "/imgcatfish.gif" string(14) "/imglmbass.gif" string(14) "/imgminnow.gif" string(13) "/imgperch.gif" string(16) "/imgpickerel.gif" string(14) "/imgshiner.gif" string(14) "/imgsmbass.gif" string(15) "/imgsunfish.gif"
    The proper filepath should be /img/filename.gif. Also, what are string(5) "/img." and string(6) "/img.."?

    C) ($root_dir)
    Is $root_dir even necessary?

    Third, I want there to be a random chance that they will catch nothing; when a fish is caught, it would say
    echo "Nice one! You caught a " . $fish[$rand] . "!";
    but if nothing is caught it would say
    echo "Uh-oh, looks like that one got away!";
    Last, I have audio that I want to play when a fish is not caught.

    Here's all of my code:

        <div class="container">
          <div class="starter-template">
            <h2>Catch a Fish!</h2>
            <p class="fishes">
              <form action="fishing.php" method="post">
                $fish = array('catfish.gif' => 'catfish', 'lmbass.gif' => 'large-mouth bass', 'smbass.gif' => 'small-mouth bass', 'shiner.gif' => 'shiner', 'perch.gif' => 'perch', 'pickerel.gif' => 'pickerel', 'minnow.gif' => 'minnow', 'sunfish.gif' => 'sunfish');
                $rand = array_rand($fish);
                //link file name (key) to value
                echo "Nice one! You caught a " . $fish[$rand] . "!";
                $files = scandir('img');
                if ($files !== false) {
                    foreach($files as $f) {
                      if ($f == '..' || $f == '.') continue;
                        echo '<li class="fish_pic"><img src="img/'.$f.'" alt="'.$f.'" title="" class="fish"></li>'."\n";
                $files = scandir('img');
                $extensions = array(".txt",".DS_Store");
                // Loop through each filename of scandir
                foreach ($files as $filename) {
                  // Construct a full path
                  $filepath = '/'.'img'.$filename;
                  // Is it a file? If so, get the extension using some function you created
                  if(is_file($filepath)) {
                      $ext = getFileExtension($filename);
                      // Is the file extension not included in the array of forbidden extensions?
                      // Since it is not included, execute code to list the file or whatever
                      if (!in_array($ext,$extensions)) {
                         // Code for files not excluded
            <p><input type="submit" class="btn btn-primary" value="Go Fishing!"></p>
            <audio controls>
              <source src="audio/missed.ogg" type="audio/ogg">
              <source src="audio/missed.mp3" type="audio/mpeg">
              <p>Your browser does not support the audio element.</p>
            <div class="clearfix"></div>
          </div><!-- close .starter-template -->
        </div><!-- /.container -->
    One last note, my folder/file structure are all at the root. So fishing.php and /audio and /css and /img are all on the same level.

    I know this is a lot but I've googled every single issue and tried numerous things and nothing seems to work.

    Any help I could get would be extremely appreciated!
    Last edited by winthropite; 02-05-2014 at 10:35 PM. Reason: formatting

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center