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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KyZu
Forum Newbie
Posts: 17
Joined: Sun Apr 24, 2011 9:13 pm

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

Post 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!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post by social_experiment »

What output do you receive from print_r($_SESSION['songlist']) ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

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

Post 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>
KyZu
Forum Newbie
Posts: 17
Joined: Sun Apr 24, 2011 9:13 pm

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

Post 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...
Post Reply