Jump to content

function help


tutorialstuff

Recommended Posts

I've written a function that creates uses an array over and over so if I want to use that same array in different context such as a list or for a drop down menu all I have to do is call the function and declare an open tag and closing tag, but sometimes I might want to use a value with that function that prints out the array name but I'm having a hard time figuring out how. Maybe somebody can help. You can see a live demo at: http://tutorialstuff.com/test/arrays.php and the code is below.

 

Thanks - Mike

 

<?php
function rooms($open, $close){
$rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage');
$i = 0;
while($rooms[$i]){
print "$open$rooms[$i]$close";
$i++;
}
}
print rooms("<b>", "</b><br />\n");
?>
<form>
<select>
<?php
print rooms("<option value=\"$rooms[$i]\">", "</option>\n");
?>
</select>
</form>

 

Link to comment
Share on other sites

For your first example you  are probably better off just using an implode.

 

<?php

$rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage');

 

echo "<b>", implode("</b><br /><b>\n", $rooms), "</b><br />\n";

// or you could use print

print "<b>" . implode("</b><br /><b>\n", $rooms) . "</b><br />\n";

?>

 

If I am understanding the second one you are really just using the value twice.  First off increase the reuse of it pass in the array.  Then pass the wrapping text in as array pieces.  This will take an arbitrary number of values and insert it between each element of the wrapping pieces.  (This will work for the <b> example as well)

 

<?php

 

$rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage');

 

function wrapem($what, $enclosure) {

  foreach ($what as $val) {

    $final .= implode($val, $enclosure);

  }

  return $final;

}

 

?>

 

<form>

<select>

<?php

print wrapem($rooms, array('<option value="', '">', "</option>\n"));

?>

</select>

</form>

 

Link to comment
Share on other sites

Lets try that again.....

 

 

For your first example you  are probably better off just using an implode.

 

<?php
$rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage');

echo "<b>", implode("</b><b>\n",  $rooms), "</b>\n";
// or you could use print
print "<b>" . implode("</b><b>\n",  $rooms) . "</b>\n";
?>

 

If I am understanding the second one you are really just using the value twice.  First off increase the reuse of it pass in the array.  Then pass the wrapping text in as array pieces.  This will take an arbitrary number of values and insert it between each element of the wrapping pieces.  (This will work for the example as well)

 

 

<?php

$rooms = array('Bedroom','Living/Family Room','Dinning Room','Kitchen','Utility Room','Garage');

function wrapem($what, $enclosure) {
  foreach ($what as $val) {
    $final .= implode($val, $enclosure);
  }
  return $final;
}

?>

<form>
<select>
<?php
print wrapem($rooms, array('<option value="', '">', "</option>"));
?>
</select>
</form>

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.