Jump to content

Recommended Posts

Hello all,

I have a site I am working on. Its a sports site and I am trying to add stats to a DB. I have a table that will have varying amounts of rows depending on how many kids are on the team. I need to take the rows of the stats table and be able to add them to the DB with a single submit button. Here is  the code for my table. The form is calling another php page for processing. I am thinking I will need a foreach to pull all the rows out of the table but not sure on how to reference the table in the POST.

Any help would be greatly appreciated.

<table align="center" width="780 px" name="aStatsInput">
<tr>
<td align="center" colspan="14"><b>Away Team </b>Bushnell-Prairie City/Avon</td>
</tr>
<tr>
<th style="table-layout: fixed; align: left; width: 100 px;">Player</th>
<th>fgM</th>
<th> fgA</th>

<th> ftM</th>
<th> ftA</th>
<th> 3ptM</th>
<th> 3ptA</th>
<th> OReb</th>

<th> DReb</th>
<th> Assts</th>
<th> Steals</th>
<th> Fouls</th>
<th> TFouls</th>

<th> Turnovrs</th>
</tr>
<tr bgcolor="white" id="row1">
<td>Hulett Dewain</td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>

<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
</tr>
<tr bgcolor="#aaaaaa" id="row2">
<td>Powell Cory</td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>

<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
<td><INPUT name="" type="text" size="1"></td>
</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/178269-solved-parse-multirow-html-table/
Share on other sites

you want to create an HTML array of your variables. something like

<td>Powell Cory</td>
<td><INPUT name="stat1[]" type="text" size="1"></td>
<td><INPUT name="stat2[]" type="text" size="1"></td>
<td><INPUT name="stat3[]" type="text" size="1"></td>

 

for each row, you probably want a hidden field to identify whose stat you are updating, like

<input type="hidden" name="player[]" />

 

now when you go to the submit page, you could do something like

foreach($_POST['player'] as $key => $player){
//every row has the same key in the post array, so the key for
//the player name's will be the same key for his stats. so we can do
$playerStat1 = $_POST['stat1'][$key];//and now we have the players stat1
//do the rest for the others

$sql = "update etc etc etc";
$query = mysql_query($sql);
//etc.
}//end foreach

 

Thanks this worked out great! However, When I post it I would like to echo the results of the post before I insert into the database. So now I can get it to echo all of the players stats. I would like for it to post the players name in front of the stats as well. Any further help would be greatly appreciated.

if you took my advice on the hidden player name field, you change the foreach to the following

 

foreach($_POST['player'] as $key => $player){

//every row has the same key in the post array, so the key for

//the player name's will be the same key for his stats. so we can do

$playerStat1 = $_POST['stat1'][$key];//and now we have the players stat1

//do the rest for the others

//get the name

$playerName = $_POST['name'][$key];

echo $playerName;

 

$sql = "update etc etc etc";

$query = mysql_query($sql);

//etc.

}//end foreach

mikesta707,

Thank you for all your help. I tried your suggestion and I did follow your advice on the hidden field. I added your extra code and it still only returns numbers for the stats. Any other help you could offer would help tremendously.

 

Thanks,

Cory

Mkesta707,

My code follows per your request.

This is the function that builds the table of players.

 

function populatePlayerTblNames($teamID) {	  
  //Query for players
  $aPlayers = mysql_query("SELECT l_name, f_name, number FROM bbball_stats WHERE team_ID = '$teamID' ORDER BY number")
      	or die(mysql_error());
  	  //Loop to display players
  while ($row = mysql_fetch_array($aPlayers)) {
   	  //Initialize the coutner for the alt row color if statement
  	  $i++;
  	  //Begin the row for the table
  	  //code to alternate color and build the table
  	  if($i % 2){//this divied $i by 2 and if there is a remainder
 	    echo "<tr bgcolor=\"white\" id=\"row" . $i . "\">\n";}
 	    else {//This means that $i divided by 2 is an even number
 	    echo "<tr bgcolor=\"#aaaaaa\" id=\"row" . $i . "\">\n";
 	    }
      //list the player name for the team
      echo "<td><input type=\"hidden\" name=\"player[]\" />" . $row["l_name"] . " " . $row["f_name"] . "</td>\n";	
	  //For loop to insert input boxes for player stats
      for ($r = 1; $r<14; $r++) {
       	//insert the input boxes
		echo "<td><INPUT name=\"stat" . "$r" . "[]\" type=\"text\" size=\"1\"></td>\n";
		//End input box for loop
	  }		
      echo "</tr>\n";
	  //End player while loop
  }
//End Function
}

 

