0

Could anyone help me with such issue: I'm trying to make thing, that based on $_Post data I'm showing or not data in the Google tables. (I've removed unnecessary parts of code, that are not actual to this question)

currently, check looks like:

if(!empty($_POST['include_pm'])) {
      $pm_script = "data.addColumn('string', 'PM');";
      $t0 = ",'\".";
      $t1 = '$row['."'".PM."'".']';
      $t2 = ".\"'";
      $pm_ent = $t0.$t1.$t2;
    }
    else
    {
        $pm = "";   
        $pm_script = "";
        $pm_ent = "";
    }   

And in google visualization table it goes as :

<?php echo $pm_script; ?>
          data.addRows([
        <?php
            foreach ($rows as $row) { 
            echo "['".$row['TYPE']."'".$pm_ent."],";
        }
        ?>          
        ]);

So basically, when $_POST is empty, nothing is included (as well in script), and it works pretty fine, as needed.

But I'm not able to make it work, when $_POST is not empty: as far as I was experimenting(this one is my last attempt), it printed in the data table exactly string value of the pm_ent value, so it wasn't working in the code as such but became really as a string.

Maybe someone could help me with this, so it would work dynamically - If it's not empty, PM row would be added to the data Rows? As I'm not so good at PHP, I'm having lack of knowledge, how to solve this...

Or maybe there is some smarter way how to do it?

6
  • is PM variable? in $t1 = '$row['."'".PM."'".']';.. if not a variable change to $t1 = $row['PM']; Commented Jun 28, 2016 at 9:06
  • PM is an column name, which is retrieved from SQL script, which is in running background. So basically yes, it is variable Commented Jun 28, 2016 at 9:18
  • I think $t1 = '$row['."'".PM."'".']'; should probably be $t1 = $row["PM"]; Otherwise you'll be outputting the PHP code into javascript, not outputting the value of the PM row. Commented Jun 28, 2016 at 9:30
  • How about a simple json_encode...?! data.addRows(<?php echo json_encode($rows); ?>); – Never ever should you be manually assembling Javascript code snippets by hand. Commented Jun 28, 2016 at 9:35
  • Hm, and how json_encode works? because your mentioned example data.addRows(<?php echo json_encode($rows); ?>); is falling to an error: Uncaught Error: Argument given to addRows must be either a number or an array Commented Jun 28, 2016 at 9:45

1 Answer 1

1
 $pm_script = "";
if(!empty($_POST['include_pm'])) {
      $pm_script = "data.addColumn('string', 'PM');";   
    }

And in google visualization table it goes as :

<?php echo $pm_script; ?>
          data.addRows([
        <?php
            foreach ($rows as $row) {

               $pm_ent = "";
                if( $pm_script !="") 
                {

                  $pm_ent =",'".$row['PM']."'";
                }

            echo "['".$row['TYPE']."'".$pm_ent."],";
        }
        ?>          
        ]);

Hope it will help..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.