Page 1 of 1

Help with variable refs and concat - simple?

Posted: Fri May 02, 2003 7:00 pm
by shylock
I am trying to do a mysql insert/update using a loop for a large set of form-submitted options (33+). My logic (flawed?) was to make 33 fields with names as numbers so that I could simply (hmm) reference the field name using a loop var and churn through them

I guess there are several issues.
First, how do I reference a variable that is a number in the loop, for instance the var would be $1 (with a string value) and the loop var would be $i (which would=1 on the first pass, see code).

Second, if that works, can I use the same concept in the UPDATE statement to ref the field name? First field name is 1, then I say UPDATE $i (or $i++ as I don't have a 0 field). -I've never used a var to ref a field and couldn't find where that was not doable in the docs.

Third, maybe someone will tell me that naming fields with numbers is inane and then passing variables as numbers is equally so?


Code: Select all

<?php require_once('../Connections/cateye2.php');
mysql_select_db($database_cateye2, $cateye2);
?>

<?php
//get the product name from product table using the passed id

$query_rsProdID = "Select products_name from products WHERE products_id = $prod_id";
$rsProdID = mysql_query($query_rsProdID, $cateye2);
$row_rsProdID = mysql_fetch_assoc($rsProdID);
$ProdName = $row_rsProdID&#1111;'products_name'];

//insert the easy part first

$sql = "INSERT INTO specs_com (prod_id, prod_name) VALUES ('$prod_id','$ProdName')";
$spec_id = mysql_insert_id(); //grab the id key for the value insert below

//create arrays for the set of specs 1, 1a, 2, 2a, etc.

for ($i=1; $i< 34;$i++)&#123;

if (isset($.$i)&#123; //form variable should start with $1, $2, $3, etc
$specarray&#1111;]= $.$i; //main spec string
$specarrayV&#1111;]= $.$i.a; // value for the above, if set
&#125;
&#125;

//Loop through the array and update the table for each

for ($i=0;$i< count($specarray);$i++)&#123;
$sqlS="UPDATE specs_com SET $i++ = $specarray&#1111;$i] WHERE id = $spec_id";
$sqlV="UPDATE specs_com SET $i++.a = $specarrayV&#1111;$i] WHERE id = $spec_id";
mysql_query($sqlS, $cateye2);
mysql_query($sqlV, $cateye2);
&#125;

if (isset($acc1))&#123;
$sqlAcc1="UPDATE specs_cam SET acc1 = $acc1 WHERE id = $spec_id";
mysql_query($sqlACC1, $cateye2);
&#125;

if (isset($acc12))&#123;
$sqlAcc2="UPDATE specs_cam SET acc2 = $acc2 WHERE id = $spec_id";
mysql_query($sqlACC2, $cateye2);
&#125;
?>
The above gives me a "expected T_Variable" error

Thanks for any pointers.

Posted: Fri May 02, 2003 7:20 pm
by volka
http://www.php.net/manual/en/language.variables.php
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
But try

Code: Select all

<html>
	<body>
		<pre>
			<?php
				if (isset($_POST['data']))
				{
					print_r($_POST);
					foreach($_POST['data'] as $datfield)
						echo $datfield, "\n";
				}
			?>
		</pre>
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			<?php
				for($i=0; $i!=10; $i++)
					echo '<input type="text" name="data[', $i, ']" />', $i,'<br />';
			?>
			<input type="submit" />
		</form>
	</body>
</html>
or

Code: Select all

<?php
	$fields = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
?><html>
	<body>
		<pre>
			<?php
				if (isset($_POST['data']))
				{
					print_r($_POST);
					foreach($_POST['data'] as $idx=>$datfield)
						echo $idx, '=>', $datfield, "\n";
				}
			?>
		</pre>
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			<?php
				foreach($fields as $f)
					echo '<input type="text" name="data[', $f, ']" />', $f,'<br />';
			?>
			<input type="submit" />
		</form>
	</body>
</html>
the array $fields might be created from the result of
DESCRIBE, show fields, string mysql_field_name ( resource result, int field_index) or might be hardcoded.

Posted: Sun May 04, 2003 12:23 pm
by shylock
Excellent. Not exactly what I was looking for but it sparked a solution anyway. Thanks.

Thanks also for the manual snip, I now remember reading that when i embarked on this quest and promptly filed it away under "marginal knowledge".

-a :oops: