Jump to content

[SOLVED] Loop to C/R/U/D team roster


webguy262

Recommended Posts

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...

 

$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!

Link to comment
Share on other sites

  • Replies 59
  • Created
  • Last Reply

I'm really not sure I understand what you are trying to accomplish... but I can tell you the $i isn't working because you're using single quotes instead of double quotes:

 

Either of these will work:

<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_fname', $row['player_roster_fname'], 'size="10"'); ?></td>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

You can keep it as is.

 

Before inserting into the database:

1) concatenate all of the fields for a single row

2) trim() result from step 1

3) if strlen( result_from_step_2 ) == 0, then do not insert

Link to comment
Share on other sites

Also, with your form the way it is now, fill it out and submit it and on the processing page perform a:

<?php echo '<pre>' . print_r( $_POST, true ) . '</pre>'; ?>

 

 

Then try changing your form to this:

<td><?php echo  tep_draw_input_field("player[$i][fname]", $row['player_roster_fname'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][lname]", $row['player_roster_lname'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][address]", $row['player_roster_address'], 'size="15"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][city]", $row['player_roster_city'], 'size="15"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][state]", $row['player_roster_state'], 'size="2"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][zip]", $row['player_roster_zip'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][phone]", $row['player_roster_phone'], 'size="10"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][email]", $row['player_roster_email'], 'size="20"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][number]", $row['player_roster_number'], 'size="2"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][gradyear]", $row['player_roster_gradyear'], 'size="4"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][feet]", $row['player_roster_height_feet'], 'size="1"'); ?></td>
<td><?php echo  tep_draw_input_field("player[$i][inches]", $row['player_roster_height_inches'], 'size="4"'); ?></td></tr>

Perform the same experiment you did before.  Fill out the form and on your processing page do this:

<?php echo '<pre>' . print_r( $_POST, true ) . '</pre>'; ?>

 

The second version of the form should be easier to process.

<?php
/* processing the form */
$cols = array_keys( $_POST['player'][0] );
$stmt = $pdo->prepare( "insert into thetable ( " . implode( ', ', $cols ) . " ) values ( " . implode( ', ', array_fill( 0, count( $cols ), '?' ) ) . " )" );
$success = true;
foreach( $_POST['player'] as $idx => $player ) {
  if( !strlen( trim( implode( "", $player ) ) ) ) { continue; } // empty so do not insert
  $success = $success && $stmt->execute( $player );
}
echo 'Success: ' . ($success ? 'true' : 'false') . "<br />\n";
?>

 

You could modify that to perform updates as well.  :)

Link to comment
Share on other sites

Thanks for your help!  I followed your suggestions.

 

If I understand correctly, the second version of the form creates a single "player" array with all the values for all players.

 

Then the /* processing the form */ code performs the INSERT, checking to see if the array from a row was empty and if so, not inserting it.

 

Is that accurate?

 

How would I handle the INSERT of the two hidden fields: CURDATE() and $customer_id?   

Link to comment
Share on other sites

Yes, your understanding of how it works is correct.

 

<?php

if( !empty( $_POST ) ) {
/**
* If you print_r( $_POST ) you will find that you have an array named 'players'.
* Each index into this array will be POSITIVE and the players database ID if they already exist in the database
* The index will be NEGATIVE if the player is new and needs to be inserted.
*
* And then each player is an array where the associative names should match your column names,
* so that you can easily generate your insert / update statements based on what I
    * showed you earlier.
*/
$insert = $pdo->prepare( "an insert statement" );
$update = $pdo->prepare( "an update statement" );
foreach( $_POST['players'] as $player_id => $player_info ) {
	if( $player_id < 0 ) {
		$insert->execute( $player_info );
	}else{
		$update->execute( $player_info );
        }
    }
exit(); return; // whatever is appropriate to stop processing
}

$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
'zip' => 'size="10"', 'phone' => 'size="10"',
'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'feet' => 'size="1"', 'inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
$blankplayer[$c] = '';
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
if( $result ) {
	$player = mysql_fetch_assoc( $result );
    }
