Jump to content

Recommended Posts

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): http://pastebin.com/2AGqgxQd

 

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/236707-trouble-with-very-simple-program/
Share on other sites

You were setting the sessions to a blank array every time the form was processed here:

 

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

 

This should work:

 

<?php session_start(); ?>

<html>
<head>
<title>Song List</title>
</head>
<body>
   
<form action="song.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'];

if(!is_array($_SESSION['songlist']))
	$_SESSION['songlist'] = array();
if(!is_array($_SESSION['songrating']))
	$_SESSION['songrating'] = array();
               
$_SESSION['songlist'][] = $song;
$_SESSION['songrating'][] = $song;
               
echo "Your song " . $song . " is rated " . $rating . " stars!";
}
print_r($_SESSION['songlist']);
echo "<br /><br />";
print_r($_SESSION['songrating']);
?>

</body>
</html>

The session['rating'] was set to song in the above code, I fixed that, made the array a single combined and delimiter of | and added a little more to it.

If you really wanted to you can create a new array, use natsort() to show highest rated songs.

 

<?php session_start(); ?>

<html>
<head>
<title>Song List</title>
</head>
<body>
   
<form action="" 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'];

if(!is_array($_SESSION['songlist']))
	$_SESSION['songlist'] = array();

               
$_SESSION['songlist'][] = "$song|$rating";//switch to $rating|$song if using natsort()
      
echo "Your song " . $song . " is rated " . $rating . " stars!";
}
echo "<br /><br />";
print_r($_SESSION['songlist']);
echo "<br /><br />";

$song_number = -1;
foreach($_SESSION['songlist'] as $song_values){
$song_number = $song_number +1;
$song_array = explode("|",$_SESSION['songlist'][$song_number]);
$show_song = $song_array[0];
$show_rating = $song_array[1];

echo $song_number.": ".$show_song. "-" .$show_rating."<br />";

}

?>

</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.