Jump to content

foreach and array of arrays


AdRock
Go to solution Solved by AdRock,

Recommended Posts

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" />
Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

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' />";
}
?>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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' />";
	}
}

}
?>
Link to comment
Share on other sites

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);
}
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.