Jump to content

Looping problem


codergirl93

Recommended Posts

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)s

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 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">

<?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!! :-)

Link to comment
Share on other sites

 

 

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"

Use glob to get all files ending in .gif. 

$imgDir = 'img/';
foreach(glob($imgDir.'*.gif') as $image)
{
   // do something with image
}

 

 

C) ($root_dir)

NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

Is $root_dir even necessary?

It is returning null for $root_dir because you have not defined it.

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.