john-formby
asked on
Create PHP Array in IF statement within For loop
Hi,
I am trying to create a little form that will automatically generate a mysql table. All has been going well, but now I am a bit stuck with creating an array. I am using a For loop to echo out the number of form fields.
I have a sample of the form code for displaying the "key" fields:
<td><input type="radio" name="tblfieldkey['.$x.']" value="primary" /></td>
<td><input type="radio" name="tblfieldkey['.$x.']" value="index" /></td>
<td><input type="radio" name="tblfieldkey['.$x.']" value="unique" /></td>
<td><input type="radio" name="tblfieldkey['.$x.']" value="" /></td>
When I post the form, I am using a for loop to loop through some validation by entry. The problem is that I want to create a small array for any fields with the value "index". I am using an IF statement for this and trying to output the value as the fieldname (another field on my form). My code is as follows:
if(isset($_POST['createfor mdb'])) {
foreach($_POST as $key=>$value) {
$$key = $value;
}
$i = $_POST['numoffields'];
for($x=1;$x<=$i;$x++) {
if($tblfieldkey[$x] == 'index') {
$array = array();
$array[] = $tblfieldname[$x];
$b = implode(',',$array);
}
echo $b;
This is currently outputting 2 arrays:
print_r($array) = Array ( [0] => field1 ) Array ( [0] => fiel2 )
$b = field1 field2
The problem I am having is that I need to be able output as follows:
$b = 'field1','field2'
Is this possible? I have tried loads of things but cannot get it working.
Many Thanks,
John
I am trying to create a little form that will automatically generate a mysql table. All has been going well, but now I am a bit stuck with creating an array. I am using a For loop to echo out the number of form fields.
I have a sample of the form code for displaying the "key" fields:
<td><input type="radio" name="tblfieldkey['.$x.']"
<td><input type="radio" name="tblfieldkey['.$x.']"
<td><input type="radio" name="tblfieldkey['.$x.']"
<td><input type="radio" name="tblfieldkey['.$x.']"
When I post the form, I am using a for loop to loop through some validation by entry. The problem is that I want to create a small array for any fields with the value "index". I am using an IF statement for this and trying to output the value as the fieldname (another field on my form). My code is as follows:
if(isset($_POST['createfor
foreach($_POST as $key=>$value) {
$$key = $value;
}
$i = $_POST['numoffields'];
for($x=1;$x<=$i;$x++) {
if($tblfieldkey[$x] == 'index') {
$array = array();
$array[] = $tblfieldname[$x];
$b = implode(',',$array);
}
echo $b;
This is currently outputting 2 arrays:
print_r($array) = Array ( [0] => field1 ) Array ( [0] => fiel2 )
$b = field1 field2
The problem I am having is that I need to be able output as follows:
$b = 'field1','field2'
Is this possible? I have tried loads of things but cannot get it working.
Many Thanks,
John
ASKER
Hi,
Thanks for the quick reply. I tried that but it gives the same output as before (Field1 Field2). Also, would it be possible to format the output as:
'Field1', 'Field2' not Field1, Field2
Many Thanks,
John
Thanks for the quick reply. I tried that but it gives the same output as before (Field1 Field2). Also, would it be possible to format the output as:
'Field1', 'Field2' not Field1, Field2
Many Thanks,
John
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks so much for your help. It is working perfectly now. I must have had a typo or something in the code.
Many Thanks,
John
Thanks so much for your help. It is working perfectly now. I must have had a typo or something in the code.
Many Thanks,
John
$array = array();
for($x=1;$x<=$i;$x++) {
if($tblfieldkey[$x] == 'index') {
$array[] = $tblfieldname[$x];
}
}
$b = implode(',',$array);
echo $b;