I am trying to take posted variables (lots) and loop through to put them in a sequence of mysql tables.
I am having trouble with how to reference the names easily.
Since they are named sequencially (sp), such as $name1, $name2, etc., I thought it might be easy to do
for ($i = 1;$i !=$max;$i++){
echo "$name.$i";
}
but, I obviously don't understand how to reference variable names using concatenation. Since there is no variable $name (without its number), then php doesn't understand.
Can I do this? I've tried various ways of putting things together ("$"."name".$i") and ("$name".$i) etc. but no luck.
Or, after reading about $_POST , I wonder if I can reference the posted vars by number? if posts are held in an array and I know the sequence, can I just loop through with $_POST[$i] ?
thanks once again for the pointers.
=a
Referencing posted variables in loop
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Maybe:
If you would like to learn how to use variable variables:
http://www.php.net/manual/en/language.v ... riable.php
Generally though putting things in arrays makes it all much simpler.
Mac
Code: Select all
for ($i = 1;$i !=$max;$i++){
echo $_POST['name'.$i];
}http://www.php.net/manual/en/language.v ... riable.php
Generally though putting things in arrays makes it all much simpler.
Mac
Wow, that worked! sort of.
I am able to echo the POST var using $_POST['name'.$i] so I can loop it.
but, I can't get it to work in my sql statement if the value is a string. It's probably me not understanding proper concat syntax again.
here's my code:
again, the insert works with numbers, not strings. hmm
I am able to echo the POST var using $_POST['name'.$i] so I can loop it.
but, I can't get it to work in my sql statement if the value is a string. It's probably me not understanding proper concat syntax again.
here's my code:
Code: Select all
<?php
for ($i=1;$i< 34;$i++){
if ($_POST['spec'.$i] !=""){
$sqlS="UPDATE specs SET spec".$i." = ".$_POST['spec'.$i]." WHERE id = $spec_id";
mysql_query($sqlS, $cateye2);
} else {
break;
}
if ($_POST['spec'.$i.'a'] !=""){
$sqlV="UPDATE specs SET spec".$i."a = ".$_POST['spec'.$i.'a']." WHERE id = $spec_id";
mysql_query($sqlV, $cateye2);
} else {
$sqlV="UPDATE specs SET spec".$i."a = 1 WHERE id = $spec_id";
mysql_query($sqlV, $cateye2);
}
}
?>string literals have to be quoted for mysql.
http://www.mysql.com/doc/en/String_syntax.html
Code: Select all
INSERT INTO tablename (aNumericalField, aStringField) VALUES (1, 'a'), (2, 'b'), (3, 'c')Once again, I figured out my issue after posting.
since I agree that using arrays is much easier, (yet this application couldn't use them for various reasons), I was able to make one after the syntax for $_POST was explained clearer.
Thanks again to all who helped, this list is awesome.
the working code:
since I agree that using arrays is much easier, (yet this application couldn't use them for various reasons), I was able to make one after the syntax for $_POST was explained clearer.
Thanks again to all who helped, this list is awesome.
the working code:
Code: Select all
<?php
//make an array using each posted var
for ($i=0;$i < 33;$i++){
$specArr[] = $_POST['spec'.$i];
}
for ($i=0;$i < 33;$i++){
$specArrV[] = $_POST['spec'.$i.'a'];
}
//Loop through the array and update the table for each
for ($i=1;$i< 34;$i++){
if ($specArr[$i] !=""){
$sqlS="UPDATE specs SET spec".$i." = '$specArr[$i]' WHERE id = $spec_id";
mysql_query($sqlS, $cateye2);
} else {
break;
}
if ($specArrV[$i] !=""){
$sqlV="UPDATE specs SET spec".$i."a = '$specArrV[$i]' WHERE id = $spec_id";
mysql_query($sqlV, $cateye2);
} else {
$sqlV="UPDATE specs SET spec".$i."a = 1 WHERE id = $spec_id";
mysql_query($sqlV, $cateye2);
}
}
?>