Jump to content

how to terminate everything and only show the error message


et4891

Recommended Posts

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();
}
?>

 

-----------------------------------------------------------------

post-139891-0-51036500-1360975429_thumb.jpg

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

 

-----------------------------------------------------------------

post-139891-0-21937000-1360975434_thumb.jpg

This is how I want the error message to show...but of course without the arrow...just plain error message nothing else....

Link to comment
Share on other sites

$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>";
}

Link to comment
Share on other sites

$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 by et4891
Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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 by et4891
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.