I am not getting the desired result from the code below. Everything works fine But there is one main problem:
- When there is no song in the database it shows empty result just with back button. It fails to show echo ‘Sorry! This song is not available on our database.’;
I cant figure out where the mistake is. So plz help.
Thanks in advance!!!
JavaScript
x
<?php
// Php code that fetches audio from the database in server and shows the audio files with singers available for the submitted data
// In submission page DROPDOWN consists of Playlist name and TEXTBOX allows to type the song number for that playlist.
// Standard format for the audio file stored in the databse is Songnumber-Playlistname-Singer's Shortname.mp3
// MP3 files will be inside the AUDIO folder and this PHP code runs from the root folder where there is index.html file for data submission.
// Valid song Numbers of Each Playlists that user choose
$validsongnumbers = [
'Rock' => 3,
'Pop' => 5,
'Jazz' => 6
];
// Data captured from dropdown submitted by a user in homepage
$PlaylistName = $_POST['Dropdown'];
$songNumber = $_POST['songnumber'];
// Check the playlist exists
if (!array_key_exists($PlaylistName, $validsongnumbers)) {
echo 'Invalid playlist provided.';
exit;
}
// Check the song number is not greater than what is allowed
if ((int)$songNumber > $validsongnumbers[$PlaylistName]) {
echo 'Invalid song number provided.';
exit;
}
$userselectedsong=sprintf('%s-%s', $songNumber, $PlaylistName );
$audiofilelocation = 'audio/' .$userselectedsong. ".mp3";
// check for user entered song in entire audio folder
$files = glob("audio/" .$userselectedsong. "*.{mp3,wav,wma,mp4}", GLOB_BRACE);
$count= count ($files);
if ($count == 0) {
echo '<span style="color: red;"/>Sorry! This song is not available on our database.</span>'; //Why this part is not wotking??? Rest all is ok
}else
$arr=outputFiles( $audiofilelocation , $userselectedsong );
foreach( $arr as $obj ) {
printf(
'<link rel="stylesheet" type="text/css" href="audio/css/main.css"><div class="wrap-login100 p-l-85 p-r-85 p-t-55 p-b-55"><br><br><audio width="100%" height="100%" controls="controls" src="%1$s" controlslist="nodownload" type="audio/mp3" style="visibility: visible;">
</audio><br /><font size="4" color="#000080"><b>Singer : <font size="4" color="#B22222">%2$s<br></b></font></font></div>',
$obj->file,
$obj->name
);
}
function singeractualname( $ssn ) {
switch( $ssn ){
case 'RI':return 'Rihanna';
case 'MJ':return 'Michael Jackson';
default:return 'Singer name not available !!!';
}
}
function outputFiles( $path, $song ){
$output=[];
if( file_Exists( $path ) ){
$dirItr=new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::KEY_AS_PATHNAME );
foreach( new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST ) as $obj => $info ) {
if( $info->isFile() ){
$pttn=sprintf( '@%s-w+.w+@i', $song );
preg_match( $pttn, $info->getFileName(), $col );
if( !empty( $col ) ){
foreach( $col as $file ){
$ext=pathinfo( $file, PATHINFO_EXTENSION );
list( $int, $cat, $code )=explode( '-', pathinfo( $file, PATHINFO_FILENAME ) );
$output[]=(object)[
'ext' => $ext,
'code' => $code,
'name' => singeractualname( $code ),
'file' => 'audio/' . $info->getFileName(),
'index' => $int,
'category' => $cat
];
}
}
}
}
}
return $output;
}
Advertisement
Answer
First you have doubled “if if”:
JavaScript
if if(count($files) < 0
Second, number of files can never be negative. You should compare if number is equal 0 (zero), not less than 0.
UPDATE:
Still wrong code:
JavaScript
if ($count=0)
This is not comparison, but assignment. You are giving $count value 0. For comparison you must use:
JavaScript
if ($count == 0)