// I'm Aware that this code is not correct it's kinda just for understanding
//what I'm trying to do more so
I'm trying to figure out a simpler way to code a PHP script so that the $_POST array can be easily dissected. Soo .. I have 10 input="text" fields in a form:
NOW I Want to have each "name" and "score" connected to its respective player so that when I output:
Code:
//not correct code, just for conceptual
<?php foreach($_POST as "name" => "score") {
echo "name - score <br />";
}
?>
It will output the name of the player with their score. As if they were connected together instead of building an key/value array....
with this output data I'm looking to claculate the average, change the color of above(green) and below(red) averages scores, and sort the output into descending order. What I have now can be found at this link as for structure of the output and the sort function yet it is'nt really what I want.
.....Any help much appreciated http://livelaughlovephotos.me/Lab3/scores.html
Are you going to be using a database of any kind? Or will you have to always
enter the name and score each time you want statistics?
I'm asking this because there are nice and easy options to creating a simple
database (with MySQL), or with Google Docs. Using Google Docs Spreadsheet,
you can allow anyone you want to go in and modify data. Names, teams, scores,
dates, handicaps, etc. without a need for MySQL.
The PHP script will generate statistics from the database ... there would be no
forms for you to enter data. As data is modified, the statistics will change (of course).
The MySQL method is a database within your own website server, the Google Docs is
a database controlled by them. MySQL has more database power, but Google Docs is
easy to have "outside" people enter data (it's just a basic spreadsheet, like Excel).
I guess it depends on your PHP/MySQL skills, or personal preference. I use both of them
all the time ... in fact, sometimes I use both together ... Google Docs as a way to give
only certain people permission to modify data without creating an admin script.
Eventually I'm looking to connect this simple little thing to a MySql Database just to screw around and become more familiar with PHP to MySql integration.
My PHP skill are rough at that .. and MySql are about the same . but I'm trying to start small at the moment
As for now I have the PHP script I would like to modify but I'm having trouble
with certain built in functions(array_sum() & arsort()) ..
My html for the link HERE is set up like this:
I'm getting an error stating I need have an array for the 2 functions (sum and sort) but aren't they in the array I'm stating?...
as u can see I'm confused
The PHP (untested) ...
I'm sort of guessing on this. I should test it (and learn how to do it),
before I post this. But this might provide some direction.
from reading your code tho it looks as if there is no connection to each player and their respective scores.
so if I go and sort the data from highest to lowest the scores will sort yet their respective players will not.
so when it might start off with:
player 1's score = 30
player 2's score = 10
by sorting the scores in descending order it will change the scores and have no ties to the player who's score it really is:
player 1's score = 10
player 2's score = 30
player 1's name is directly related to player 1's score
and 2 for 2's so on and so fourth.
essentially making the name the key and the score the value.
Not to mention the arsort() and array_sum() want an array for a parameter
and it's giving me an error when I code:
Code:
$aver = array_sum($data['score'])
as a parameter it keeps saying it wants an array for both and that IS an array of scores,
to my understanding...... so I'm lost
mlseim ...u are the man! ..works like a charm ..simple clean code.
Thanks for all your help on it. For now validation is something I'm not too worried about and can implement it at a later point anyways. Just kinda wanted to get this going so I can become more comfortable with PHP coding.
The only thing I'll have to figure out is what the "|" means for syntax.
I assume it's "separate" after an output or something...I'll look into it.
| is called a "pipe". I only use it because nobody ever uses it when they type.
You can just as easily use a comma, or any other character. By separating two
or more values with a pipe| you can later on, explode the string, separating them
apart.
If you do a lot of Excel, you'll see a term like CSV (comma separated variables).
This would be converting your Excel spreadsheet to something like this:
1234,automobile,ford mustang
3423,automobile,chrysler,challenger
With PHP, you would explode this lines, separating the variables by commas.
Exploding creates an array.
You can see one of the problems ... what if one of the variables contains a comma?
The separation of variables would also include the commas within the variables.
So that's why I use pipes | ... no other reason.
Have fun and keep learning.
EDIT:
And know that if you use MySQL instead of the HTML <form> to get your data,
pretty much all of the searching, math, sorting, grouping, logic conditions happen
with the query itself. That's why MySQL is really the best way to do this.