Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Looping issue

  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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?

    --I think using glob() would work to exclude .DS_Store but I'm not sure how to use it.

    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!

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Posts
    454
    Thanks
    0
    Thanked 20 Times in 20 Posts
    1. Since all your fish are in an array you don't need to run through the directory to find them.

    $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');

    The only time you need to scan a dir for something like this is to generate an array list otherwise:

    $fish_location = 'images/';

    //-- add empty space to array for 'nothing caught' --//
    $fish = array('','catfish.gif', 'lmbass.gif', 'smbass.gif', 'shiner.gif', 'perch.gif', 'pickerel.gif', 'minnow.gif', 'sunfish.gif');

    $Caught = $fish[$rand];

    $Output = '';
    if(!empty($Caught)){
    $Output = '<image scr="' .$fish_location.$Caught. '" />';
    }
    else{
    $Output = 'Uh-oh, looks like that one got away!';
    $Output .= '<audio controls>';
    $Output .= '<source src="audio/missed.ogg" type="audio/ogg">';
    $Output .= '<source src="audio/missed.mp3" type="audio/mpeg">';
    $Output .= '<p>Your browser does not support the audio element.</p>';
    $Output .= '</audio>';
    }
    echo $Output;


    I've googled every single issue and tried numerous things and nothing seems to work.
    Simplicity works better when dealing with codes from thousands of tutorials and different coding skills.
    1. Catch fish - show fish
    2. Catch nothing - show 'oops!' and play 'missed' mp3
    3. Catch fish by random selection - use array of selections leaving a blank space so no fish are caught.
    4. Array generation options - hardcode array, dynamic generation by scanning dir and getting array of file names. alt tag name is substring of file name.
    5. Add location for quick access to item images.

    Hope that helps
    Last edited by c1lonewolf; 02-06-2014 at 04:34 PM.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • Users who have thanked c1lonewolf for this post:

    winthropite (02-07-2014)

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you SO MUCH for all of the help. I've adapted some and changed a few things and my main question now is how to play that audio file using jQuery when that if(empty($caught)) { $Output=... is true. So when that's true, or when $Output is echoed, the sound file autoplays.

    Here's the jQuery script; I just need to link the two together.
    Code:
    <script>
    *** $(document).ready(function() {
    ******* var audioElement = document.createElement('audio');
    ******* audioElement.setAttribute('src', 'audio/TEST.mp3');
    ******* audioElement.setAttribute('autoplay', 'autoplay');
    ******* //audioElement.load()
    
    ******* $.get();
    
    ******* audioElement.addEventListener("load", function() {
    *********** audioElement.play();
    ******* }, true);
    
    ******* $('.play').click(function() {
    *********** audioElement.play();
    ******* });
    
    ******* $('.pause').click(function() {
    *********** audioElement.pause();
    ******* });
    *** });
    </script>
    Here's the whole thing, just in case-
    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('' => '', 'coelacanth.gif' => 'coelacanth', 'seadragon.gif' => 'sea dragon', 'rainbow_trout.gif' => 'rainbow trout', 'dolphinfish.gif' => 'dolphinfish', 'catfish.gif' => 'catfish', 'monkfish.gif' => 'monkfish', 'lmbass.gif' => 'large-mouth bass', 'smbass.gif' => 'small-mouth bass', 'shiner.gif' => 'shiner', 'perch.gif' => 'perch', 'frogfish.gif' => 'frogfish', 'pickerel.gif' => 'pickerel', 'minnow.gif' => 'minnow', 'flying_fish.gif' => 'flying fish', 'clownfish.gif' => 'clownfish', 'tropical_fish.gif' => 'tropical fish', 'betta.gif' => 'betta', 'shark.gif' => 'shark', 'octopus.gif' => 'octopus', 'koi.gif' => 'koi', 'sunfish.gif' => 'sunfish');
    *********** $rand = array_rand($fish);
    
    *********** $Caught = $fish[$rand];
    *********** $Output = '';
    *********** if(empty($Caught)){
    ************* $Output = 'Uh-oh, looks like that one got away!';
    ************* echo $Output;
    *********** } else {
    ************* echo '<li class="fish_pic"><img src="img/'.$rand.'" alt="'.$fish[$rand].'" title="'.$fish[$rand].'" class="fish"></li>'."\n";
    ************* echo '<p class="caught">' . "Nice one! You caught a " . $fish[$rand] . "!</p>";
    *********** }
    ********* ?>
    ******* </p>
    ********* <p><input type="submit" class="btn btn-primary" value="Go Fishing!"></p>
    ********* </form>
    
    ********* <div class="btn btn-primary play">Play</div>
    ********* <div class="btn btn-primary pause">Stop</div>
    
    ******* <div class="clearfix"></div>
    ***** </div><!-- close .starter-template -->
    *** </div><!-- /.container -->
    
    *** <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    *** <script src="https://code.jquery.com/jquery.js"></script>
    *** <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    *** <!-- Include all compiled plugins (below), or include individual files as needed -->
    *** <script src="js/bootstrap.min.js"></script>
    
    <script>
    *** $(document).ready(function() {
    ******* var audioElement = document.createElement('audio');
    ******* audioElement.setAttribute('src', 'audio/TEST.mp3');
    ******* audioElement.setAttribute('autoplay', 'autoplay');
    ******* //audioElement.load()
    
    ******* $.get();
    
    ******* audioElement.addEventListener("load", function() {
    *********** audioElement.play();
    ******* }, true);
    
    ******* $('.play').click(function() {
    *********** audioElement.play();
    ******* });
    
    ******* $('.pause').click(function() {
    *********** audioElement.pause();
    ******* });
    *** });
    </script>

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Posts
    454
    Thanks
    0
    Thanked 20 Times in 20 Posts
    I don't know anything about jQuery, but the eaiest way is to place all jQuery and audio codes into an html file and get the sound running the way you want first then add it to the output string.
    Is this like a school project or something?
    And what does the 'fishing.php' in the action tag actually do?

    All this can go in a single page. It's not that hard...php basics. Submitting a form to itself. So why the extra file? Just wondering.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #5
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey... I have the sound file working but it plays every time the page loads, refreshes, or a new image is outputted. When I added it to the output string it stopped working.

    It's for school homework but the assignment was to give examples of loops and to code them. I did that and already turned it in but I'd like to finish it properly for my portfolio.

    And fishing.php is the name of the main, and only, file. I already removed action as it wasn't need. I've been paring down the code.

    Again, thank you for all of your help.

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Posts
    454
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hey... I have the sound file working but it plays every time the page loads, refreshes, or a new image is outputted. When I added it to the output string it stopped working.
    How did you add it?
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.


  •  

    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
    •