Passing Variables

Hi. First time poster, and PHP novice.

I’m working on a project that passes three variables to a php page through a URL. i.e.
www.xxxxxxx.com?name=Tom&score=20&message=We won!

The variables are then sorted by score value, displayed on the page, and finally saved to the simple text file. If the name variable already exists on the list, only the score and message (needs to be) is appended.

The original file which was written for me by a friend, only saved the name and score variables. I added the message variable, but I’m not having much luck getting it to work.

Here’s my “working” code. I added “not sure” comments where I assume the code needs to be altered.

Thanks for any help!

[php]<?php
// get the user input
$in_name = $_GET[‘name’];
$in_score = $_GET[‘score’];
$in_message = $_GET[‘message’];

		// open the file of name/scores and store the contents
		$file_contents = file( 'scores.txt' );
		
		foreach( $file_contents as $line )
		{
		    list( $name, $score, $message ) = explode( '|', $line );
                       
                         //not sure how this needs to be appended?
                         $output[$name] = $score;
		}
                     
                    //not sure how this needs to be appended?
		$output[$in_name] = $in_score;
		
		//currently this is sorting from highest to lowest, I'd like to reverse that
		arsort( $output );
		
		// print the results
                    //not sure how this needs to be appended?
		print "Name : Score : Message";
		foreach( $output as $name => $score )
		{
		    print "<br>$name : $score : $message";
		    $store .= "$name|$score|$message\n";
		}

		// store the results in the file
		file_put_contents( 'scores.txt', $store );

?>
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service