Using if then with multiple conditions along with operators in PHP

做~自己de王妃 提交于 2020-07-10 06:53:05

问题


There is a dropdown menu which says select playlist.

And there are certain number of songs in each playlist.

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

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.

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

This is the case!!! But this code is not working I can't figure out where the mistake is. Please correct it!!

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


回答1:


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();


来源:https://stackoverflow.com/questions/62755542/using-if-then-with-multiple-conditions-along-with-operators-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!