[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

eval() on indexed value

Posted on 2002-06-10
7
Medium Priority
?
197 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

656 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