?
Solved

eval() on indexed value

Posted on 2002-06-10
7
Medium Priority
?
192 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
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

 
LVL 32

Accepted Solution

by:
Batalf earned 200 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 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