Jump to content

Not giving error msg


maxwel

Recommended Posts

<?php
$dir = 'direction';
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);


f (strlen($q) < 3) {
echo "Search must be longer than 3 characters.<br><br><a href='http://mbrservices.no-ip.org/search.html'>Back To Search Box</a>";

} else {



if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){


while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {

$nicefile = str_replace(".mp3", "", "$file");
$info = pathinfo($file);
if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) { show links of the files that
 contain query of the string)

the problem is that i want to create an if statment which check if there is files of query that user searhed show it else if the search result is ended up with nothing it return an error msg.

 

i tried alot of things like:

if ($files != ""){show the files}
elseif(!isset($errorMsg)){errormsg}

if (!empty($files)){show the files}

elseif(!isset($errorMsg)){errormsg}
if ($files != 0){show the files}
elseif(!isset($errorMsg)){errormsg}

and more but nothing ended up right they always gave blank with no error msg or nothing, any idea?

 

Thanks alot,

 

Maxwel

Link to comment
https://forums.phpfreaks.com/topic/278337-not-giving-error-msg/
Share on other sites

Is $files an array? If that's the case then you can use this:

if(count($files) != 0) {
 // Show the files
} else {
 // Show the error message
}

I can't seem to understand your code, it's broken. I don't know where $files came from. Including the part where $files is declared can help.

its not uploading its like searching directory and coming up with results that contain such query.



<?php
$dir = 'dir';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);
if (strlen($q) < 3) {
echo "Search must be longer than 3 characters.<br><br>";
} else {
if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){


while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {

$nicefile = str_replace(".mp3", "", "$file");
$info = pathinfo($file);
if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) {
if(count($file) != 0) {

echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>";
echo "<br>";

}elseif(!isset($errorMsge)){
$errorMsge = "ERROR: File not found.<br><br>";
}else{}
}elseif(!isset($errorMsg)){
$errorMsg = "ERROR: File not found.<br><br>";
}else{}
}
}
if (isset($errorMsge)){echo $errorMsge;}
else{}
if (isset($errorMsg)){echo $errorMsg;}
else{}
}else{echo"ERROR: File not found.<br><br>";}
}
closedir($res);
?>

this is the full code and the problem is that iwhen i try to search something that does not exist in directory it dont give error msg but it gives nothing in return :S

i found that when i do it like this

<?php
$dir = 'dir';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);
if (strlen($q) < 3) {
echo "Search must be longer than 3 characters.<br><br>";
} else {
if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){


while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {

$nicefile = str_replace(".mp3", "", "$file");
$info = pathinfo($file);
if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) {

echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>";
echo "<br>";


}elseif(!isset($errorMsg)){
$errorMsg = "ERROR: File not found.<br><br>";
}else{}
}elseif(!isset($errorMsge)){
$errorMsge = "ERROR: File not found.<br><br>";
}else{}
}
if (isset($errorMsge)){echo $errorMsge;}
else{}
if (isset($errorMsg)){echo $errorMsg;}
else{}
}else{echo"ERROR: File not found.<br><br>";}
}
closedir($res);
?>

it shows the error msg whether there is results or not.

seriously, i dont know why it shows if in if condition got no error msgs that i added

You are making this more difficult than it needs to be (I should know, I do that all the time). You can't say "File not found" inside the while loop just because the current file does not match. You have to make a note when you DO find a file and then echo the "Not Found" message AFTER the loop (if you didn't find one).

 

Try this rewrite:

$dir = 'dir';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);
if (strlen($q) < 3) {
	echo "Search must be longer than 3 characters.<br><br>";
} elseif (($q == "mp3") || ($q == "wav")) {
	echo "Invalid Search Criteria<BR>";
} else { 
	$foundOne = false;
	while(false!== ($file = readdir($res))) {
		if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {

			$info = pathinfo($file);
			$nicefile = $info['filename'];
			if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) {
				echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>";
				echo "<br>";
				$foundOne = true;
			}
		}
	}
	if (!$foundOne) echo "No files found<BR>";
}
closedir($res);

I don't know if it was the forum that messed up your code, or you just didn't use indenting. If it was you, learn to use it, it makes it clearer as to what is part of an IF or ELSE or LOOP or whatever.

Archived

This topic is now archived and is closed to further replies.

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