Hi anturj,
You would probably be better to do this more like a search page with the potential filters being Artist, Album, Song. Instead of having a user traverse 3 pages, you could set this up in one page as a form. Then use a traditional post on submit to carry the selected filters over to a PHP page that grabs the post values, connects to the database, runs an SQL query and then returns the result set back that is "ORDER BY" in a hierarchy based on the filters that are chosen. This is typically done where you've grab the $_POST values and then dynamically build the SQL query. Normally this is done like
PHP Code:
$sql = "
SELECT
artist
, album
, song
FROM
music
WHERE
1 = 1 ";
Now the 1=1 might be a little puzzling. This is done so you can append 'AND' statements after the 1=1 and no matter if any or one or more filters are chosen then the query will still run.
To test if filters exists, you would then do this:
PHP Code:
if ($_POST['artist']) {
$artist = mysql_escape_string($_POST['artist']);
$sql .= "
AND
artist = $artist
";
}
if ($_POST['album']) {
$album = mysql_escape_string($_POST['album']);
$sql .= "
AND
album = $album
";
}
if ($_POST['song']) {
$song = mysql_escape_string($_POST['song']);
$sql .= "
AND
song = $song
";
}
The use of mysql_escape_string() is not encouraged as it is now recommended to use PDO or mysqli to prepare and bind values, however I have used this just to show that you need in some way to clean all input data including $_POST, $_GET, $_SESSION, even JSON and serialized data.
If your form filter fields use free-text rather than combo box selections then you can substitute the equal sign (=) for LIKE: i.e.
PHP Code:
if ($song) {
$sql .= "
AND
song LIKE %$song%;
";
}
This will allow people to kind of know who the song name but don't have the exact title so in this case it will match songs like whatever name they type in the song form field.
One other thing is that you may consider using a javascript library like jQuery to perform an AJAX $_POST. This has the advantage of simplifying implementing AJAX and you can POST and return a result set by using a callback that will for instance return the results in JSON format without reloading the page.
But see how you make out with the first idea and I hope this helps a little.
Regards,
Steve
Bookmarks