Jump to content
Greysoul

Easier Way To Make Almost 80 Insert Boxes

Recommended Posts

Note if you do this, they will both print the same:

$i++;++$j;echo 'i: ' . ($i) . '<br>';echo 'j: ' . ($j) . '<br>';

This will print them differently:

  echo 'i: ' . ($i++) . '<br>';  echo 'j: ' . (++$j) . '<br>';

The difference is the order of incrementing vs. reading the value. In the first code, both values are already incremented by the time they get printed.

Share this post


Link to post
Share on other sites
I know how they work, I'm just not sure I can explain it well enough. :) So if anyone has a better explanation, then by all means...They create an array when you POST your variables to a PHP script, to save the information for example. This makes it much easier to get all of the information you need. You can just create a loop and get all the information for player by referencing the POST variable (ie. $_POST['player'][0]) instead of creating a unique name for each player (ie. playerone, playertwo, etc.).I hope I've made it at least a little clearer... :)
is there anything extra i need to add to generate the array in my _POST? obviously if i change all the posts to 0's or 1's it will only post one of the rows to the DB. and if i leave the brackets empty i just get an error saying i can't do that :) oh wait, i think you answered that in the post above saying i need to create a loop, is there any special loop to use in this case?Fatal error: Cannot use [] for reading in scheduleedit.php on line 80
<?phpif($_POST['Submit']){$Player=$_POST['player'][];$Score=$_POST['score'][];$Kills=$_POST['kills'][];$Deaths=$_POST['deaths'][];$Caps=$_POST['caps'][];$Attempts=$_POST['attempts'][];$query="INSERT INTO Scorecard(ID, Player, Score, Kills, Deaths, Caps, Attempts, GameID)VALUES ('NULL','".$Player."','".$Score."','".$Kills."','".$Deaths."','".$Caps."','".$Attempts."','".$matchid."')";mysql_query($query) or die ('Error updating database');}?>

Edited by Greysoul

Share this post


Link to post
Share on other sites
i think you answered that in the post above saying i need to create a loop, is there any special loop to use in this case?
Exactly :)There is no "special loop" its just whatever you prefer. I personally prefer for loops, but you can use whichever you like. (Though on second thought maybe a while loop might work better)I'll use a while loop for this. You're code should look something like this (I think):
<?phpif($_POST['Submit']){$index = 0;while (isset($_POST['player'][$index])) {$Player=$_POST['player'][$index];$Score=$_POST['score'][$index];$Kills=$_POST['kills'][$index];$Deaths=$_POST['deaths'][$index];$Caps=$_POST['caps'][$index];$Attempts=$_POST['attempts'][$index];$query="INSERT INTO Scorecard(ID, Player, Score, Kills, Deaths, Caps, Attempts, GameID)VALUES ('NULL','".$Player."','".$Score."','".$Kills."','".$Deaths."','".$Caps."','".$Attempts."','".$matchid."')";mysql_query($query) or die ('Error updating database');}}?>

Share this post


Link to post
Share on other sites
Exactly :)There is no "special loop" its just whatever you prefer. I personally prefer for loops, but you can use whichever you like. (Though on second thought maybe a while loop might work better)I'll use a while loop for this. You're code should look something like this (I think):
<?phpif($_POST['Submit']){$index = 0;while (isset($_POST['player'][$index])) {$Player=$_POST['player'][$index];$Score=$_POST['score'][$index];$Kills=$_POST['kills'][$index];$Deaths=$_POST['deaths'][$index];$Caps=$_POST['caps'][$index];$Attempts=$_POST['attempts'][$index];$query="INSERT INTO Scorecard(ID, Player, Score, Kills, Deaths, Caps, Attempts, GameID)VALUES ('NULL','".$Player."','".$Score."','".$Kills."','".$Deaths."','".$Caps."','".$Attempts."','".$matchid."')";mysql_query($query) or die ('Error updating database');}}?>

that appears to of initiated an explosion in my DB, its added over 40,000 records and its not stopping. my id count was 8 before that, now its at 1453153. Edited by Greysoul

Share this post


Link to post
Share on other sites
that appears to of initiated an explosion in my DB, its added over 40,000 records and its not stopping. my id count was 8 before that, now its at 1453153.
Dag gommit. :) I forgot to increment $index. I apologize for that, really I do! That's the one thing I always forget when I'm not working with for loops.Just insert $index++; somewhere inside the loop.

Share this post


Link to post
Share on other sites

no worries, i think that made it work! remade the table..that'll fixer lol. thanks for showing me what not to do. that's an evil prank to play on someone lol

Edited by Greysoul

Share this post


Link to post
Share on other sites

