Solved

eval() on indexed value

Posted on 2002-06-10
7
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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