?
Solved

Create PHP Array in IF statement within For loop

Posted on 2010-01-08
4
Medium Priority
?
470 Views
Last Modified: 2012-05-08
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['createformdb'])) {
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
0
Comment
Question by:john-formby
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:ludofulop
ID: 26209065
change your for loop this way:

$array = array();
for($x=1;$x<=$i;$x++) {      
      if($tblfieldkey[$x] == 'index') {
            $array[] = $tblfieldname[$x];            
      }
}
$b = implode(',',$array);
echo $b;
0
 
LVL 14

Author Comment

by:john-formby
ID: 26209109
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
0
 
LVL 15

Accepted Solution

by:
ludofulop earned 2000 total points
ID: 26209134
can you please double check your modifications according to my solution?

in my case, this code:

        $i=3;
        $tblfieldkey = array(1=>'primary', 2=>'index', 3=>'index');
        $tblfieldname = array(1=>'field1', 2=>'field2', 3=>'field3');
       
        $array = array();
        for($x=1;$x<=$i;$x++) {      
            if($tblfieldkey[$x] == 'index') {
                $array[] = $tblfieldname[$x];            
            }
        }
        $b = implode(',',$array);
        echo $b;


ouputs field2,field3

according to output format, change this line:
                $array[] = $tblfieldname[$x];          
with this:
                $array[] = "'".$tblfieldname[$x]."'";
0
 
LVL 14

Author Comment

by:john-formby
ID: 26209859
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question