if( !$player ) {
	// We've run out of players, so create a blank one to insert
        $result = null; // stop trying to access result
	$player = $blankplayer;
        $player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
}
// dump the fields
foreach( $cols as $c => $extra ) {
	echo "<td>" . tep_draw_input_field( "players[{$player['id']}][{$c}]", $row['player_roster_' . $c], $extra ) ."</td>";
    }
    $player = null; // important!
}
?>

 

CURDATE() probably goes along with a modify_tm field or something similar.  You don't need to include it in the form at all.  When you write your update / insert just make it like:

 

update table set modi_tm=CURDATE()

 

insert table ( modi_tm ) values ( CURDATE() )

 

 

Link to comment
Share on other sites

Thanks for continuing to help me with this!  My boss is anxious to have it in place.

 

I loaded your code and I have a couple of questions...

 

The cells are coming out like this...

 

<td><input type="text" name="players[-1][fname]" size="10"></td>

 

Before I go too far, will the code you suggested process them when they are in that format?  The []'s have me wondering.

 

Also, where do I add <tr></tr>'s?  I suspect I'll need a count routine, placing them every 12 <td>'s.

 

I know if needs to go in this area...

 

foreach( $cols as $c => $extra ) {
      echo "<td>" . tep_draw_input_field( "players[{$player['id']}][{$c}]", $row['player_roster_' . $c], $extra ) ."</td>";
    }

 

...but I'm not sure how to code it.

 

Thanks again!

 

I'm learning slowly but surely!

Link to comment
Share on other sites

Trying to insert arrayed data.

 

