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
https://forums.phpfreaks.com/topic/93722-function-help/
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
https://forums.phpfreaks.com/topic/93722-function-help/#findComment-480270
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
https://forums.phpfreaks.com/topic/93722-function-help/#findComment-480294
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.