Yea really sorry about that. I guess I gotta remember to double check my code when I post. Especially loops. :) I'm glad to hear you got it all sorted out though! :)

Share this post


Link to post
Share on other sites
jkloth did you get my pm? or do they not work on this board
I got it. Just replied.

Share this post


Link to post
Share on other sites

few problems:first of all, in order to show that information just how i put it in, i have to have some sort of identifier to order it by so that i can group them correctly. in this case, the name of the clan for each player would do. but the way its set up to loop 2 tables, i don't know how to make the first table = clan1 and the second table to = clan2. the clans would come from the match ID gotten from the URL. they are labeled as green and red for each match ( thats the colors of the teams ). also i can only seem to make one outpout table to show this info..i tried to duplicate how it was for the input table..but to no avail.the second problem is, each team has 4 players and 2 subs. they may not use their 2 subs in the game, so all 6 blanks may not be filled out. right now, if add is pressed, it inserts data for all blanks, even if they are empty. so i end up with a bunch of empty rows in my db table. how do i make it where if the blank is empty, it doesn't insert a row? you don't have to code it out for me if you'd rather explain the idea of what functions to use..i don't want you to think i'm just getting you to do this for me. i really want to learn how to it myself. thanks!an example of what i'm trying to accomplish: http://sparkgn.com/d.php?p=Scorecard&&sID=53my entire page of code, delete button at the bottom doesn't work yet

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><body class="body"><head><link rel="stylesheet" type="text/css" href="styles.css" /></head> <?php include("weekselection.php"); ?> <?phpif (isset($_GET['match'])) {$matchid= $_GET['match'];} echo "<form id='insertmatch' name='insertmatch' method='post' action=''>"; $clans=mysql_query("SELECT * FROM Schedule WHERE GameID='$matchid'"); while($row=mysql_fetch_array($clans)){echo "<table border='1' cellpadding='3' cellspacing='0'>";echo "<tr>";echo "<td bgcolor='FFCC00' align='center'>" . "<strong>" . "Clan" . "</strong>" . "</td>";echo "<td bgcolor='FFCC00' align='center'>" . "<strong>" . "Score" . "</strong>" . "</td>";echo "</tr>";echo "<form id='insertmatch' name='insertmatch' method='post' action=''>";echo "<tr>";echo "<td bgcolor='FFCC00' align='center'>" . "<strong>" . $row['Green'] . "</strong>" . "</td>";echo "<td>" . "<input type='text' name='green' id='green' value='' size='2'>" . "</td>\n";echo "</tr>";echo "<tr>";echo "<td bgcolor='FFCC00' align='center'>" . "<strong>" . $row['Red'] . "</strong>" . "</td>";echo "<td>" . "<input type='text' name='red' id='red' value='' size='2'>" . "</td>\n";}echo "</tr>";echo "</table";echo "<br>";   $tables = 1;$i=0;do{echo "<table class='table'><tr><th class='th'>Player</th><th class='th'>Score</th><th class='th'>Kills</th><th class='th'>Deaths</th><th class='th'>Caps</th><th class='th'>Attempts</th><th class='th'>MVP</th></tr>";$i=0;do  {  $i++;echo "<tr>\n";echo "<td class='td'><input class='form' type='text' name='player[]' id='player".$tables.$i."' value='' size='12'></td>\n";echo "<td class='td'><input class='form' type='text' name='score[]' id='score".$tables.$i."' value='' size='3'></td>\n";echo "<td class='td'><input class='form' type='text' name='kills[]' id='kills".$tables.$i."' value='' size='2'></td>\n";echo "<td class='td'><input class='form' type='text' name='deaths[]' id='deaths".$tables.$i."' value='' size='4'></td>\n";echo "<td class='td'><input class='form' type='text' name='caps[]' id='caps".$tables.$i."' value='' size='2'></td>\n";echo "<td class='td'><input class='form' type='text' name='attempts[]' id='attempts".$tables.$i."' value='' size='6'></td>\n";echo "<td class='td'><input class='form' type='radio' name='mvpradio[]' id='mvpradio".$tables.$i."' value='1'></td>\n";echo "</tr>\n";  }  while ($i<=5);} while (++$tables <= 2); echo "<br>";echo "</table>";echo "<br>";echo "<input type='Submit' value='Add Match' name='Submit'>";echo "</form>";?><br><br> <?phpif($_POST['Submit']){$index = 0;while (isset($_POST['player'][$index])) {$Player=$_POST['player'][$index];$Score=$_POST['score'][$index];$Kills=$_POST['kills'][$index];$Deaths=$_POST['deaths'][$index];$Caps=$_POST['caps'][$index];$Attempts=$_POST['attempts'][$index];$MVP=$_POST['mvpradio'][$index];$index++;  $query="INSERT INTO Scorecard(ID, Player, Score, Kills, Deaths, Caps, Attempts, GameID, MVP)VALUES ('NULL','".$Player."','".$Score."','".$Kills."','".$Deaths."','".$Caps."','".$Attempts."','".$matchid."','".$MVP."')";mysql_query($query) or die ('Error updating database');}} echo "<table class='table'><tr><th class='th'>Player</th><th class='th'>Score</th><th class='th'>Kills</th><th class='th'>Deaths</th><th class='th'>Caps</th><th class='th'>Attempts</strong></th><th class='th'>MVP</strong></th></tr>"; $stats=mysql_query("SELECT * FROM Scorecard WHERE GameID='$matchid' ORDER BY ID");while($row=mysql_fetch_array($stats)){echo "<tr>\n";echo "<td class='tdshow'>" . $row['Player'] . "</td>";echo "<td class='tdshow'>" . $row['Score'] . "</td>";echo "<td class='tdshow'>" . $row['Kills'] . "</td>";echo "<td class='tdshow'>" . $row['Deaths'] . "</td>";echo "<td class='tdshow'>" . $row['Caps'] . "</td>";echo "<td class='tdshow'>" . $row['Attempts'] . "</td>";echo "<td class='tdshow'>" . $row['MVP'] . "</td>";echo "</tr>\n";}echo "</table>";echo "<br>";echo "<form id='deletematch' name='deletematch' method='post' action=''>";echo "<input type='Submit' value='Delete Match' name='Delete'>";echo "</form>";if($_POST['Delete']){mysql_query("DELETE FROM Scorecard WHERE GameID='$matchid'");}if(mysql_query==true){echo "Successfully Deleted Match Results" . "<br>";}?></body></html>