This coded is my the code from the page that the form posts the information to. This includes the code from your second posting on trying to get the names with the stats.

 

foreach($_POST['player'] as $key => $player){
//every row has the same key in the post array, so the key for
//the player name's will be the same key for his stats. so we can do
$playerStat1 = $_POST['stat1'][$key];
//and now we have the players stat1
//do the rest for the others
$playerStat2 = $_POST['stat2'][$key];
$playerStat3 = $_POST['stat3'][$key];
$playerStat4 = $_POST['stat4'][$key];
$playerStat5 = $_POST['stat5'][$key];
$playerStat6 = $_POST['stat6'][$key];
$playerStat7 = $_POST['stat7'][$key];
$playerStat8 = $_POST['stat8'][$key];
$playerStat9 = $_POST['stat9'][$key];
$playerStat10 = $_POST['stat10'][$key];
$playerStat11 = $_POST['stat11'][$key];
$playerStat12 = $_POST['stat12'][$key];
$playerStat13 = $_POST['stat13'][$key];
    //Echo the player stats.
echo $playerStat1 . " " . $playerStat2 . " " . $playerStat3 . " " . $playerStat4 . " " . $playerStat5
. " " . $playerStat6 . " " . $playerStat7 . " " . $playerStat8 . " " . $playerStat9 . " " . $playerStat10
. " " . $playerStat11 . " " . $playerStat12 . " " . $playerStat13 . "<br />";

//This is the code I added from your second post.	
$playerName = $_POST['name'][$key];
echo $playerName;
}

 

Again thanks for all your help.

Cory

 

I thought that could have been the culprit before but it still doesn't bring the names with it. I just tried it to be sure and it didnt bring them again. I tried changing playerName = $_POST['name'][$key]; to playerName = $_POST['player'][$key]; as well as playerName = $_POST['player[]'][$key]; I am at a loss for what is keeping this from posting the names.

 

 

oh well one thing.

 

<input type=\"hidden\" name=\"player[]\" />"

 

needs a value attribute for it to pass anything. you probably want to make it

 

echo "<td><input type=\"hidden\" name=\"player[]\" value=\"" . $row["l_name"] . " " . $row["f_name"] . "\" />" . $row["l_name"] . " " . $row["f_name"] . "</td>\n";

 

  • 2 weeks later...

I wonder if you can get away by using the SS_WebPageToCSV script in biterscripting. That script extracts a table from a web page. This is how I tried it.

 

I copied the contents of your quoted html in file "C:/X.txt", then I called the script with the following command

 

script "C:/Scripts/SS_WebPageToCSV.txt" page("C:/X.txt") number(1)

 

The page() argument is the location of the html page. It can be http : / / www . something ... or a local file. I used a local file X.txt. The number() argument is the number of the table to extract - an html page may have several tables in it.

 

Anyway, this was the output.

 

Away Team Bushnell-Prairie City/Avon ,

 

Hulett Dewain , , , , , , , , , , , , , ,

 

Powell Cory , , , , , , , , , , , , , ,

 

Is this what you want ? If so, that may work for you. The open source code for that script is posted at http://www.biterscripting.com/SS_WebPageToCSV.html .

 

You can redirect the output of the command to a .csv file with > Y.csv, then import that file into the DB. You can do this periodically from cron/task scheduler if needed.

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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