Note that there are some explanatory texts on larger screens.

plurals
  1. POPass array values into foreach and finally another variable?
    primarykey
    data
    text
    <p>In PHP, what's the best practice for dealing with a loop in a variable? Let me explain.</p> <p>I have a file called index.php which contains the following:</p> <pre><code>&lt;?php $SELECT_INDUSTRY = array("Industry1", "Industry2", "Industry3"); require_once('form.inc'); ?&gt; &lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;?php echo $FORM_FIELD_INDUSTRY ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I then have a second file called form.inc:</p> <pre><code>&lt;?php $FORM_FIELD_INDUSTRY = '&lt;select&gt;&lt;option value=&gt;Select an Industry&lt;/option&gt;'.$INDUSTRY.'&lt;/select&gt;'; ?&gt; </code></pre> <p>Here's what is needed. I'd like to pass over the array values that are entered, into a foreach to process the array and spit out the <code>&lt;option&gt;&lt;/option&gt;</code> code. Here's an example:</p> <pre><code>foreach ($SELECT_INDUSTRY as $INDUSTRY) { echo '&lt;option value="$INDUSTRY"&gt;$INDUSTRY&lt;/option&gt;'; } </code></pre> <p>So this function would build my select options based on the array values. This works, but how would I be able to then pass the total values into a variable that I've defined in the form.inc file above? In the form.inc file, I have $INDUSTRY as the variable, which I suppose needs some sort of return from the other function. This may also need global variables to pass between functions if it comes to it. Is there a better way of handling this?</p> <p>My end goal is to have the variable $FORM_FIELD_INDUSTRY which spits out the foreach function based on the array values. </p> <p><strong>SOLUTION</strong></p> <p>The solution is listed below, but for anyone coming across this, here is what I did:</p> <p><strong>index.php</strong></p> <pre><code>&lt;?php $SELECT_INDUSTRY = array("Industry1", "Industry2", "Industry3"); $FORM_SELECT_SIZE = ''; require_once('form.inc'); ?&gt; &lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;?php get_options( $FORM_FIELD_INDUSTRY ) ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>form.inc</strong></p> <pre><code>&lt;?php function get_options( $arr = array() ) { global $FORM_SELECT_SIZE; echo '&lt;select id="industry" class="'.$FORM_SELECT_SIZE.'"&gt;&lt;option value=&gt;Select an Industry&lt;/option&gt;'; foreach( $arr as $option ) { echo '&lt;option&gt;'.$option.'&lt;/option&gt;'; } echo '&lt;/select&gt;&lt;/div&gt;&lt;/div&gt;'; } $FORM_FIELD_INDUSTRY = $SELECT_INDUSTRY; ?&gt; </code></pre> <p>Pretty simple solution for passing values between the function, and variables.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload