0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • It is not clear what the issue is. Can you please condense your issue and question? BTW, you should be using `==` instead of `=` when comparing values. – waterloomatt Jul 06 '20 at 17:36
  • 1
    Again, the issue is not clear. `CODE IS NOT WORKING` does not help us to know what problem is. What are you expecting to happen and what is actually happening? Please, update your code with the latest changes so we can all be on the same page. Also, turn on error reporting so see any warnings/notices - https://stackoverflow.com/a/21429652/296555. – waterloomatt Jul 07 '20 at 00:06
  • Perhaps your `and` needs to be an `or`, but can't be sure because you haven't told us what the real issue is. Is it going into `InvalidSongnumber` but it shouldn't? Vice versa? – waterloomatt Jul 07 '20 at 00:08
  • i have modified my code plz have a look hope u understand it now – Nalim Yednap Jul 07 '20 at 08:50
  • i tried your code and all work.. the only thing could be the path – Simone Rossaini Jul 07 '20 at 11:47
  • path is not the problem. It is giving results one by one in same page by going through each elements inside the array. So this type of result is coming: Invalid Song number! Sorry! This song is not available on our database .Invalid Song number!. You can see here it is giving 3 results by going through each playlists POP, JAZ, ROCK. But i need it to go only through ROCK which is submitted – Nalim Yednap Jul 07 '20 at 11:55

1 Answers1

0

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();
waterloomatt
  • 3,662
  • 1
  • 19
  • 25