need a better way to check fields and write total values

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
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

need a better way to check fields and write total values

Post by rad1964 »

Hello,
I am a PHP beginner.
I am trying to write code that takes data from a form, compares a players 'id number' and writes their total amount of 'highlights'.
I was successful at comparing the initial 10 fields that make up my section of the form called team game.
There is a total of 32 player slots on the home and 32 player slots on the visitors side of the form. (See image)
Image

Here is some code I am using:

Code: Select all

// Compares playerH1 to every player spot and returns all of playerH1's individual highlights and turns them into an array [$ph1]
$ph1 = '';
if ($playerH1 == "Choose Player") {}
if (strlen($highlightsplayerH1) > 0) {
	$ph1 = $highlightsplayerH1.","; } else {}
if ($playerH1 == $playerH11 && strlen($highlightsplayerH11)  > 0) {
	$ph1 = $ph1.$highlightsplayerH11.","; } else {}
if ($playerH1 == $playerH12 && strlen($highlightsplayerH12)  > 0) {
	$ph1 = $ph1.$highlightsplayerH12.","; } else {}
if ($playerH1 == $playerH13 && strlen($highlightsplayerH13)  > 0) {
	$ph1 = $ph1.$highlightsplayerH13.","; } else {}
and on and on until playerH32 is compared.

then this:

Code: Select all

// Add highlight totals for playerH1 on home team
// Removes the trailing comma used to build the array
$ph1 = rtrim($ph1,",");
$ph1Array = explode(",",$ph1);
$ph1Count = count($ph1Array);
Then I just used a ton of code similar to above to go through the next ten slots of the team game.
If a player does not play in the team game but plays after those slots, how would I generate the rest of slots 11-32 without duplicating data.
In the end all I want is a single players ID # with all the highlights they received.

Afterwards I use this code to publish results:

Code: Select all

// Itemized Home Players Highlights
echo "<br />"; 
echo "<strong>Itemized Highlights Visitors</strong><br />";
if ($playerV1 == "Choose Player") {}
	elseif (empty($pv1)) {}
	elseif ($pv1Count < 2) {
	echo "#".$playerV1." ".$pv1."<span style='color: '>"." You shot one ".$pv1Count." highlight!"."</span>"."<br />";
	} elseif ($pv1Count < 5) {
	echo "#".$playerV1." ".$pv1."<span style='color: 3333FF'>"." You shot ".$pv1Count." highlights!"."</span>"."<br />";
	} elseif ($pv1Count < 10) { 
	echo "#".$playerV1." ".$pv1."<span style='color: 3333FF'>"." Awesome job! You shot ".$pv1Count." highlights!"."</span>"."<br />";
	} elseif ($pv1Count < 14) {
	echo "#".$playerV1." ".$pv1."<span style='color: 3333FF'>"." You are a highlight machine! You shot ".$pv1Count." highlights!"."</span>"."<br />";
	} else {
	echo "#".$playerV1." ".$pv1."<span style='color: 3333FF'>"." Does Phil Taylor know you took his skills?! You shot an astounding ".$pv1Count." highlights!"."</span>"."<br />";
}
and then use more of the same for each home AND visitors slot (64 slots total!)

Results Image: Image

Now that's a lot of code and I am sure there's a shorter more efficient way to do it.
Please teach me the correct method... Appreciate it!

rad1964
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Re: need a better way to check fields and write total values

Post by rad1964 »

Guess my PHP skills are better then I thought, with no replies...
4 weeks later..

bummer :cry:
rad1964
Post Reply