www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Looping issue

  1. #1
    Join Date
    Jan 2014
    Location
    Boston, Massachusetts
    Posts
    64

    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:
    Code:
    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)
    NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
    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
    Code:
    echo "Nice one! You caught a " . $fish[$rand] . "!";
    but if nothing is caught it would say
    Code:
    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:

    Code:
        <div class="container">
          <div class="starter-template">
            <h2>Catch a Fish!</h2>
            
            <p class="fishes">
              <form action="fishing.php" method="post">
              <?php
    
                $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;
                  //var_dump($filename);
                  //var_dump($filepath);
                  //var_dump($root_dir);
    
                  // 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>
            <p><input type="submit" class="btn btn-primary" value="Go Fishing!"></p>
            </form>
    
            <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>
            </audio>
    
            <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 09:35 PM. Reason: formatting

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    I like to use glob() to get an array of files. That way you can use its wildcard capability to limit what it picks up, e.g. "*.gif" when getting all the images.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2014
    Location
    Boston, Massachusetts
    Posts
    64
    Cool, I'll try that. Any idea about the other issues?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    If you are talking about detecting the submit input button, that depends on how it is defined in your HTML. If it is within a form and the form is using method="post", then it will be set in $_POST['button_name'], where 'button_name' is whatever value you assigned to the name attribute of the button's input element. If you used a "get" action instead of "post", then it will be in $_GET['button_name'].
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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



Recent Articles