Jump to content

[SOLVED] Arrays (3D array? or just value change?)


colombian

Recommended Posts

For simplicity sake, here is what I have:

 

<?PHP
$forms = array ( 'fname' => 'text', 'lname' => 'text')
foreach ($forms as $field => $type) {
  # Dynamic code that creates the form like:
           $output.= '<input type="text" name="'.$field.'" id="'.$field.'"';
            if (isset($_REQUEST[$field])) {
                $output.= ' value="'.htmlspecialchars($_REQUEST[$field]).'"';
            }
            $output.= ' />';

 

The main problem, is obviously, that "Lname" becomes the html that the user sees. Some of the fields (like email for example) work fine without changing that, but I wanted to somehow create either a multi-dimensional array to store the values I want the user to see, or something else to get different names to display.

 

I haven't worked with complex arrays, and the examples of multi dimensional arrays I saw were severly lacking, or way too simplistic. This seems like it should be easy, but my attempt thus far have failed.

 

I am using a switch to go between 'text' fields and 'textarea', radio buttons, etc. So I would rather not manually type the 'type'.

 

Ideally something like

'fname', 'First name' => 'text', (but this doesn't work - no proper PHP).

OR

'fname' => 'First name' => 'text'

 

and formulate a foreach statement (or while) that gets the appropriate values into the dynamic html code.

 

I tried with 2 arrays, one with types, and one with the different names, but it ended up with multiple outputs of the same form.

 

Any help would be greatly appreciated.

 

Thank you

Link to comment
Share on other sites

do you mean

<?php 
$forms = array (

     array ('caption' => 'First name',
            'field'   => 'fname',
            'type'   => 'text'
            ),
     array ('caption' => 'Last name',
            'field'   => 'lname',
            'type'   =>  'text'
            ),
     array ('caption' => 'Email',
            'field'   => 'email',
            'type'   =>  'text'
            )
);

$output = '';
foreach ($forms as $fields)
{
    extract ($fields);
    $output .= "$caption <input type='$type' name='$field' ";
    if (isset($_REQUEST[$field])) {
        $output.= ' value="'.htmlspecialchars($_REQUEST[$field]).'"';
    }
    $output.= " /><br/>\n"; 
}

echo $output;
?>

Link to comment
Share on other sites

Thank you for the prompt reply.

I am having difficulty translating the multi dimensional array into a foreach loop.

 

A normal array goes:

<?php
foreach ($forms as $field => $type) {
# do stuff
} ?>

 

I attempted several different versions trying to capture the multi dimensional array without luck. How would that work?

 

foreach ($forms as $field => $type => $name) { #this lines errors out

 

Do I need to do something more like:

Array changed to:

$forms = array( array( 'fname' => "First name",

                     'type' => "text")

                      array( 'lname' => "Last name", 'type' => 'text')

);

 

for ($row = 0; $row < 3; $row++)
{
   $forms[$row]["fname"]
   $forms[$row]["First name"]
   $forms[$row]["text"];
}

 

But I'd like to know how to make the equivalent of the above foreach loop in the array you suggested.

 

Again, thanks a lot for your help.

 

 

Link to comment
Share on other sites

You can nest your foreach loops:

 

<?php
$forms = array(array( 'fname' => "First name",'type' => "text"),array( 'lname' => "Last name", 'type' => 'text')
);
foreach($forms as $k => $v){
foreach($v as $k2 =>$v2){
	echo $k2.' : '.$v2.'<br />';
}
}
?>

 

Remember, the whole point of multidimensional arrays is that each element within the array is an array in itself. Therefore, you work with each value of the array as you would any other single dimensional array. Hmm, i wonder if i could have put 'array' into those two sentences anymore often that i did.

Link to comment
Share on other sites

Thank you very much Barand.

For further PHP knowledge, what does the "extract ($fields);" command do for this example.

 

I was leaning towards a similar solution in terms of the array, I tried your code for the foreach loop and it worked like a charm.

 

Much appreciated!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.