Edited by Greysoul

Share this post


Link to post
Share on other sites

Well, let's see...Checking for the blank rows is easy. You just need a conditional inside the while loop that has your insert query. I think you can get away with testing for an empty string (""). If it's not empty update the db. You can use any of your variables ($Player, $Score, etc.) for the comparison.As for the clans and tables, I'm not really sure yet. I see these two lines:

...echo "<td bgcolor='FFCC00' align='center'>" . "<strong>" . $row['Green'] . "</strong>" . "</td>";...echo "<td bgcolor='FFCC00' align='center'>" . "<strong>" . $row['Red'] . "</strong>" . "</td>";...

Are $row['Green'] and $row['Red'] the names or ids of your two clans?

Share this post


Link to post
Share on other sites

they are the column names for the clans. green=clan2 red=clan1 . green is always the visitor so it comes first.just a note, i don't have the top table for clans working, i don't have it linked at all actually. hadn't gotten that far yet.

Edited by Greysoul

Share this post


Link to post
Share on other sites

I'm not really sure how you could do that...hmm....Didn't I tell you I wasn't the greatest at this. :)Anyways, you could maybe tack on $tables to the name of your fields. Then your fields could look like this:In table 1:...name='tbl1player[]'......name='tbl1score1[]'...In table 2:...name='tbl2player[]'......name='tbl2score2[]'...You'd have to change a few things about your insert query loop and probably some other stuff, but without really knowing how your db is setup and the relationships between the tables I think that's about as good as I can do. Maybe something will hit me though, so I'll keep at it! :)

Share this post


Link to post
Share on other sites

Oh yea, I forgot to tell you. Your insert query is a little off too. The number of fields and the number of VALUES doesn't match. You're missing a value.

$query="INSERT INTO Scorecard(ID, Player, Score, Kills, Deaths, Caps, Attempts, GameID, MVP)VALUES ('NULL','".$Player."','".$Score."','".$Kills."','".$Deaths."','".$Caps."','".$Attempts."','".$matchid."','".$MVP."')";mysql_query($query) or die ('Error updating database');

Share this post


Link to post
Share on other sites
Oh yea, I forgot to tell you. Your insert query is a little off too. The number of fields and the number of VALUES doesn't match. You're missing a value.
$query="INSERT INTO Scorecard(ID, Player, Score, Kills, Deaths, Caps, Attempts, GameID, MVP)VALUES ('NULL','".$Player."','".$Score."','".$Kills."','".$Deaths."','".$Caps."','".$Attempts."','".$matchid."','".$MVP."')";mysql_query($query) or die ('Error updating database');

yeah i had Clan in there when testing, i took it out. ;D

Share this post


Link to post
Share on other sites
yeah i had Clan in there when testing, i took it out. ;D
can i just incorporate the clan table into the actual player tables? or are these two player tables going to have to be split off from one another Edited by Greysoul