Getting: Parse error: parse error, expecting `']''

 

Here is the code...

 

$insert = "INSERT INTO rosters (roster_date, customers_id, player_roster_fname, player_roster_lname, player_roster_address, player_roster_city, player_roster_state, player_roster_zip, player_roster_phone, player_roster_email, player_roster_number, player_roster_gradyear, player_roster_height_feet, player_roster_height_inches) VALUES (CURDATE(), '$customer_id', '$_POST[players[-1][fname]]', '$_POST[players[-1][lname]]', '$_POST[players[-1][address]]', '$_POST[players[-1][city]]', '$_POST[players[-1][state]]', '$_POST[players[-1][zip]]', '$_POST[players[-1][phone]]', '$_POST[players[-1][email]]', '$_POST[players[-1][number]]', '$_POST[players[-1][gradyear]]', '$_POST[players[-1][feet]]', '$_POST[players[-1][inches]]'),

Link to comment
Share on other sites

Well I think I'm getting close®!

 

I'm getting the following errors:

 

Warning: array_keys(): The first argument should be an array in /home/virtual/site57/fst/var/www/html/hoopgroup/roster.php on line 59

Fatal error: Call to a member function on a non-object in /home/virtual/site57/fst/var/www/html/hoopgroup/roster.php on line 61

 

I'm not worried about the fatal error as I have not yet added an update statement.  Hopefully, when I get the insert working, creating the update will be easy.

 

Here is the code...

if( !empty( $_POST ) ) {
   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
    * showed you earlier.
   */
   $cols = array_keys( $_POST['player'][0] );
   echo '<pre>' . print_r( $_POST, true ) . '</pre>';
   $insert = $pdo->prepare( "insert into rosters ( " . implode( ', ', $cols ) . " ) values ( " . implode( ', ', array_fill( 0, count($cols), '?')) . " )" );
   $update = $pdo->prepare( "an update statement" );
   foreach( $_POST['player'] as $player_id => $player_info ) {
      if( $player_id < 0 ) {
         $insert->execute( $player_info );
      }else{
         $update->execute( $player_info );
        }
    }
   exit(); return; // whatever is appropriate to stop processing
}

echo "<form action=\"roster.php\" method=\"post\"><table>";
   
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'feet' => 'size="1"', 'inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer[$c] = '';
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   if( $result ) {
      $player = mysql_fetch_assoc( $result );
    }
   if( !$player ) {
      // We've run out of players, so create a blank one to insert
        $result = null; // stop trying to access result
      $player = $blankplayer;
        $player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   foreach( $cols as $c => $extra ) {
      echo "<td>" . tep_draw_input_field( "player[{$player['id']}][{$c}]", $row['player_roster_' . $c], $extra ) ."</td>";
    }
   // echo '<pre>' . print_r( $_POST, true ) . '</pre>';
    $player = null; // important!
}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

 

Thanks again for your help!

Link to comment
Share on other sites

Thanks, roopurt18, that seems to have worked for the insert statement. 

 

But, alas, I'm stuck again.  (Hey, at least I figured out where to echo the <TR></TR> so the form is not longer 180 cells wide!)

 

I can't figure out how to code the update statement.

 

I know the basic structure is:

 

"update rosters set ( " . implode( ', ', $cols ) . " )=( " . implode( ', ', array_fill( 0, count($cols), '?')) . " ) where player_id = $_POST[player_id]"

 

But obviously that is not going to work as there is no array creating the $cols=$values pairs.

 

So once again, I need some help.

 

TIA

Link to comment
Share on other sites

You might do something like this for processing then:

<?php
if( !empty( $_POST ) ) {
   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
    * showed you earlier.
   */
   foreach( $_POST['players'] as $player_id => $player_info ) {
  foreach( $player_info as $k => $v ) {
	$player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
      }
  if( $player_id < 0 ) {
         $player_info['primary_key'] = "'" . mysql_real_escape_string( $player_id ) . "'";
	 $stmt = "insert into `yourtable` ( " . implode( ', ', array_keys( $player_info ) )
		. " ) values ( " . implode( ', ', $player_info ) . " )";
      }else{
	foreach( $player_info as $k => $v ) {
		$player_info[$k] = '`' . $k . '`=' . $v;
	}
	$stmt = "update `yourtable` set " . implode( ', ', $player_info ) . " where "
		. "`primary_key`='" . mysql_real_escape_string( $player_id ) . "'"; 
  }
      mysql_query( $stmt ); // check for errors and success
    }
   exit(); return; // whatever is appropriate to stop processing
}
?>

Link to comment
Share on other sites

Thanks for continuing to help!

 

Here's what I have.  When I fill in a row and submit it, I get a no results.  Nothing in the browser; nothing in the database.

 

Never thought I be disappointed at not seeing an error, but with a blank page, I don't know where to begin trouble  shooting.

 

Can you take a look?

 

<?php

if( !empty( $_POST ) ) {
   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
    * showed you earlier.
   */
   foreach( $_POST['player'] as $player_id => $player_info ) {
     foreach( $player_info as $k => $v ) {
      $player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
      }
     if( $player_id < 0 ) {
         $player_info['primary_key'] = "'" . mysql_real_escape_string( $player_id ) . "'";
       $stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
         . " ) values ( " . implode( ', ', $player_info ) . " )";
      }else{
      foreach( $player_info as $k => $v ) {
         $player_info[$k] = '`' . $k . '`=' . $v;
      }
      $stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
         . "`primary_key`='" . mysql_real_escape_string( $player_id ) . "'"; 
     }
      mysql_query( $stmt ); // check for errors and success
    }
   exit(); return; // whatever is appropriate to stop processing
}

echo "<form action=\"roster.php\" method=\"post\"><table>";
   
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'feet' => 'size="1"', 'inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer[$c] = '';
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
      $player = mysql_fetch_assoc( $result );
    }
   if( !$player ) {
      // We've run out of players, so create a blank one to insert
        $result = null; // stop trying to access result
      $player = $blankplayer;
        $player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   foreach( $cols as $c => $extra ) {
      echo "<td>" . tep_draw_input_field( "player[{$player['id']}][{$c}]", $row['player_roster_' . $c], $extra ) ."</td>";
    }
    $player = null; // important!
   echo "</tr>";
    
}
?>

Link to comment
Share on other sites

Thanks

 

Here is what I did...

 

   foreach( $_POST['player'] as $player_id => $player_info ) {
     foreach( $player_info as $k => $v ) {
      $player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
      }
      echo "1";
     if( $player_id < 0 ) {
         $player_info['primary_key'] = "'" . mysql_real_escape_string( $player_id ) . "'";
       $stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
         . " ) values ( " . implode( ', ', $player_info ) . " )";
          echo "2";
      }else{
      foreach( $player_info as $k => $v ) {
         $player_info[$k] = '`' . $k . '`=' . $v;
          echo "3";
      }
      $stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
         . "`primary_key`='" . mysql_real_escape_string( $player_id ) . "'";
          echo "4"; 
     }
      mysql_query( $stmt ); // check for errors and success
       echo "5";
    }
   exit(); return; // whatever is appropriate to stop processing
    echo "6";
}

 

And when I filled in a row, I got this...

 

125125125125125125125125125125125125125125125

Link to comment
Share on other sites

Try:

   echo "5a";
   exit(); return; // whatever is appropriate to stop processing
    echo "6";

 

It's also more helpful if they appear on their own lines, so append either <br> or "\n" to them.  And you can remove some of the basic ones now that you know they're being executed.

Link to comment
Share on other sites

Thanks again for your continued help.

 

I updated the code and now I get...

 

1

2

Unknown column 'fname' in 'field list'

5

 

 

 

The column in the database is player_roster_fname so it is failing on the first insert because only part of the column name is getting iterated.

 

Do we need this?

 

$cols = array_keys( $_POST['player'] );
$cols = array_keys( $_POST['player'][$cols[0]] );

 

It was in there with the PDO version of the script, but it did not seem to match the code you suggested after learning I did not have PDO installed.

Link to comment
Share on other sites

Well there you go.  mysql_error() is telling you that `fname` is not a field in your database.

 

Go back and look at the HTML generated for the original form.  The idea is for each field to be named after the associated field in the database.  In the form generation code I gave you I did my best to duplicate your table structure, but I don't know if I nailed it.  But all of the inputs should have a name attribute of the format:

 

name="players[player_primary_key_from_database][player_roster_column]"

 

Make sure they're working like that and the update / insert code should also work.

 

Otherwise, post:

1) Your table structure

2) The entire code

3) The HTML being generated for the form

Link to comment
Share on other sites

Thanks again for your help!

 

The html being created ia as follows (first row only)...

 

 

<form action="roster.php" method="post"><table><tr><td><input type="text" name="player[-1][fname]" size="10"></td><td><input type="text" name="player[-1][lname]" size="10"></td><td><input type="text" name="player[-1][address]" size="15"></td><td><input type="text" name="player[-1][city]" size="15"></td><td><input type="text" name="player[-1][state]" size="2"></td><td><input type="text" name="player[-1][zip]" size="10"></td><td><input type="text" name="player[-1][phone]" size="10"></td><td><input type="text" name="player[-1][email]" size="20"></td><td><input type="text" name="player[-1][number]" size="2"></td><td><input type="text" name="player[-1][gradyear]" size="4"></td><td><input type="text" name="player[-1][feet]" size="1"></td><td><input type="text" name="player[-1][inches]" size="4"></td></tr>

etc...

 

Here is the dbase structure...

 

CREATE TABLE `rosters` (
  `player_id` tinyint(4) NOT NULL auto_increment,
  `customers_id` int(11) NOT NULL default '0',
  `roster_date` timestamp(14) NOT NULL,
  `player_roster_fname` varchar(32) NOT NULL default '',
  `player_roster_lname` varchar(32) NOT NULL default '',
  `player_roster_address` varchar(32) NOT NULL default '',
  `player_roster_city` varchar(32) NOT NULL default '',
  `player_roster_state` varchar(32) NOT NULL default '',
  `player_roster_zip` varchar(32) NOT NULL default '',
  `player_roster_phone` varchar(32) NOT NULL default '',
  `player_roster_email` varchar(32) NOT NULL default '',
  `player_roster_number` varchar(32) NOT NULL default '',
  `player_roster_gradyear` varchar(32) NOT NULL default '',
  `player_roster_height_feet` varchar(32) NOT NULL default '',
  `player_roster_height_inches` varchar(32) NOT NULL default '',
  PRIMARY KEY  (`player_id`),
  KEY `customers_id` (`customers_id`)
) TYPE=MyISAM AUTO_INCREMENT=119 ;

 

Here is the code...

 

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php

if( !empty( $_POST ) ) {
   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
    * showed you earlier.
   */
   foreach( $_POST['player'] as $player_id => $player_info ) {
     foreach( $player_info as $k => $v ) {
      $player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
      }
      echo "1<br />";
     if( $player_id < 0 ) {
         $player_info['primary_key'] = "'" . mysql_real_escape_string( $player_id ) . "'";
       $stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
         . " ) values ( " . implode( ', ', $player_info ) . " )";
          echo "2<br />";
      }else{
      foreach( $player_info as $k => $v ) {
         $player_info[$k] = '`' . $k . '`=' . $v;
          echo "3<br />";
      }
      $stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
         . "`primary_key`='" . mysql_real_escape_string( $player_id ) . "'";
          echo "4<br />"; 
     }
     $r = mysql_query( $stmt ); // check for errors and success
	if( !$r ) {
  		echo mysql_error() . "<br>";
	}
       echo "5<br />";
    }
   echo "5a<br />";
   exit(); return; // whatever is appropriate to stop processing
    echo "6<br />";
}

echo "<form action=\"roster.php\" method=\"post\"><table>";
   
$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'feet' => 'size="1"', 'inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer[$c] = '';
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
      $player = mysql_fetch_assoc( $result );
    }
   if( !$player ) {
      // We've run out of players, so create a blank one to insert
        $result = null; // stop trying to access result
      $player = $blankplayer;
        $player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   foreach( $cols as $c => $extra ) {
      echo "<td>" . tep_draw_input_field( "player[{$player['id']}][{$c}]", $row['player_roster_' . $c], $extra ) ."</td>";
    }
    $player = null; // important!
   echo "</tr>";
    
}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

 

Here is the html...

 

<form action="roster.php" method="post"><table><tr><td><input type="text" name="player[-1][fname]" size="10"></td><td><input type="text" name="player[-1][lname]" size="10"></td><td><input type="text" name="player[-1][address]" size="15"></td><td><input type="text" name="player[-1][city]" size="15"></td><td><input type="text" name="player[-1][state]" size="2"></td><td><input type="text" name="player[-1][zip]" size="10"></td><td><input type="text" name="player[-1][phone]" size="10"></td><td><input type="text" name="player[-1][email]" size="20"></td><td><input type="text" name="player[-1][number]" size="2"></td><td><input type="text" name="player[-1][gradyear]" size="4"></td><td><input type="text" name="player[-1][feet]" size="1"></td><td><input type="text" name="player[-1][inches]" size="4"></td></tr> </table><input type="submit" name="editplayers" value="Submit" />
</form>

 

Thanks again!

Link to comment
Share on other sites

The problem is in the code that outputs the form; here is your generated form:

<form action="roster.php" method="post">
<table>
<tr>
<td><input type="text" name="player[-1][fname]" size="10"></td>
<td><input type="text" name="player[-1][lname]" size="10"></td>
<td><input type="text" name="player[-1][address]" size="15"></td>
<td><input type="text" name="player[-1][city]" size="15"></td>
<td><input type="text" name="player[-1][state]" size="2"></td>
<td><input type="text" name="player[-1][zip]" size="10"></td>
<td><input type="text" name="player[-1][phone]" size="10"></td>
<td><input type="text" name="player[-1][email]" size="20"></td>
<td><input type="text" name="player[-1][number]" size="2"></td>
<td><input type="text" name="player[-1][gradyear]" size="4"></td>
<td><input type="text" name="player[-1][feet]" size="1"></td>
<td><input type="text" name="player[-1][inches]" size="4"></td>
</tr>
</table>

<input type="submit" name="editplayers" value="Submit"></form>

 

Remember I said the goal is for the name attributes to have the table column in them; these are all missing the player_roster_ prefix.  So we will add it in the code that generates the form.

 

The new code.  I've changed two lines and commented them with (MODIFIED TO ADD player_roster_ prefix):

<?php
require('includes/application_top.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
$navigation->set_snapshot();
tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php require('includes/form_check_coach.js.php'); ?>

</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->

<?php

if( !empty( $_POST ) ) {
   /**
   * If you print_r( $_POST ) you will find that you have an array named 'players'.
   * Each index into this array will be POSITIVE and the players database ID if they already exist in the database
   * The index will be NEGATIVE if the player is new and needs to be inserted.
   *
   * And then each player is an array where the associative names should match your column names,
   * so that you can easily generate your insert / update statements based on what I
* showed you earlier.
   */
   foreach( $_POST['player'] as $player_id => $player_info ) {
 foreach( $player_info as $k => $v ) {
  $player_info[$k] = "'" . mysql_real_escape_string( $v ) . "'";
  }
  echo "1<br />";
 if( $player_id < 0 ) {
	 $player_info['primary_key'] = "'" . mysql_real_escape_string( $player_id ) . "'";
   $stmt = "insert into `rosters` ( " . implode( ', ', array_keys( $player_info ) )
	 . " ) values ( " . implode( ', ', $player_info ) . " )";
	  echo "2<br />";
  }else{
  foreach( $player_info as $k => $v ) {
	 $player_info[$k] = '`' . $k . '`=' . $v;
	  echo "3<br />";
  }
  $stmt = "update `rosters` set " . implode( ', ', $player_info ) . " where "
	 . "`primary_key`='" . mysql_real_escape_string( $player_id ) . "'";
	  echo "4<br />";
 }
 $r = mysql_query( $stmt ); // check for errors and success
  if( !$r ) {
	echo mysql_error() . "<br>";
  }
   echo "5<br />";
}
   echo "5a<br />";
   exit(); return; // whatever is appropriate to stop processing
echo "6<br />";
}

echo "<form action=\"roster.php\" method=\"post\"><table>";

$result = mysql_query( $sqlplayers );
$maxrows = 15;
$insid = -1;
// create a blank player template
$cols = array( 'fname' => 'size="10"', 'lname' => 'size="10"', 'address' => 'size="15"', 'city' => 'size="15"', 'state' => 'size="2"',
   'zip' => 'size="10"', 'phone' => 'size="10"',
   'email' => 'size="20"', 'number' => 'size="2"', 'gradyear' => 'size="4"', 'feet' => 'size="1"', 'inches' => 'size="4"' );
$blankplayer = array();
foreach( $cols as $c => $extra ) {
   $blankplayer['player_roster_' . $c] = ''; // MODIFIED TO ADD player_roster_ prefix
}
// we now have a blank player template
for( $i = 1; $i <= $maxrows; $i++ ) {
   echo "<tr>";
   if( $result ) {
  $player = mysql_fetch_assoc( $result );
}
   if( !$player ) {
  // We've run out of players, so create a blank one to insert
	$result = null; // stop trying to access result
  $player = $blankplayer;
	$player['id'] = $insid--; // first blank player is id -1, second is -2, third is -3, etc.
   }
   // dump the fields
   foreach( $cols as $c => $extra ) {
  $c = 'player_roster_' . $c; // MODIFIED TO ADD player_roster_ prefix
  echo "<td>" . tep_draw_input_field( "player[{$player['id']}][{$c}]", $row[$c], $extra ) ."</td>";
}
$player = null; // important!
   echo "</tr>";

}
?>
</table><input type="submit" name="editplayers" value="Submit" />
</form>

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.