-5

I suspect it's not allowable

I am trying to filtering the data according to the user selection..

Here is the variable having the code changes dynamically according to the users input.

Sometimes the variable $p having the

$p="$line[10]=='MICRO'";

or

$p="$line[10]=='MICRO' || $line[10]=='NO'";

or

$p="$line[10]=='MBTS' || $line[10]=='MICRO' || $line[10]=='NO' || $line[10]=='ODM' && $line[9]=='HUBSITE' || $line[10]=='NO'";

Only the thing is the $p variable data is dynamically varies according to the users selections.

and the number inside the $line array is column number of the csv file.

what it does is displaying the list of the values in variable and these values has to print in the if condition

Here I am using the CSV file and in the csv file I am filtering the particualr columns using the above variable.

<?php 
$circle_sheet="DETAILS.csv";
$f = fopen($circle_sheet, "r");
while (($line = fgetcsv($f)) !== false)
{
    $p=eval($p);
    if(${$p})
    {
        echo "<tr>";
        foreach($line as $cell)
        {   
            echo "<td style='color:white;'>" . htmlspecialchars($cell) . "</td>";
        }echo "</tr>\n";
    }
}
?>

In the above while loop the if condition is wrong , the $p variable data has to echo in if Block..

But I am getting the Parse error: syntax error, unexpected T_ECHO

John Saunders
  • 160,644
  • 26
  • 247
  • 397
mahesh Cholleti
  • 43
  • 1
  • 1
  • 8

1 Answers1

-1

If I got the question right you could do if(eval("return ".$p)). However evaluating user input is not recommended. php documentation for eval

Like this:

<?php 
$circle_sheet="DETAILS.csv";
$f = fopen($circle_sheet, "r");
while (($line = fgetcsv($f)) !== false)
{
    if(eval("return ".$p))
    {
        echo "<tr>";
        foreach($line as $cell)
        {   
            echo "<td style='color:white;'>" . htmlspecialchars($cell) . "</td>";
        }echo "</tr>\n";
    }
}
?>
notlisted
  • 79
  • 2
  • how to return $p value...? to if condition...? – mahesh Cholleti Jun 10 '15 at 11:26
  • eval("return ".$p) returns true if the conditions in $p are satisfied false if not. if(eval("return ".$p)) will execute the inside of the if if $p is satisfied. – notlisted Jun 10 '15 at 11:49
  • the challenge is ..how to execute the return value inside of the if condition block....can u please help me with code...? – mahesh Cholleti Jun 10 '15 at 11:58
  • Which return value? the value of $p is true if you get inside of the if. And the code in the if block get executed if $p evaluates to true. – notlisted Jun 10 '15 at 12:15
  • No..u r not getting my point ...I would like to echo the value of $p in if block so that according to the value of $p will filter the csv file... – mahesh Cholleti Jun 10 '15 at 12:17
  • if you want to echo if $p is true or not you can simply `echo "true"` in the if and `echo "false"` in an else block. Or should $p be checked for every line and just echo the lines where $p is true? – notlisted Jun 10 '15 at 12:25
  • yes..thanks for code sharing, eg : entire code is in $p = implode(' && ', $data); ... now this $p value has to call in IF() BLOCK ...but I am getting the error....Syntax error...unexpected } ..... – mahesh Cholleti Jun 10 '15 at 12:26
  • Unable to satisfy condition ...eval() in if() ...please help..? – mahesh Cholleti Jun 10 '15 at 12:38
  • Yes..if the 'IF' condition satisfies...than only the value of $p should echo in IF Block ...or else according to u ..if(eval(return $p)) is not working .. – mahesh Cholleti Jun 10 '15 at 12:41
  • in your code above you use `if(eval(return $p))` see my code again. "return " need to be a string and concated with $p which must be a string that evaluates to a boolean. can $p be evaluated to a boolean? – notlisted Jun 10 '15 at 12:41
  • yes, I do the same code if(eval("return ".$p)) there i am getting the error...Parse error..syntax error... – mahesh Cholleti Jun 10 '15 at 13:00
  • ...is there any possible to get return value without passing variable to function...? – mahesh Cholleti Jun 10 '15 at 13:04
  • Then it's likely becaue $p is malformed. Can you try var_dump $p and post what $p actually is when the error occurs? For strings like "$line[10]=='MICRO'" from your question it should work. return value without passing it to function? only if $p is a funtion that return the value itself. But for the values of $p in your question since they are strings the need to be evalueted to get the return value. – notlisted Jun 10 '15 at 13:09
  • yes, I tried var_dump($p) ..there is no error..and yes the values of $p is a strings after echoing in the value if Block it evaluates to PHP code... – mahesh Cholleti Jun 11 '15 at 04:36
  • ....sorry dude...I am asked many questions to u ....and I am very thankful to u..for giving idea ..finally I got the solution ...if(eval("return $p;")) .....this is worked ....fine...Thanks for Idea and Helpful to u... – mahesh Cholleti Jun 11 '15 at 13:30