Share this post


Link to post
Share on other sites
can i just incorporate the clan table into the actual player tables?
Yea you can probably do that. As long as you can create a clean link with your SQL statements it shouldn't be a problem.Actually, on second thought. You have clan id's stored in the matches table I presume? You could create two more arrays of the players for each clan and then use two while loops to loop through the array for each clan and print the player information in your tables.

Share this post


Link to post
Share on other sites
Yea you can probably do that. As long as you can create a clean link with your SQL statements it shouldn't be a problem.Actually, on second thought. You have clan id's stored in the matches table I presume? You could create two more arrays of the players for each clan and then use two while loops to loop through the array for each clan and print the player information in your tables.
i do, i just can't figure out how to insert the clan name of the player in all of this. from what i can tell it looks like i have to do away with the table duplication loop and just have two seperate ones. Edited by Greysoul

Share this post


Link to post
Share on other sites

Ok so you need to assign the clan names from the matches table to some variables.

$clan1_ID = $clans['Green'] //Or whatever the column name for the clan id is$clan2_ID = $clans['Red']

Then you construct a new query to pull all the players from your players table where their clanID matches $clan1_ID

$sql = "SELECT * FROM PlayersTbl WHERE ClanID = '".$clan1_ID."'";$clan1 = mysql_query($sql);$sql = "SELECT * FROM PlayersTbl WHERE ClanID = '".$clan2_ID."'";$clan2 = mysql_query($sql);

Then loop through the arrays and create your tables using two loops. Since you already have the clan ID's stored in variables you can just print them out:

echo "<h3>".$clan1_ID."</h3>\n";......While loop to print player info......echo "<h3>".$clan2_ID."</h3>\n";......While loop to print player info......

I just used <h3> tags to make it simple. You can incorporate the clan names into your table however you'd like. Notice though that they are outside the loops. The only thing that should be in the loops is the <tr> and <td> tags for the players.I think that should get you on the right track...**I just noticed your edit on your last post. Yes, you are probably going to have to do away with the loop we showed you before that creates the two tables and make two separate loops.***Well I'm out for the day. I'll catch up with you tommorrow.

Edited by jkloth

Share this post


Link to post
Share on other sites

i couldn't figure out how to make the clan of the player be red or green...so when displaying it i just call for the first 6 to be shown as the green team and the last 6 as the red. it's all ordered by #'s so technically it works. unfortunatly it's like duck taping it though, lol.

Share this post


Link to post
Share on other sites

Did you follow my example above? If you have the clanID stored in the matches table and the clanID in the players table you shouldn't have any trouble. Show me the code that you have.Maybe I'm confused as to how your db is setup? If you could, also maybe explain a little about how your db is setup. For example, what tables you have and the relationships between the tables. I don't need to know everything just the important stuff, like how the players and clans are linked to each other and to the matches table. I think that will help.

Share this post


Link to post
Share on other sites

some days my brain doesn't work as well as others...yesterday i didn't understand your example and now it makes total sense duh. i'll rework it that way and see how it works. my db is setup like this:greysoul_leaguedb (4) * Structure Clans * Structure Players * Structure Schedule * Structure ScorecardClans has all the clans and tallied stats over time, as well as other additional information in regards to that clan.Players has all the players and total tallied stats over time.Schedules has the green vs red, green score-red score, date and timeScorecard has all the players for each match identified by that match ID. Schedules and scorecard both have a "GameID" field which match up via the get URL.the matches insert page basically has to...insert into scorecard, update the players, insert the score into schedule, and update the clan information(this one i'm a little fuzzy on). i couldn't think of any other way to do it unless i can just void the clan and player table completely and just do the math on the page that actually displays the info to the public. it's not just addition..there's division as well. not sure how that would work. i'm familiar with sql sum only :|

Share this post


Link to post
Share on other sites
some days my brain doesn't work as well as others...yesterday i didn't understand your example and now it makes total sense duh.
No worries! Happens to me all the time! :)
the matches insert page basically has to...insert into scorecard, update the players, insert the score into schedule, and update the clan information(this one i'm a little fuzzy on). i couldn't think of any other way to do it unless i can just void the clan and player table completely and just do the math on the page that actually displays the info to the public. it's not just addition..there's division as well. not sure how that would work. i'm familiar with sql sum only :|
Well as far as updating the clan information that should be relatively easy. You already have all of the players' stats being submitted so all you'd have to do is loop through the POST data for each player from the clan and do whatever calculations you need to do. You don't have to worry about using SQL to perform the calculations. Do that with PHP and only update your clans table with the results.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...