Jump to content

Not giving error msg


maxwel
Go to solution Solved by DavidAM,

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

Link to comment
Share on other sites

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

Edited by maxwel
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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.

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.