AdRock Posted October 23, 2013 Share Posted October 23, 2013 Hopefully as easy one. I have this array of values I want to put in a form input $buttons = array(); $buttons['back'] = array('class' => 'back', 'name' => 'backbutton', 'id' => 'back-button', 'title' => 'Go Back'); $buttons['save'] = array('class' => 'save', 'name' => 'savebutton', 'id' => 'save-button', 'title' => 'Save'); $buttons['add'] = array('class' => 'add', 'name' => 'addbutton', 'id' => 'add-button', 'title' => 'Add'); $buttons['edit'] = array('class' => 'edit', 'name' => 'editbutton', 'id' => 'edit-button', 'title' => 'Edit'); $buttons['delete'] = array('class' => 'delete', 'name' => 'deletebutton', 'id' => 'delete-button', 'title' => 'Delete'); $buttons['archive'] = array('class' => 'archive', 'name' => 'archivebutton', 'id' => 'archive-button', 'title' => 'Archive'); What I want to to do is create an array like this if possible $buttons = array($buttons['back'], $buttons['save'], $buttons['add'], $buttons['edit'], $buttons['delete'], $buttons['archive']); and use a foreach loop like this foreach($buttons as $button) { echo '<input type="button" '.$button.'/>'; } so it outputs a load of buttons depending what was in the buttons array and using the elements in each array to give the buttons values I should end up with is <input type="button" class="back" name="backbutton" id="back-button" title="Go Back" /> <input type="button" class="save" name="savebutton" id="save-button" title="Save" /> <input type="button" class="add" name="addbutton" id="add-button" title="Add" /> <input type="button" class="edit" name="editbutton" id="edit-button" title="Edit" /> <input type="button" class="delete" name="deletebutton" id="delete-button" title="Delete" /> <input type="button" class="archive" name="archivebutton" id="archive-button" title="Archive" /> Quote Link to comment Share on other sites More sharing options...
kicken Posted October 23, 2013 Share Posted October 23, 2013 (edited) Use a nested foreach loop to go through and build your attributes string, then output the input with the class and other attributes. foreach ($buttons as $class=>$attrs){ $attrString = ''; foreach ($attrs as $name=>$value){ //...build $attrString } //output the tag } Note your $buttons = array($buttons['back'], $buttons['save'], ...) bit is unnecessary. Your initial buttons array is already sufficient for outputting the tags using a loop. Edited October 23, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
AdRock Posted October 23, 2013 Author Share Posted October 23, 2013 (edited) Thanks Kicken The reason I put it in an array is becuase different pages may have different buttons. Some page only need 2 buttons and not all of them. Will your code allow for that? Edited October 23, 2013 by AdRock Quote Link to comment Share on other sites More sharing options...
buzzycoder Posted October 23, 2013 Share Posted October 23, 2013 This code might get you at right direction: <?php $buttons[] = array('class' => 'back', 'name' => 'backbutton', 'id' => 'back-button', 'title' => 'Go Back'); $buttons[] = array('class' => 'save', 'name' => 'savebutton', 'id' => 'save-button', 'title' => 'Save'); $buttons[] = array('class' => 'add', 'name' => 'addbutton', 'id' => 'add-button', 'title' => 'Add'); $buttons[] = array('class' => 'edit', 'name' => 'editbutton', 'id' => 'edit-button', 'title' => 'Edit'); $buttons[] = array('class' => 'delete', 'name' => 'deletebutton', 'id' => 'delete-button', 'title' => 'Delete'); $buttons[] = array('class' => 'archive', 'name' => 'archivebutton', 'id' => 'archive-button', 'title' => 'Archive'); foreach ($buttons as $button) { extract($button); echo "<input type='button' class='$class' name='$name' id='$id' title='$title' />"; } ?> Quote Link to comment Share on other sites More sharing options...
AdRock Posted October 23, 2013 Author Share Posted October 23, 2013 I was trying to simplify my problem and your solutions look sound but i can't get it to work for my case so I'll explain a bit more. I've built my own MVC and in my config file I include on every page I have my array of buttons I wanted to have them as constants but seeing as it's a pain, i thought if i add it here it should be made public $buttons = array(); $buttons['back'] = array('class' => 'back', 'name' => 'backbutton', 'id' => 'back-button', 'title' => 'Go Back'); $buttons['save'] = array('class' => 'save', 'name' => 'savebutton', 'id' => 'save-button', 'title' => 'Save'); $buttons['add'] = array('class' => 'add', 'name' => 'addbutton', 'id' => 'add-button', 'title' => 'Add'); $buttons['edit'] = array('class' => 'edit', 'name' => 'editbutton', 'id' => 'edit-button', 'title' => 'Edit'); $buttons['delete'] = array('class' => 'delete', 'name' => 'deletebutton', 'id' => 'delete-button', 'title' => 'Delete'); $buttons['archive'] = array('class' => 'archive', 'name' => 'archivebutton', 'id' => 'archive-button', 'title' => 'Archive'); In my controller I add the array of buttons I want to appear in my form in my view file $this->view->buttons = array('back', 'save'); Then in my view file, I would have my foreach where in this case I want to include buttons 'back' and 'save' foreach ($this->buttons as $button) { echo '<input type="button" class="$button['class']" name="$button['name']" id="$button['id']" title="$button['title']" />'; } I think the problem I've got is looping through the foreach loop and including the right button from the config file. Would it be okay to define the array in a constant (i've read you need to serialize it first), then when i need to include it, unserialize it. I just need a way to make more code more efficient so I can use one view and add as many buttons as I need Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 (edited) Ignore Edited October 23, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
buzzycoder Posted October 23, 2013 Share Posted October 23, 2013 Ok,this might help you to achieve your goal: <?php $buttons[] = array('class' => 'back', 'name' => 'backbutton', 'id' => 'back-button', 'title' => 'Go Back'); $buttons[] = array('class' => 'save', 'name' => 'savebutton', 'id' => 'save-button', 'title' => 'Save'); $buttons[] = array('class' => 'add', 'name' => 'addbutton', 'id' => 'add-button', 'title' => 'Add'); $buttons[] = array('class' => 'edit', 'name' => 'editbutton', 'id' => 'edit-button', 'title' => 'Edit'); $buttons[] = array('class' => 'delete', 'name' => 'deletebutton', 'id' => 'delete-button', 'title' => 'Delete'); $buttons[] = array('class' => 'archive', 'name' => 'archivebutton', 'id' => 'archive-button', 'title' => 'Archive'); $recs = array('back','save','delete'); //Array of buttons needed foreach ($recs as $rec){ foreach ($buttons as $button) { extract($button); if ($class == $rec) { echo "<input type='button' class='$class' name='$name' id='$id' title='$title' />"; } } } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 Because your trying to reference $buttons from within your class you need to make the $buttons array accessible from your controller and/or view class. You're better of making a helper function to render your buttons in your view function Button($name) { global $buttons; // access the global $buttons array if(isset($buttons[$name])) { foreach($buttons[$name] as $attr_name => $attr_value) $attrs[] = sprintf('%s="%s"', $attr_name, $attr_value); return '<input type="submit" '.implode(' ', $attrs).'>'; } return; } Then in your view to render the buttons for the view you'd use foreach ($this->buttons as $button) { echo Button($button); } Quote Link to comment Share on other sites More sharing options...
Solution AdRock Posted October 23, 2013 Author Solution Share Posted October 23, 2013 Thanks all....got it working now with your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.