RE: [PHP-DB] Variable Variables in PHP

2001-06-27 Thread Taylor, Stewart

?

You are allowed the have input fields with the same name on a form, the
browser creates an array of these fields,
e.g. to access them using javascript form.name[index].value.

You need to inform php that these fields are arrays by putting an '[]' after
the INPUT fields name.  Then when the form is submitted php will create
arrays for the multiple input fields.

You can also use an index value to specify each fields location in the
array.


$result = mysql_query(SELECT * FROM name_and_email);
$num = mysql_numrows($result);
if ($num!=0) {
$i = 0;
PRINT FORM action='test.php';
WHILE ($i  $num) {
$name = mysql_result($result,$i,name);
$email = mysql_result($result,$i,email);
$id = mysql_result($result,$i,id);
PRINT INPUT type='text' name='name[$id]' value='$name';
 modified
PRINT INPUT type='text' name='email[$id]' value='$email'BR;
 modified

$i++;
}
PRINT INPUT type='submit'/FORM;
}

?


To create the query you can simple loop through the arrays
e.g.
foreach ($name as $k=$v)
{
   $sql = sprintf(UPDATE .
  name_and_email .
  SET .
  name = '%s',email = '%s' .
  WHERE .
  id = '%s', $v, $email[$k], $k);
   // run query etc..
}

-Original Message-
From: Morgan Tocker [mailto:[EMAIL PROTECTED]]
Sent: 27 June 2001 03:04
To: [EMAIL PROTECTED]
Subject: [PHP-DB] Variable Variables in PHP


I am having trouble with variable variables;

I want to create a form as follows :-

?

$result = mysql_query(SELECT * FROM name_and_email);
$num = mysql_numrows($result);
if ($num!=0) {
$i = 0;
PRINT FORM action='test.php';
WHILE ($i  $num) {
$name = mysql_result($result,$i,name);
$email = mysql_result($result,$i,email);
$id = mysql_result($result,$i,id);
PRINT INPUT type='text' name='name$id' value='$name';
PRINT INPUT type='text' name='email$id' value='$email'BR;

$i++;
}
PRINT INPUT type='submit'/FORM;
}

?


## THIS SHOULD SUBMIT SOMETHING LIKE THIS 

test.php?name1=avalueemail1=avalueagainname2=secondnameemail2=secondemail


THE PROBLEM:
From PHP with variable variables, how do I change $name1 and $email1 into -

## for all $name.$x values do this..
$sql = UPDATE name_and_email SET name = '$name1', email = '$email1' WHERE
id = '$id';


Many thanks if someone can help,
Morgan Tocker.



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP-DB] Variable Variables in PHP

2001-06-27 Thread Jason Stechschulte

On Wed, Jun 27, 2001 at 12:03:42PM +1000, Morgan Tocker wrote:
 ## THIS SHOULD SUBMIT SOMETHING LIKE THIS 
 
 test.php?name1=avalueemail1=avalueagainname2=secondnameemail2=secondemail
 
 THE PROBLEM:
 From PHP with variable variables, how do I change $name1 and $email1 into -
 
 ## for all $name.$x values do this..
 $sql = UPDATE name_and_email SET name = '$name1', email = '$email1' WHERE
 id = '$id';

Put it inside of a loop.

for($i = 1; $i  3; $i++) {
   echo ${name$i};
}

This will print out both names.  Hopefully you can figure out how to
modify this to create the sql code you want.

-- 
Jason Stechschulte
[EMAIL PROTECTED]
--
It's the Magic that counts.
 -- Larry Wall on Perl's apparent ugliness

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]