et4891 Posted February 16, 2013 Share Posted February 16, 2013 I kind of posted this code earlier but earlier I had other question anyways I ran into another situation and got stuck again..... ok I made this drop list and I only make it to show all .txt files in the current folder and made it to have error if there's NO .txt files in the folder but I want the error to show with ONLY the error message but somehow what I'm doing now shows the drop list with nothing in the drop list and the error message. How can I make it so it only shows the error message only and shows nothing else? This is my coding: <?php if(!(is_dir("./aaa"))) { die("Must create a <strong>aaa</strong> folder first, sorry"); } echo "<form action=\"./page2.php\" method=\"get\">"; echo "<select name=\"aaa\">"; $aaa_files = scandir("./aaa"); $hastxt = false; foreach($aaa_files as $file_list) { $file_list = strtolower($file_list); if(($file_list == ".") || ($file_list == "..")) { continue; } if(strlen($file_list)>4 && strtolower(substr($file_list, -4))!='.txt') { continue; } else { $hastxt = true; echo "<option value=\""; echo basename($file_list,".txt"); echo "\">"; echo basename($file_list,".txt"); echo "</option>"; } } echo "</select>"; echo "<br/><input type=\"submit\">"; echo "</form>"; if($hastxt == false) { echo "Must create text files first, sorry"; die(); } ?> ----------------------------------------------------------------- This is what shows if I don't have any txt file in the folder but what I want it to so is something like the image below ----------------------------------------------------------------- This is how I want the error message to show...but of course without the arrow...just plain error message nothing else.... Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2013 Share Posted February 16, 2013 $directory = "./aaa"; if(!(is_dir($directory))) { die("Must create a <strong>aaa</strong> folder first, sorry"); } $aaa_files = scandir($directory); $selectOptions = ''; foreach($aaa_files as $file) { if(($file == ".") || ($file == "..")) { continue; } $file = strtolower($file); if(strlen($file)<5 || substr($file, -4) == '.txt') { continue; } $name = basename($file, '.txt'); $selectOptions .= "<option value=\"{$name}\">{$name}</option>\n"; } if(empty($selectOptions)) { echo "Must create text files first, sorry"; } else { echo "<form action=\"./page2.php\" method=\"get\">"; echo "<select name=\"aaa\">"; echo $selectOptions; echo "</select>"; echo "<br/><input type=\"submit\">"; echo "</form>"; } Quote Link to comment Share on other sites More sharing options...
et4891 Posted February 16, 2013 Author Share Posted February 16, 2013 (edited) $selectOptions = ''; foreach($aaa_files as $file) { if(($file == ".") || ($file == "..")) { continue; } $file = strtolower($file); if(strlen($file)<5 || substr($file, -4) == '.txt') { continue; } $name = basename($file, '.txt'); $selectOptions .= "<option value=\"{$name}\">{$name}</option>\n"; } Great thanks!!! But there are just two more questions....sorry I'm a total beginner to php....I quoted the part for my question.... why does $selectOptions = ''; need to be created first? why can't we just create that variable at the bottom when you used. $selectOptions .= "<option value=\"{$name}\">{$name}</option>\n"; also for the $selectOptions .= what does that . infront of the = means? I know it's not typo because I tried erasing that and it didn't work hehe.... Also why $name needs {} around it? I did search for .= on php.net but still a bit not understanding how it works here. Sorry as I said I'm totally noob here so trying to understand as much as I can instead of just using the code without knowing it properly. Hope you don't mind explaining it....thanks in advance. Edited February 16, 2013 by et4891 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2013 Share Posted February 16, 2013 You can find answers to most of that in the manual. Look at the String section. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2013 Share Posted February 16, 2013 $selectOptions = ''; need to be created first? why can't we just create that variable at the bottom when you used. It doesn't absolutely NEED to be, but it is good programming standard. When developing code you should turn on error reporting to E_ALL (see the manual on how to do this). As you know when there are critical errors the PHP parser will display an applicable error. But, there are also less critical errors (warnings) which don't normally get displayed. But, by turning up the error reporting you will see them. In this case, I define $selectOptions before using it - otherwise it would generate a warning. $selectOptions .= "<option value=\"{$name}\">{$name}</option>\n"; also for the $selectOptions .= what does that . infront of the = means? That .= is used to concatenate a string to an existing string. If you just used = then you would be overwriting the previous value with the new value. Also why $name needs {} around it? It doesn't NEED it, but it is the standard I prefer. You can put variables within strings that are defined with double quotes and the string will be interpreted. In a single quote string the variable is treated as a literal string $color = 'blue'; echo 'The car is $color'; //Output: The car is $color echo "The car is $color"; //Output: The car is blue however, if certain characters are next to the variable in the string the PHP parser can get confused. But, putting the {} brackets around the variable name it tells the PHP where the beginning and end of the variable is. plus, in some development IDE's it allows the variable to be color coded in the string. $amount = '15,990'; echo 'The car is $$amount'; //Output: The car is echo "The car is ${$amount}"; //Output: The car is $15,990 [/code] The first one shows not value for amount because the double dollar signs makes PHP look for a variable variable - i.e. a variable named as $15,990 - which is invalid Quote Link to comment Share on other sites More sharing options...
et4891 Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) Psyco thanks for the explanation Maybe I'm too dumb to understand the .= for now.... I did check php.net and see how .= works and I know .= is a bit like $b = "Hey, "; $b .= "what's up~!"; echo $b; and the outcome will be like Hey, what's up~! which means it's like $b = $b . "what's up~!" wich then means $b="Hey, what's up~!" but with $selectOptions .= "<option value=\"{$name}\">{$name}</option>\n"; $selectOptions = $selecOptions . "<option value=\"{$name}\">{$name}</option>\n"; which will mean $selectOptions = "<option value=\"{$name}\">{$name}</option>\n" . "<option value=\"{$name}\">{$name}</option>\n"; isn't it? but how did it work so perfectly in this situation though. I think I do somehow understand it but not like 100% to get it clear in my head at the moment.... Sorry for being so dumb and annoying asking these....gonna try understanding it 100% clearly before using something I don't know....because then that means if I want to re-create the same thing again I couldn't and would be pretty useless.... Edited February 18, 2013 by et4891 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.