while loop to manage a team roster

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
webguy262
Forum Newbie
Posts: 2
Joined: Sun Sep 23, 2007 8:45 am

while loop to manage a team roster

Post by webguy262 »

I want to allow a coach to create and maintain a roster of 15 players (first name, last name, address, etc.).

To keep it simple, I want to use the same page/form to display an empty roster, as well as one that already has players.

The first time a coach loads the roster page, the form is empty; after entering 1 - 15 players, the form displays them.

The form should also display the existing fields so they can be edited, and should show 15 rows even if the coach has entered fewer players.

Updating values for any existing players, as well as entering values entered into any empty rows, would save upon submit.

Using while($row = mysql_fetch_array($result)) I can displays editable fields for however many players have been entered.

However, I need to get 15 rows and I need to have the field names in each row be different (in order for the INSERT/UPDATE query to work, right?).

I'm trying a while loop with a while loop like this...

Code: Select all

$result = mysql_query($sqlplayers);
$count=14;
$i=0;
while($i<=$count) {
    while($row = mysql_fetch_array($result)); {  ?>
 
<tr>
<td><?php echo  tep_draw_input_field('player[$i]_roster_fname', $row['player_roster_fname'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_lname', $row['player_roster_lname'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_address', $row['player_roster_address'], 'size="15"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_city', $row['player_roster_city'], 'size="15"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_state', $row['player_roster_state'], 'size="2"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_zip', $row['player_roster_zip'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_phone', $row['player_roster_phone'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_email', $row['player_roster_email'], 'size="20"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_number', $row['player_roster_number'], 'size="2"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_gradyear', $row['player_roster_gradyear'], 'size="4"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_feet', $row['player_roster_height_feet'], 'size="1"'); ?></td>
<td><?php echo  tep_draw_input_field('player[$i]_roster_inches', $row['player_roster_height_inches'], 'size="4"'); ?></td></tr>
 
<?php
}
$i++;
}
This code gives me 15 rows, but the row values are not appearing, and the [$i] in the "player[$i]_roster_inches" is not iterating.

Am I at all on the right track?

Is there another way to do this?

Could really use some help!
Last edited by Benjamin on Thu May 21, 2009 1:49 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: while loop to manage a team roster

Post by Darhazer »

You have a semicolon after the second while - this probably is mistake, 'cause this leads the output to be only for the outer while, and $row is set to false in the last execution of the inner while.

Also this 'player[$i]_roster_fname' probably stands with the $i inside instead with a number, because PHP do not parse variables when the string is in single quotes, try using "player[$i]_roster_fname"
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: while loop to manage a team roster

Post by Yossarian »

Simplify your code for a while till you get this straightened out.

Code: Select all

 
 
$cnt = 1 ;
 
foreach( $results as $row ) {
 
echo tep_draw_input_field( 'player_roster_fname[' . $i . ']' , $row['player_roster_fname'] ) ; 
 
$cnt ++ ;
}
 
while ( $cnt <= 15 ){
// echo a blank form element
echo '<input type=text>' . PHP_EOL ;
}
 
echo 'end of your form here' ;
 
That way you should be able to access your variable so;

Code: Select all

 
var_dump( $_POST );
 
$_POST['player_roster_fname'][1] ;
 
Get it working without all the tables, and styling.
Create legit HTML, get the save working - then slowly add styling etc back in then use the alt syntax.
webguy262
Forum Newbie
Posts: 2
Joined: Sun Sep 23, 2007 8:45 am

Re: while loop to manage a team roster

Post by webguy262 »

Thanks for your suggestions! I've managed to do what I want to do, only to discover it can't work that way.

What I dreamed of having was a form with 15 rows. The rows would be empty if the roster was empty; and if there were players on a roster, they would fill up as many as all 15 of the rows.

All rows would appear in text boxes so editing and adding players would be quite easy.

The problem I've run into is that the extra blank rows create rows in the database even if they are empty. That because I am inserting an auto increment ID and a date. So every time the form is submitted, I make 15 new rows.

So I either need to bag the hidden rows, or do the form so that it loads with a single blank row with an "Add" button, meaning people add one player at a time.

Each time a row is added, it would appear in a list above the blank row. And each of these populated rows would have "Edit" & "Delete" buttons.

Any comments on this approach?
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: while loop to manage a team roster

Post by divito »

You could check against the form to see if any are empty. What you do with that is up to you, either breaking the insert after that, or entering an "Empty" value in all the blank portions. Or a multitude of other things.
Post Reply