Solved

eval() on indexed value

Posted on 2002-06-10
7
173 Views
Last Modified: 2011-10-03
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?
0
Comment
Question by:sargento
  • 5
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 7068100
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 "."
0
 

Author Comment

by:sargento
ID: 7068181
$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.
0
 
LVL 32

Expert Comment

by:Batalf
ID: 7068183
That's why I'm using double $$ (also called variablevariable)

This is wrong

if($tmpVar!="")

This is RIGHT:

if($$tmpVar!="")
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Accepted Solution

by:
Batalf earned 50 total points
ID: 7068187
Or maybe this is right

$tmpVar = "OrderLine".$cnt;

if($$tmpVar!=""){

If you do this

$tmpVar will be "OrderLine1" if $cnt is 1. Then if($$tmpVar.. is the same as if($OrderLine1)
0
 

Author Comment

by:sargento
ID: 7068188
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!
0
 
LVL 32

Expert Comment

by:Batalf
ID: 7068191
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!=""){
0
 
LVL 32

Expert Comment

by:Batalf
ID: 7068200
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
  }
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now