首页 > 解决方案 > 满足条件后PHP无法回显文本

问题描述

我没有从下面的代码中得到想要的结果。一切正常但有一个主要问题:

  1. 当数据库中没有歌曲时,它仅使用后退按钮显示空结果。它无法显示回声“对不起!这首歌在我们的数据库中不可用。';

我无法弄清楚错误在哪里。所以请帮忙。

提前致谢!!!

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

标签: php

解决方案


首先,您将“如果”加倍:

if if(count($files) < 0

其次,文件数永远不能为负数。您应该比较数字是否等于 0(零),不小于 0。

更新:

还是错误的代码:

if ($count=0)

这不是比较,而是分配。您给出的 $count 值为 0。为了进行比较,您必须使用:

if ($count == 0)

推荐阅读