Jump to content

Easier Way To Make Almost 80 Insert Boxes


Recommended Posts

so in the past week or so i've learned quite a bit about how to insert, delete, update, and display data using php and sql. i'm finishing up a project with one last step, and thats to insert data. its a game league db..so for each game we enter the stats the players obtained during the game. now usually with update and array..i can display the blanks filled with input thats already there for pre-existing information. with insert...i can call for it, but nothing exists to show blanks. i have to create tons of forms and tons of variables. the pic is an example, but not a completely finished example. i don't even have the clan names/score and what not factored in. i'd have to make 12x6 rows with blanks to make variables for. there's certainly got to be an easier way than to define almost 80 variables. cause not only would i have to type it in the tables..but in the actual update script too. any ideas how to make my life easier? 2uix003.jpg

Edited by Greysoul
Link to post
Share on other sites
  • Replies 95
  • Created
  • Last Reply

Top Posters In This Topic

If you have 12 players with 6 columns each, then you need 12x6 inputs. You can loop through things though, you don't need to write code to handle each variable individually. Just loop through the rows and check for the 6 fields on each row. You can also write a loop to display the tables.

Link to post
Share on other sites
If you have 12 players with 6 columns each, then you need 12x6 inputs. You can loop through things though, you don't need to write code to handle each variable individually. Just loop through the rows and check for the 6 fields on each row. You can also write a loop to display the tables.
you know i really hadn't thought of it that way. always looked at the loops for pulling data. this is what i did and it worked, tell me if i went about it the right way?
<?php$i=1;do  {  $i++;  ?><tr><td align="center" bgcolor="#FFCC00"><strong><input type="checkbox" name="playercheckbox" id="playercheckbox"></strong></td><td align="center" bgcolor="#FFFFCC"><strong><input size="14" type="text" name="player.$i" id="player" value=""></strong></td><td align="center" bgcolor="#FFFFCC"><strong><input size="1" type="text" name="score.$i" id="score" value=""></strong></td><td align="center" bgcolor="#FFFFCC"><strong><input size="1" type="text" name="kills.$i" id="kills" value=""></strong></td><td align="center" bgcolor="#FFFFCC"><strong><input size="1" type="text" name="deaths.$i" id="deaths" value=""></strong></td><td align="center" bgcolor="#FFFFCC"><strong><input size="1" type="text" name="caps.$i" id="caps" value=""></strong></td><td align="center" bgcolor="#FFFFCC"><strong><input size="1" type="text" name="attempts.$i" id="attempts" value=""></strong></td></tr><?  }while ($i<=6);?>

anything different you would of done?

Link to post
Share on other sites

For starters, you should get those align and bgcolor attributes out of your td tags. Keep that stuff in CSS in either style tags in the head or in an external style sheet. Notice that I changed this info to classes.I'm pretty sure that your inputs are not going to be named the way you expect. You are referencing $i outside your php tags. Use echo to write your html.Also notice I changed your names into arrays and added $tables and $i to the id instead. This is so you have a unique id. The names being arrays makes it possible to loop through your data when/if you submit.Also, like jsg said, you can use one loop for all your tables.

<?php$tables = 1;$i=1;doecho "<tr>....Header row information....</tr>\n";do  {  $i++;echo "<tr>\n";echo "<td class='cellHdr'><strong><input type='checkbox' name='playercheckbox[]' id='playercheckbox".$tables.$i."'></strong></td>\n";echo "<td class='cell'><strong><input type='text' name='player[]' id='player".$tables.$i."' value=''></strong></td>\n";...echo "</tr>\n";  }while ($i<=6);}while ($tables <= 3);?>

Edited by jkloth
Link to post
Share on other sites

$i starts at 1 and then immediately gets incremented, so the first fields with have the number 2. You'll also want to make sure $table gets incremented so that the loop ends, you can do that here:while (++$tables <= 3);

