Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-30-2011, 05:40 PM   PM User | #1
joanzn
New Coder

 
Join Date: Nov 2010
Posts: 54
Thanks: 12
Thanked 0 Times in 0 Posts
joanzn is an unknown quantity at this point
PHP $_POST array configuration

// 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:
Code:
<form action="scores2.php" method="POST">		
<table>
<tr>
<td>Player 1 Name: </td>
<td><input type="text" name="name1" /></td>
<td>Score:</td>
<td><input type="text" name="score1" size="3" maxlength="3" /></td></tr>
<!_--------------------_>
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
joanzn is offline   Reply With Quote
Old 09-30-2011, 06:53 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.


.

Last edited by mlseim; 09-30-2011 at 06:55 PM..
mlseim is offline   Reply With Quote
Old 09-30-2011, 07:57 PM   PM User | #3
joanzn
New Coder

 
Join Date: Nov 2010
Posts: 54
Thanks: 12
Thanked 0 Times in 0 Posts
joanzn is an unknown quantity at this point
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:
Code:
 

<input type="text" name="playerinfo[1][name]" />
<input type="text" name="playerinfo[1][score]" size="3" maxlength="3" />
<input type="text" name="playerinfo[2][name]" />
<input type="text" name="playerinfo[2][score]" size="3" maxlength="3" />
<input type="text" name="playerinfo[3][name]" />
<input type="text" name="playerinfo[3][score]" size="3" maxlength="3" />
... and my PHP is set like this
Code:
<?php
define('NUMPLAYERS', 5);
?>
<html>...some code
<body>
<?php
	  foreach($_POST['playerinfo'] as $player) {
echo $player['name']."  ";

$playerScore = $player['score'];
$aver = array_sum($playerScore)/NUMPLAYERS;
If ($playerScore >= $aver){
	echo "<font color=\"green\">" .$playerScore. "</font><br />";
	}
	else{
	echo "<font color=\"red\">" .$playerScore. "</font><br />";
	}

"<br />";
	  }
	 echo "The average was " .$aver." <br />";

//arsort($playerScore);
?>
</body>
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
joanzn is offline   Reply With Quote
Old 09-30-2011, 08:48 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Your form will use [] for arrays ... more like this.
No need to number them.

<input type="text" name="playerinfo[][name]" />
<input type="text" name="playerinfo[][score]" size="3" maxlength="3" />
<input type="text" name="playerinfo[][name]" />
<input type="text" name="playerinfo[][score]" size="3" maxlength="3" />
<input type="text" name="playerinfo[][name]" />
<input type="text" name="playerinfo[][score]" size="3" maxlength="3" />

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.

PHP Code:

<html>...some code
<body>
<?php

$data
=$_POST['playerinfo'];
$numPlayers=count($data);
$aver array_sum($data['score'])/$numPlayers;

 foreach(
$data as $player) {
$playerScore=$player['score'];
If (
$playerScore >= $aver){
    echo 
"<font color=\"green\">" .$playerScore"</font><br />";
    }
    else{
    echo 
"<font color=\"red\">" .$playerScore"</font><br />";
    }

"<br />";
      }
     echo 
"The average was " .$aver." <br />";

//arsort($playerScore);
?>
</body>

Last edited by mlseim; 09-30-2011 at 08:54 PM..
mlseim is offline   Reply With Quote
Old 09-30-2011, 09:14 PM   PM User | #5
joanzn
New Coder

 
Join Date: Nov 2010
Posts: 54
Thanks: 12
Thanked 0 Times in 0 Posts
joanzn is an unknown quantity at this point
Angry

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

By correlating the data
Code:
playerinfo[1][name]
playerinfo[1][score]
playerinfo[2][name]
playerinfo[2][score]
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
joanzn is offline   Reply With Quote
Old 09-30-2011, 10:22 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I'll do some more experimenting.
mlseim is offline   Reply With Quote
Old 09-30-2011, 11:21 PM   PM User | #7
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
You can be more specific in your input names: <input name="players[playerIdAbc][name]" value="Abc Name" />

