Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 8th, 2004, 11:42 PM
Authorized User
Join Date: Nov 2004
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IP076
Default Looping Form Generation - Refering to Controls

I think this is above the beginner level, so, I put it here.

I have the following form generated (very simple version):

$numpoints = 3;
$endcount = $numpoints + 1;

echo "<form action='http://www.geft-online.org/test4.php' method='POST'>
        <table align='center' border='1' cellspacing='2' cellpadding='2'>
        <tr><th align='center' colspan='3'>Enter Point Information Below
        <input type='text' name='numpoints' value='$numpoints'</th></tr><form>";

for($counter=1; $counter<$endcount; $counter++)
  echo "<tr><td><input type='text' name='test$counter'><td><tr>";

  echo "<tr><td colspan='3' align='center'><input type='submit' value='Submit'></td></tr>";
echo "</form></table>";


Which, refers to this page to run the data:

$point_id = 0;

$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
 or die('Could not connect to MySQL database. ' . mysql_error());

$numpoints = $_POST['numpoints'];
$endcount = $numpoints + 1;
for($counter=1; $counter<$endcount; $counter++)
echo $_POST['test$counter'];
$sql = "INSERT IGNORE INTO tbltest VALUES (NULL, ' " . $_POST[' " . "test" . $counter . "'] . " ')";

mysql_query($sql) or die("Its ****ED!" . mysql_error());
echo $numpoints;

Its the values part of the SQL I cant seem to get to work.

My form is generated, and the control is named test$counter...it works out to be test1, test2, test3, and so on....

Now, I guess I need to know if I can use a variable in the $_POST statement, such as $_POST['test$counter'], to retrieve the data for test1, test2, and all the other ones.

If anyone knows that you can do this for sure, or maybe has a different way of collecting the data, I'd greatly appreciate the help. I'm basically a hobbyist, doing this to help out a college team I volunteer coach, and looking to create a website the team can use to track stats and results. Thanks for the info in advance!

Reply With Quote
  #2 (permalink)  
Old December 10th, 2004, 08:13 PM
Friend of Wrox
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts

quote:Now, I guess I need to know if I can use a variable in the $_POST statement, such as $_POST['test$counter'], to retrieve the data for test1, test2, and all the other ones.
The best way to know if you can do something is to try it ;)

Logically, this is possible since $_POST is just an array and the index (the part within the brackets) is just a string.

Replace the single quotes with double quotes so that the PHP processor looks in the string for variables, like this:


With single quotes, the string will literally be "test$counter" but with double quotes it would be something like "test1" or "test2" etc.


-Snib - http://www.snibworks.com
Where will you be in 100 years?
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic generation of asp.net server controls shashi1312 Classic ASP XML 1 August 2nd, 2005 04:52 AM
Looping controls and array redim mega Excel VBA 2 April 19th, 2005 11:57 AM
Dynamic generation of controls in ASP.NET Maxood ASP.NET 1.0 and 1.1 Basics 2 March 8th, 2004 07:29 PM
Looping Through Controls stu9820 ASP.NET 1.0 and 1.1 Professional 2 March 4th, 2004 01:40 PM
Looping through Controls on a Page jbenson001 ASP.NET 1.x and 2.0 Application Design 2 December 4th, 2003 03:20 PM

All times are GMT -4. The time now is 12:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.