Jump to content

Incorrect if/else statement?


golden14

Recommended Posts

I'm working on passing a lot of variables from Flash to PHP, and then back to Flash again.  It works correctly when I run this:

 

<?php

for ($i=1; $i<31; $i++) {
  ${'rg1tName'.$i}=$_POST['rg1_'.$i];

  if($i>1){
    echo "&rg1_".$i. "=".${'rg1tName'.$i};
  }

  else { 
    echo "rg1_".$i. "=".${'rg1tName'.$i};
  }

}
?>

 

But when I add more into the if/else statements (which I need to do), it doesn't work. 

 

<?php

for ($i=1; $i<31; $i++) {
  ${'rg1tName'.$i}=$_POST['rg1_'.$i];
  ${'rg2tName'.$i}=$_POST['rg2_'.$i];

  if($i>1){
    echo "&rg1_".$i. "=".${'rg1tName'.$i};
    echo "&rg2_".$i. "=".${'rg2tName'.$i};
  }

  else { 
    echo "rg1_".$i. "=".${'rg1tName'.$i};
    echo "rg2_".$i. "=".${'rg2tName'.$i};
  }

}
?>

 

Using trial and error and commenting out different sections of the code and changing it (both the AS3 and the PHP), it seems like the problem is in the PHP in the echoing sections.  Based on what you see here, is there something noticeably wrong?  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/95331-incorrect-ifelse-statement/
Share on other sites

It's not pulling from a URL, Flash is storing text box strings in variables using the URLVariables method in AS3.  I have 4 groups of variables, each with a different prefix (rg1, rg2, rg3 and rg4).  It works no problem when I try it with just one.  But when I add that second grouping in there, it will POST it without giving an error but the echo sections will give an error.

 

Thanks anyway though!

I'm not that much with flash, but from what I read, Flash need each variable in it's own line

 

<?php

for ($i=1; $i<31; $i++) {
  ${'rg1tName'.$i}=$_POST['rg1_'.$i] . "\n";
  ${'rg2tName'.$i}=$_POST['rg2_'.$i];

  if($i>1){
    echo "&rg1_".$i. "=".${'rg1tName'.$i} . "\n";
    echo "&rg2_".$i. "=".${'rg2tName'.$i};
  }

  else { 
    echo "rg1_".$i. "=".${'rg1tName'.$i} . "\n";
    echo "rg2_".$i. "=".${'rg2tName'.$i};
  }

}
?>

 

just add "\n" at the end of each variable except the last one, so if you have three or four

 

// 3 variables example
    echo "rg1_".$i. "=".${'rg1tName'.$i} . "\n";
    echo "rg2_".$i. "=".${'rg2tName'.$i} . "\n";
    echo "rg3_".$i. "=".${'rg3tName'.$i};

// 4 variables example
    echo "rg1_".$i. "=".${'rg1tName'.$i} . "\n";
    echo "rg2_".$i. "=".${'rg2tName'.$i} . "\n";
    echo "rg3_".$i. "=".${'rg3tName'.$i} . "\n";
    echo "rg4_".$i. "=".${'rg4tName'.$i};

 

and so on

Oh.. I just thought about your code's logic.  It doesn't make sense.  You can do it like this:

 

$sep = '';
for ($i=1; $i<31; $i++) {
  ${'rg1tName'.$i}=$_POST['rg1_'.$i];
  ${'rg2tName'.$i}=$_POST['rg2_'.$i];

  echo $sep . "rg1_".$i. "=".${'rg1tName'.$i};
  $sep = '&';  
  echo $sep . "rg2_".$i. "=".${'rg2tName'.$i};

}

 

$sep will be blank for the first variable only, ensuring that you get a & in front of every subsequent variable.

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.