Link to post
Share on other sites
$i starts at 1 and then immediately gets incremented, so the first fields with have the number 2. You'll also want to make sure $table gets incremented so that the loop ends, you can do that here:while (++$tables <= 3);
Oops! :) Yea thanks!
Link to post
Share on other sites

out of curiosity i ran that just to see how it worked, so i can understand better :/Parse error: syntax error, unexpected T_DO, expecting T_WHILE in scheduleedit.php on line 46if i go down the list and change everything it says to change it just spams my page with the header info

Link to post
Share on other sites

Also note that "...Header Row information..." should be your table headers (The rows across the top of your tables) and the other "..." should be your other <td> tags.I only put that stuff there so I didn't have to type out all of your code.

Link to post
Share on other sites

alright almost done. its giving me two tables, but i guess i was under the assumption it was going to fill 6 rows in each. oddly it puts one row, i'm not sure how that works though. included a pic so you can see the output.

<form id="insertmatch" name="insertmatch" method="post" action=""><?php$tables = 1;$i=1;do{echo "<table border='1' cellpadding='3' cellspacing='0'><tr><td align='center' bgcolor='#FFCC00'><strong>Select</strong></td><td align='center' bgcolor='#FFCC00'><strong>Player</strong></td><td align='center' bgcolor='#FFCC00'><strong>Score</strong></td><td align='center' bgcolor='#FFCC00'><strong>Kills</strong></td><td align='center' bgcolor='#FFCC00'><strong>Deaths</strong></td><td align='center' bgcolor='#FFCC00'><strong>Caps</strong></td><td align='center' bgcolor='#FFCC00'><strong>Attempts</strong></td></tr>";do  {  $i++;echo "<tr>\n";echo "<td class='cellHdr'><strong><input type='checkbox' name='playercheckbox[]' id='playercheckbox".$tables.$i."'></strong></td>\n";echo "<td class='cell'><strong><input type='text' name='player[]' id='player".$tables.$i."' value='' size='12'></strong></td>\n";echo "<td class='cell'><strong><input type='text' name='score[]' id='score".$tables.$i."' value='' size='2'></strong></td>\n";echo "<td class='cell'><strong><input type='text' name='kills[]' id='kills".$tables.$i."' value='' size='2'></strong></td>\n";echo "<td class='cell'><strong><input type='text' name='deaths[]' id='deaths".$tables.$i."' value='' size='4'></strong></td>\n";echo "<td class='cell'><strong><input type='text' name='caps[]' id='caps".$tables.$i."' value='' size='2'></strong></td>\n";echo "<td class='cell'><strong><input type='text' name='attempts[]' id='attempts".$tables.$i."' value='' size='6'></strong></td>\n";echo "</tr>\n";  }while ($i<=6);}while (++$tables <= 2); echo "</table>";echo "<input type='Submit' value='Add' name='submit'>";?></form>

yeah i know the string style is annoying but i'm learning for function, not for anything else. however i suppose it would clean up the code quite a bit :/ 106dlbs.jpg

Edited by Greysoul
Link to post
Share on other sites

You need to reset $i after the inside loop, note the inside loop goes until $i equals 6, but never resets $i for the next table. It would actually be better to start $i at 0 instead of 1, or increment it at the end of the loop instead of the beginning.

Link to post
Share on other sites

i hate to say this, but this is turning into an obvious, "sorry guys i don't know what the ###### i'm doing". sorry for the lack of knowledge in piecing together commands. i changed the $i to 0.is this what you meant by reset $i?echo reset($i); i tried putting that on several different lines and all i got back were errors. wasn't much example of it on the tutorials.

Link to post
Share on other sites