Should be accessible in PHP as:

$_REQUEST['players']['playerIdAbc']['name'] === 'Abc Name';
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 09-30-2011, 11:41 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Another try ... this time I tested it.

The HTML form ... I hardcoded names and scores to make it quicker to test ...
PHP Code:
<html>
<
head>
    <
title>Scores</title>
</
head>
<
body>
    <
h1>Bowling Boulders Statistics</h1>
    <
div>
    <
form action="scores.php" method="POST">        
        <
table>
            <
tr>
                <
td>Player 1 Name: </td>
                <
td><input type="text" name="playername[]" value="John" /></td>
                <
td>Score:</td>
                <
td><input type="text" name="playerscore[]" value="150" style="width:40px;" maxlength="3" /></td>
            </
tr>
            <
tr>
                <
td>Player 2 Name:</td>
                <
td><input type="text" name="playername[]" value="Bill" /></td>
                <
td>Score:</td>
                <
td><input type="text" name="playerscore[]" value="180" style="width:40px;" maxlength="3" /></td>
            </
tr>
            <
tr>
                <
td>Player 3 Name:</td>
                <
td><input type="text" name="playername[]" value="Sara" /></td>
                <
td>Score:</td>
                <
td><input type="text" name="playerscore[]" value="205" style="width:40px;"maxlength="3" /></td>
            </
tr>
            <
tr>
                <
td>Player 4 Name:</td>
                <
td><input type="text" name="playername[]" value="Paul" /></td>
                <
td>Score:</td>
                <
td><input type="text" name="playerscore[]" value="110" style="width:40px;" maxlength="3" /></td>
            </
tr>
            <
tr>
                <
td>Player 5 Name:</td>
                <
td><input type="text" name="playername[]" value="Jenny" /></td>
                <
td>Score:</td>
                <
td><input type="text" name="playerscore[]" value="175" style="width:40px;" maxlength="3" /></td>
            </
tr>
        </
table>
        <
div><input type="submit" value="Show Statistics" /></div>
    </
form>
    </
div>
</
body>
</
html

The script called "scores.php" ...
PHP Code:
<?php

$players
=$_POST['playername'];
$scores=$_POST['playerscore'];
$temp=array();
$sum=0;
for(
$i=0;$i<count($players); $i++){
$temp[$i]=$scores[$i]."|".$players[$i];
$sum=$sum+$scores[$i];
}
$aver=$sum/count($players);
rsort($temp);
?>
<html>
<body>
<?php
 
foreach($temp as $data) {
 
$item=explode("|",$data);
 if(
$item[0] >= $aver){
 echo 
$item[1]." &nbsp;&nbsp; <span style='color:#00f000;'>".$item[0]."</span>";
 }
 else{
 echo 
$item[1]." &nbsp;&nbsp; <span style='color:#f00000;'>".$item[0]."</span>";
 }
 echo
"<br />";
 }
 echo
"<br /><br /> The Average is $aver";
 
?>
</body>

The only thing I didn't do was make sure the user didn't enter a name without a score
or vice-versa. That would probably mess it up.


.
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
joanzn (10-01-2011)
Old 10-01-2011, 04:36 PM   PM User | #9
joanzn
New Coder

 
Join Date: Nov 2010
Posts: 54
Thanks: 12
Thanked 0 Times in 0 Posts
joanzn is an unknown quantity at this point
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.
joanzn is offline   Reply With Quote
Old 10-01-2011, 06:46 PM   PM User | #10
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
| 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.

.

Last edited by mlseim; 10-01-2011 at 06:51 PM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
joanzn (10-01-2011)
Reply

Bookmarks

Tags
array, form, php, sort

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:54 PM.


Advertisement
Log in to turn off these ads.