Page 1 of 1

Trouble with very simple program! (sorting entries via form)

Posted: Tue May 17, 2011 9:40 pm
by KyZu
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:
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>
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!

Re: Trouble with very simple program! (sorting entries via f

Posted: Wed May 18, 2011 5:01 am
by social_experiment
What output do you receive from print_r($_SESSION['songlist']) ?

Re: Trouble with very simple program! (sorting entries via f

Posted: Wed May 18, 2011 8:03 am
by Weiry
Your first problem is that every time the page loads, you are redefining your session arrays again, clearing any data which was previously in them.

Something similar to this should get you what your after. (untested)

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) {
          print "<option value='$number'>$number</option>";
     }
     ?>
     </select>
     <input type="submit" value="Submit!" />
</form>
<?php
if(!isset($_SESSION['songlist'])){

     $_SESSION['songlist'] = array();

}

if (!empty($_POST['song']) && !empty($_POST['rating'])) {
                
     $_SESSION['songlist'][] = array("name" => $_POST['song'], "rating" => $_POST['rating']);

}

foreach($_SESSION['songlist'] as $song){

     print $song['name'] . " - " . $song['rating'] . " stars";

}


?>
</body>

Re: Trouble with very simple program! (sorting entries via f

Posted: Wed May 18, 2011 11:50 am
by KyZu
Thanks Weiry, that worked.

The only problem is I'm not figuring out how to sort the list first by rating, and then by song name alphabetically, any ideas? I'll keep working on it a bit more and see if I come up with anything...