Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2004, 11:42 PM
Authorized User
 
Join Date: Nov 2004
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):
<?php

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

<?php
require('config.php');
$point_id = 0;

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

$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;
echo "CHECK DATABASE!";
?>

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!

 
Old December 10th, 2004, 08:13 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Quote:
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:

$_POST["test$counter"]

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

HTH!

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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.