0

In a foreach() loop I got a static ID that stays the same but I want it to change every time the foreach() loop posts a new line.

I have tried a for() loop and a foreach() loop, and in the example below I am using an array but I get the following error:

Array to string conversion

What I want:

id="1",id="2",id="3",id="4",id="5";

My code:

private function recursiveTemplate($templates){



    foreach($templates as $key => $template){
        $templatenames =array(1,2,3,4,5);
        $int=0;
        if(is_array($template)){

            $this->templateHtml .= '<div class="input-group tempHolder"><span style="width: 155px;" class="input-group-addon" id='.$key.'>'.$key.'</span>';
            $this->recursiveTemplate($template);
            $this->templateHtml .= '
               <button type="button" class="btn2 btn-info"  id="addTemplate"><span class="glyphicon glyphicon-plus">
               <button type="button" class="btn2 btn-danger"  id="deleteTemplate" ><span class="glyphicon glyphicon-minus"></span></span></div>';
        }else{
            $this->templateHtml .= '<input type="text" class="form-control" id="'.$templatenames .'" name="jezus" aria-describedby="basic-addon3" value="'.$template.'">';
        }


    }

}

}

Blubberguy22
  • 1,344
  • 1
  • 17
  • 29
  • First of all, an id cannot begin with a number. [see this question](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html). Second: `id="'.$templatenames .'"` is assigning the entire array as id. Do something like `id="id_'.$key .'"`, as the `$key`'s are always different. Or use a simple counter. – Michel Sep 05 '16 at 12:44
  • Yes that worked. thank alot – MR Grow A Lot Sep 05 '16 at 12:50

1 Answers1

1

You have to send additional parameter to your function. i made a full working example below:

<?php

class MyClass
{

    // public $templateHtml;
    // ...

    public function recursiveTemplate($templates, $counter = 0)
    {

        foreach ($templates as $key => $template) {
            if (is_array($template)) {

                $this->templateHtml .= '<div class="input-group tempHolder"><span style="width: 155px;" class="input-group-addon" id=' . $key . '>' . $key . '</span>';
                $this->recursiveTemplate($template, $counter++); // <-- Note the counter
                $this->templateHtml .= '
                   <button type="button" class="btn2 btn-info"  id="addTemplate"><span class="glyphicon glyphicon-plus">
                   <button type="button" class="btn2 btn-danger"  id="deleteTemplate" ><span class="glyphicon glyphicon-minus"></span></span></div>';
            } else {
                $this->templateHtml .= '<input type="text" class="form-control" id="' . ($counter += 1) . '" name="jezus" aria-describedby="basic-addon3" value="' . $template . '">';
            }

        }

    }

    // ...

    public function render()
    {
        $this->recursiveTemplate(['a', 'b', ['d', 'e']]);
        echo $this->templateHtml;
    }

}

$Obj = new MyClass();
$Obj->render();
MoeinPorkamel
  • 701
  • 5
  • 8