Trouble with very simple program! (sorting entries via form)
Posted: Tue May 17, 2011 9:40 pm
I'm just trying to incorporate some of what I've learned into practice and I can't believe I'm having problems with this but I really want to figure it out.
I'm trying to create a simple program where a user enters the name of a song, and gives it a rating (out of 5), and it displays the output first by song rating then (if the ratings are the same) displays it by the name of the song alphabetically. The idea in practice would work like this...user enters:
I just keep undoing things and clearly I'm not getting it. I had many ideas making it so I'll show you what I wrote (which isn't much): FILE: Songs.php
It looks terrible I know, I was just trying to find out how to put things from the form INTO an array and trying to output the array as I described above. If I could figure out how to use these session arrays to hold the entries from the post, then I'd be able to sort them properly, I'm just lost right now because everything I've read online and tried wasn't fixing the problem.
Thank you so much for the help!
I'm trying to create a simple program where a user enters the name of a song, and gives it a rating (out of 5), and it displays the output first by song rating then (if the ratings are the same) displays it by the name of the song alphabetically. The idea in practice would work like this...user enters:
Song Name: My Song
Rating: 3
Song Name: His Song
Rating: 5
Song Name: Her Song
Rating: 5
Your list so far is:
Her Song - 5 stars
His Song - 5 stars
My Song - 3 stars
I just keep undoing things and clearly I'm not getting it. I had many ideas making it so I'll show you what I wrote (which isn't much): FILE: Songs.php
Code: Select all
<?php
session_start();
?><html>
<head>
<title>Song List</title>
</head>
<body>
<form action="songs.php" method="POST">
Song: <input type="text" name="song" />
Rating: <select name="rating">
<?php
foreach (range(5, 1) as $number) {
echo "<option value=\"$number\">$number</option>";
}
echo "</select>";
?>
<input type="submit" value="Submit!" />
</form>
<?php
if (isset($_POST['song']) && isset($_POST['rating'])) {
$song = $_POST['song'];
$rating = $_POST['rating'];
$_SESSION['songlist'] = array();
$_SESSION['songrating'] = array();
$_SESSION['songlist'][] = $_POST['song'];
$_SESSION['songrating'][] = $_POST['rating'];
echo "Your song " . $song . " is rated " . $rating . " stars!";
} else {
$song = NULL;
$rating = NULL;
}
print_r($_SESSION['songlist']);
?>
</body>
</html>Thank you so much for the help!