1

I want to make an array of various inputs. For example that my key is submit button, and I want that my array key would be a content of what my submit button needs, also my button is connected to text field.

My minimal code:

<?php

function element($submit){

    if ($submit == "submit"){ 
        $element = "<input type = $submit value = $submit /><INPUT type = $submit name = $submit value = $submit size=40 />";

    }
    $content = $element ;

    return $content;


} // function element


function main($submit){

//print_r ($defaults);

    foreach ($submit as $k=>$submit){
        @$content .=element ($submit[$k]) . "<br />\n";

    }

    return "<form action = {$_SERVER['PHP_SELF']} method = POST>\n$content</form>";

}



$arr = array(
    "submit" => array("submit", "OK","text", "question", ""),
);

$content .= main ($arr["submit"]);

print $content;

So the problem is I don't know how to put my key array values into HTML. Maybe I am doing it wrong? And is it bad practice to do like this?

2 Answers 2

1

This is outputting the inputs I believe like you want. There are comments in the code for what I changed and some thoughts.

<?php

//Change param to $name of the index and $data that you will usefor readability
function element($name, $data){
    //Checking that we are on the right element
    $element = "";
    if ($name == "submit"){ 
        //It is confusing that you have 2 inputs here.
        //Rather than hard coding 0, 1, 2, 3, 4 there could be a name
        //in an associative array. This is not very flexible
        //This is string interpolation I like it more hen concatenation for
        //Simple put this variable here
        $element = "<input type='{$data[0]}' value='{$data[1]}' /><input type='{$data[2]}' name='{$data[3]}' value='{$data[4]}' size=40 />";
    }

    //No need to do $content = $element then just return
    //Not sure what should happen if the name does not exist...
    //Right now it is an empty string
    return $element;
}


function main($inputs){

    //print_r ($defaults);

    //Create this before just using it removes a warning.
    $content = '';

    //Change to $v just for clarity not sure what it will do to actual $submit.
    //Loops the whole array not just that instances of submit.
    foreach ($inputs as $k=>$v){
        $content .= element ($k, $v) . "<br />\n";
    }

    return "<form action = {$_SERVER['PHP_SELF']} method = POST>\n$content</form>";

}

//Changed to inputs assuming this would be more then one
$inputs = array(
    "submit" => array("submit", "OK","text", "question", ""),
);

//Lets call this form you are overusing the content and it is hard to follow
$form = main($inputs);

print $form;
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, it works like I wanted. One question: How in function elements, you give two variables name and data, they are not related with array inputs. Or this is foreach magic? I mean how they get value of inputs array
It does not have to do with foreach you are calling a function that has parameters $name and $data. Not matter what you pass in (in this case $k and $v) for the scope of that function they called by the paramater names.
1

You can embed variable values into a string by using the . (dot) operator in PHP (this is generally called "string concatenation"). For your code I would do it like this:

$element = "<input type='" . $btnname . "' value='" . $btnvalue . "'/><input type='" . $fieldtype . "' name=" . $fieldname . " value='" . $fieldvalue . "' size='40' />";

Or using an array (which must be defined first):

$element = "<input type='" . $sub[0] . "' value='" . $sub[1] . "' /><input type='" . $sub[2] . "' name=" . $sub[3] . " value='" . $sub[4] . "' size='40' />";

If you have an (multi-dimensional) array (with keys which are also arrays), say

$arr = array("subkey" => array($firstVar, $secondVar, [...]));

then you have to use the string concatenation like this:

$element = "<input type='" . $arr["subkey"][0] . "' value='" . $arr["subkey"][1] . "' /><input type='" . $arr["subkey"][2] . "' name=" . $arr["subkey"][3] . " value='" . $arr["subkey"][4] . "' size='40' />";

This will also work for more array subkeys (more dimensions).

You could also use string indices in your array, like the ones of $_SERVER, $_GET or $_POST by replacing the integer index by a string.

But you do not have to have a submit button connected to a text field, it can exist alone.

6 Comments

Is my foreach loop correct, because I am getting errors in that line?
It is called string concatenation.
Right, @nerdlyist, but I tried to keep it as simple as possible.
Tell people what they are doing so they know the words to look for when they search on their own.
@AlexanderLeithner can I use this as sub[0], if sub is array of array's key?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.