سؤال

I've done hours of research on this now, so I believe I'm not repeating, even though this seems like it would have a simple solution.

I have an HTML form sending values to PHP, where it takes the value, and sets them all to a variable. When I submit the values and load the PHP, it completely breaks as if there is a syntax problem, but I can't find anything obvious. I'm setting the variables from POST like so:

$ital1=$_POST['checkbox1'];
$bold1=$_POST['checkbox2']; 
$ital2=$_POST['checkbox3'];
$bold2=$_POST['checkbox4'];

I'm then using these variables with simplexml to write to an xml document for databasing. Here's a snippet of that:

$xml = simplexml_load_file('settings.xml');

function loops()
{
$italvar = (eval('return $' . "ital{$string}"));
$boldvar = (eval('return $' . "bold{$string}"));
$alertvar = (eval('return $' . "alerttitle{$string}"));
$pagevar = (eval('return $' . "page{$string}n"));
$colorvar = (eval('return $' . "colorset{$string}"));
$string = strval($i);


for($i=1; $i<21; $i++)
{
    if($xml->settings->checkbox->"ital{$string}" == $empty && $italvar == "on")//ITAL1
    {
        $xml->settings->checkbox->"ital{$string}" = $italvar;
    }
    else if($italvar == $empty && $xml->settings->checkbox->"ital{$string}" == "on")
    {
        $xml->settings->checkbox->"ital{$string}" = $empty;
    }
    else
    {
    continue;
    }
}
$xml->asXML('settings.xml');

This is just a piece of the function, but the rest is basically this repeated for different types of variables. I know the issue is within the loops function as when I delete loops(), the PHP at least loads some echos at the top of the doc.

I'm not sure what in here is breaking. I'm guessing it's the syntax with how I'm combining and iterating variables, which explains the title.

So my questions is this. How do I combine strings and variables correctly?

$italvar = (eval('return $' . "ital{$string}"));
$string = strval($i);

In this instance I want the end result to be $ital#, # being the current iteration of the forloop, so I can correctly write to xml the value of the set variable from $_POST as shown at the top.

"ital{$string}"
$string = strval($i);

And here I just want it to be a string such as ital1, then ital2, as that's what the nodes are called in the xml. I'm sure what I'm doing wrong is very obvious, but I'm very new to PHP and I can't seem to figure it out. Thank you very much in advance for any input!

هل كانت مفيدة؟

المحلول

You have a $_POST array. Use it.

for( $i=1; $i<=20; $i++) {
    $ital = $_POST['checkbox'.($i*2-1)];
    $bold = $_POST['checkbox'.($i*2)];

    // now do stuff with `$bold` and `$italic`
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top