What he meant by "reset $i" was to insert a line inside the first do loop that sets it back to its initial value. Like this:do{//...header...//reset $i right here$i = 0; //I set it to 0 since you said you changed itdo{//...rows}while()}while()The bolded line is all you need to add. The rest is just to give you an idea of the context of where the line should be placed.

Edited by jkloth
Link to post
Share on other sites
What he meant by "reset $i" was to insert a line inside the first do loop that sets it back to its initial value. Like this:do{//...header...//reset $i right here$i = 0; //I set it to 0 since you said you changed itdo{//...rows}while()}while()The bolded line is all you need to add. The rest is just to give you an idea of the context of where the line should be placed.
wow i didn't know it'd be so easy to make that happen. here's a couple of basic questions for you.1.) it made two tables, i see no where that defines the table info..how does it know to make two before the first one is even done? because as far as i can tell..$table is just a number, how did it magically create two different tables from that? 2.) what do the plus signs on this mean? $i++; and while (++$tables <= 2); Edited by Greysoul
Link to post
Share on other sites
wow i didn't know it'd be so easy to make that happen. here's a couple of basic questions for you.1.) it made two tables, i see no where that defines the table info..how does it know to make two before the first one is even done? because as far as i can tell..$table is just a number, how did it magically create two different tables from that? 2.) what do the plus signs on this mean? $i++; and while (++$tables <= 2); 3.) how do i get a break in between the two tables
1. It knows to create two tables with this line: while (++$tables <= 2);The 2 is a limit on how many times the loop is run. As long as $tables is less than or equal to 2 it's going to keep creating tables. So you could assign a global variable to avoid hard coding that which makes it easier to maintain when you need to change how many tables are created:$numTables = 2; //This is the global variable that defines how many tables there should bedo{//...header...do{//...rows}while()}while($tables <= $numTables)2. The plus signs (++) are increment operators. It adds one to the value of the variable. So if $i = 1 and you execute $i++ then $i = 23. Just echo a break after the loop that creates the rows:do{//...header...do{//...rows}while()echo "<br />";}while()Or you could give your tables a margin-bottom. Edited by jkloth
Link to post
Share on other sites
alright i understand. i guess the only other thing i'm curious on is the []'s on each form name. i know it has something to do with the array?
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... :)
Link to post
Share on other sites

Just to clarify, there are slight differences between these:$i++++$tableObviously the ++ operator is in a different spot. I guess the easiest way to explain is to use an example which just echos the values.For the form $i++, the code will first get the current value of $i, and then increment the value. For ++$i, the code will increment the value first, then get the new value. You can see that with a loop like this:

$i = 0;$j = 0;for ($k = 0; $k < 3; $k++) // loop 3 times{  echo 'i: ' . ($i++) . '<br>';  echo 'j: ' . (++$j) . '<br>';}

When that loop runs 3 times it will increment and print $i and $j, but it will do them in a different order. Since it prints $i before it increments it, you'll see it print 0, 1, and 2 for $i. Since it increments $j before printing it, for $j it will print 1, 2, 3.

Link to post
Share on other sites
Just to clarify, there are slight differences between these:$i++++$tableObviously the ++ operator is in a different spot. I guess the easiest way to explain is to use an example which just echos the values.For the form $i++, the code will first get the current value of $i, and then increment the value. For ++$i, the code will increment the value first, then get the new value. You can see that with a loop like this:
$i = 0;$j = 0;for ($k = 0; $k < 3; $k++) // loop 3 times{  echo 'i: ' . ($i++) . '<br>';  echo 'j: ' . (++$j) . '<br>';}

When that loop runs 3 times it will increment and print $i and $j, but it will do them in a different order. Since it prints $i before it increments it, you'll see it print 0, 1, and 2 for $i. Since it increments $j before printing it, for $j it will print 1, 2, 3.

Now, that's what I thought, too. But I couldn't find anything on W3Schools indicating this so I tested it a while back. When I did what you just did, $i and $j were equal, so I didn't think the order was important. Maybe I should test that again......strange, it did what you said it would. Maybe there was something faulty with my first test. Oh well, you're right. That's what counts. :) Edited by jkloth
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...