Jump to content

[SOLVED] variable variable problem. Whats wrong?


Branden Wagner

Recommended Posts

for($i = 1; $i == $school_num; $i++)
        {
                ${school_$i_tmp}        = strip_tags(strip_mq_gpc($_POST['school_${i}']));        
                ${"school_$i}         = preg_replace('/\r?\n/', '<br>', ${school_$i_tmp});
        }

 

whats wrong with this? how can i fix it?

 

thanks

Link to comment
Share on other sites

Sorry, i guess i should have put that in the first post..

 

when the loop is done it should have variables like

$school_1_tmp
$school_1
$school_2_tmp
$school_2
...

based on the variable $school_num they sey earlier on in the form(usually ranges between 1 and 5).

Basically i was trying to create a loop to iterate through the users input, to collect information on the schools they input.

 

i didnt know how to put the 2 lines on into 1 thus why i used school_1_tmp and school_1... so if its a better idea to combine them, that works for me too.

right now i get syntax errors... Unexpected T-variable.

Link to comment
Share on other sites

When you have a varying length array of data, you should use an array to hold it. This will make your code simpler and general purpose so that it will work with any number of values.

 

By using sequentially numbered variables, you need to carry around another variable to keep track of how many of them there are. With an array you don't need to do that, just use the count() function if you should ever need to know how many or even simpler, just iterate over them directly using a foreach() loop.

Link to comment
Share on other sites

The normal way would be if the javascript/form generates the input fields with names - "school[]" (and let the index be generated by the position on the form) or explicitly specify the index in the name - "school[0]", "school[1]",...

 

Both of those methods will result in an array - $_POST['school'][0], $_POST['school'][1]...

 

Assuming that you want the values in $school[0], $school[1]..., (if you want the indexes to be 1, 2, 3,... instead of 0,1,2... add the $key=> construct to the foeach() loop and use $school[$key+1] instead of $school[] in the loop) the equivalent foreach loop for the code you posted would look like -

 

foreach($_POST['school'] as $value)
{
    $school[] = preg_replace('/\r?\n/', '<br>', strip_tags(strip_mq_gpc($value)));        
}

 

 

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.