在 PHP 中使用 if then 和多个条件以及运算符 [英] Using if then with multiple conditions along with operators in PHP

查看:88
本文介绍了在 PHP 中使用 if then 和多个条件以及运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个下拉菜单,上面写着选择播放列表.

There is a dropdown menu which says select playlist.

而且每个播放列表中都有一定数量的歌曲.

And there are certain number of songs in each playlist.

例如:播放列表包括:POP(歌曲总数=3)、ROCK(歌曲=4)、Jaz(歌曲=5)、古典(歌曲=6)

For example: The playlist includes: POP (total number of songs = 3), ROCK (songs = 4 ), Jaz (songs= 5), Classical(songs= 6)

假设用户选择了一个播放列表 - POP 并在文本框(歌曲编号框)中输入数字 3,然后当单击搜索"按钮时,它将从音频文件夹(如果存在)打开 POP3.mp3 文件,否则它将显示数据库中没有该歌曲.

Suppose a user chose one playlist- POP and typed number 3 in the textbox (songnumber box) then when clicked SEARCH button it will open POP3.mp3 file from the audio folder (if it is there), and else it will show the song not available in the database.

并且如果用户选择 POP 并在歌曲编号框中键入 4,它应该显示无效的歌曲编号.

And if the user selects POP and types 4 in the songnumber box it should show invalid songnumber.

是这样的!!!但是这段代码不起作用我不知道错误在哪里.请更正!!

<?php
$valid = ['POP' => 3, 'ROCK' => 5, 'JAZZ' => 5];
// User selected genre and songnumber
$PlaylistName = 'ROCK'; // Note: I will get this value from dropdown in HTML
$songNumber = 5; // Note: I will get this value from textbox in HTML form
$song = $PlaylistName . $songNumber . '.mp3';
$file_pointer = './audio/' . $song;

foreach ($valid as $genre => $numberSongs) {
    if ($PlaylistName === $genre && $songNumber <= $numberSongs) {
        if (file_exists($file_pointer)) {
            header("Location: ./audio/" . $song);
            exit();
        } else {
            SongNotavailable();
        }
    } else {
        InvalidSongnumber();
    }
}

function InvalidSongnumber()
{
    echo "Invalid Song number!";
}

function SongNotavailable()
{
    echo '<span style="color: red;"/>Sorry! This song is not available on our database.</span>';
}
?>

// This gives result: Invalid Song number!Sorry! This song is not available on our database. Invalid Song number!

// But the valid answer is only Sorry! This song is not available on our database.

// So I need a correction in my code so that I can get only a valid result, not all results together

推荐答案

您的问题是您处于循环中,并且您正在对 valid 中的每个条目进行迭代.您应该只验证传入数据一次.

Your issue is that you're in a loop and you are iterating for each entry in valid. You should just validate the incoming data once.

<?php

$valid = [
    'POP' => 3, 
    'ROCK' => 5, 
    'JAZZ' => 5
];

// Test data
$PlaylistName = 'ROCK'; 
$songNumber = 5;

// Check the playlist exists
if (!array_key_exists($PlaylistName, $valid)) {
    echo 'Invalid playlist provided.';
    exit;
}

// Check the song number is not greater than what is allowed
if ((int)$songNumber > $valid[$PlaylistName]) {
    echo  'Invalid song number provided.';
    exit;
}

$song = $PlaylistName . $songNumber . '.mp3';
$file_pointer = './audio/' . $song;

// Check the file exists on disk
if (!file_exists($file_pointer)) {
    echo '<span style="color: red;"/>Sorry! This song is not available on our database.</span>';
    exit;
}

// We now know the song is valid.
header("Location: ./audio/" . $song);
exit();

这篇关于在 PHP 中使用 if then 和多个条件以及运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