Link to home
Start Free TrialLog in
Avatar of sargento
sargento

asked on

eval() on indexed value

I have a form that submits multiple hidden fields. Part of this form submits a field called  $orderline1 (40 total -  $orderline1 - $orderline401). I'm trying to determine how many fields have data but I keep getting errors with my eval!

I have tried:

$cnt = 1;
$orders = 1;
     while ($orders){
          eval("if($OrderLine"+$cnt+" != '')"){
          echo $cnt;
          }else{ $orders = 0;}
          $cnt++;
     }

//////////////////////////////////////////////////

I have also tried the eval line as:

if(eval("$OrderLine"+$cnt) != ""){

But I can't get the eval to work! Is this the best way to figure out how many fields have data and if so what is the eval statement. If not then what is the best way?
Avatar of Batalf
Batalf
Flag of United States of America image

Why use eval?

Couldn't use variablevariable ?

Instead of eval("if($OrderLine"+$cnt+" != '')"){

try

$tmpVar = $OrderLine.$cnt;
if($$tmpVar!=""){

And also: You don't use + in PHP. Use "."
Avatar of sargento
sargento

ASKER

$tmpVar = $OrderLine.$cnt; produces a number which is only the count (value of $cnt). I need to know somehow if
$Orderline1 holds any data and if $Orderline2 holds any data etc.

I'm just trying to index thru $Orderline# (somehow) and find out how many fields hold data.
That's why I'm using double $$ (also called variablevariable)

This is wrong

if($tmpVar!="")

This is RIGHT:

if($$tmpVar!="")
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to clarify a little further this is psrt of the form structure:

<input type="HIDDEN" name="OrderLine1"        value="">
                                                                      <input type="HIDDEN" name="OrderLine2"        value="">
                                                                      <input type="HIDDEN" name="OrderLine3"        value="">

which goes upto 40!
If this is the case, my last comment will solve your problem

$tmpVar = "OrderLine".$cnt; // $tmpVar = "OrderLine1" if $cnt is 1
if($$tmpVar!=""){ // The same as if($OrderLine1!=""){
Thank you!

An other solution is to use an array in your form. Instead of OrderLine1 you could use OrderLine[1]. Then you have an array called $OrderLine when you submit the form. You could easily loop through this form.

THe form:

<input type="HIDDEN" name="OrderLine[1]"        value="">
                                                                     <input type="HIDDEN" name="OrderLine[2]"
       value="">
                                                                     <input type="HIDDEN" name="OrderLine[3]"
       value="">

--
PHP code after submission:

for($no=0;$no<count($OrderLine);$no++){
  if($OrderLine[$no]!=""){
  // Other Code
  }
}