0

how can i escape this : var tab_mois_nb_match = <?php ".json_encode($tab_mois_nb_match)." ;?> ;

I have an code error, but the arrays are generate with sussess in the conle.log, it's totally crazy.

 foreach($tab_bases2 as $key => $univers){              
            $tab_nb_match_par_user = users_nb_match($univers);
            $tab_mois_nb_match = mois_nb_match($univers);

            echo "<div id='".$univers."' ></div>";
            echo "<script type='text/JavaScript'> 
                    var tab_mois_nb_match = <?php ".json_encode($tab_mois_nb_match)." ;?> ;
                    var line3 = [];tab_date = [];
                    for(var v in tab_mois_nb_match){
                        line3.push([v,Number(tab_mois_nb_match[v])]);
                        tab_date.push(v[0]+v[1]+v[2]+v[3]);
                    }
                    console.log(tab_mois_nb_match);
                  </script>";
         }

2 Answers 2

2

You are mixing inline PHP with a PHP command (echo).

When you are echoing a string, you do it just like normal, this means you can mix literal strings (the js you are manually typing) and the output of functions (like a json in this case):

echo "<script type='text/JavaScript'> 
                var tab_mois_nb_match = ".json_encode($tab_mois_nb_match)." ;
                var line3 = [];tab_date = [];
                for(var v in tab_mois_nb_match){
                    line3.push([v,Number(tab_mois_nb_match[v])]);
                    tab_date.push(v[0]+v[1]+v[2]+v[3]);
                }
                console.log(tab_mois_nb_match);
              </script>";

A string is a string.

Edit: On that note though, I should add, if you need something to be evaluated PRIOR to being inserted into a string, just drop some brackets around it.

echo "<script type='text/JavaScript'> 
                var tab_mois_nb_match = ".(json_encode($tab_mois_nb_match)+4)." ;
                var line3 = [];tab_date = [];
                for(var v in tab_mois_nb_match){
                    line3.push([v,Number(tab_mois_nb_match[v])]);
                    tab_date.push(v[0]+v[1]+v[2]+v[3]);
                }
                console.log(tab_mois_nb_match);
              </script>";

Okay, so +4 is a bad example, but say for example, you needed the output of a function inserted into another function... brackets are the ones that do the trick.

Lastly, not ALL functions will work like this. Sometimes, you just have to save the output of a function into a variable and then insert the variable into the string.

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

1 Comment

thanks man , I tried this var tab_mois_nb_match = ' ' '".json_encode($tab_mois_nb_match)." ' ; and I was lost
2

You cannot declare php inside php.

You can write your code in two ways:

First Method:

 foreach($tab_bases2 as $key => $univers){              
        $tab_nb_match_par_user = users_nb_match($univers);
        $tab_mois_nb_match = mois_nb_match($univers);

        echo "<div id='".$univers."' ></div>";
?>


<script type='text/JavaScript'> 
                var tab_mois_nb_match = <?php json_encode($tab_mois_nb_match) ;?> ;
                var line3 = [];tab_date = [];
                for(var v in tab_mois_nb_match){
                    line3.push([v,Number(tab_mois_nb_match[v])]);
                    tab_date.push(v[0]+v[1]+v[2]+v[3]);
                }
                console.log(tab_mois_nb_match);
              </script>

<?php                  
     }
?>

Second Method

foreach($tab_bases2 as $key => $univers){              
        $tab_nb_match_par_user = users_nb_match($univers);
        $tab_mois_nb_match = mois_nb_match($univers);

        echo "<div id='".$univers."' ></div>";
        echo "<script type='text/JavaScript'> 
                var tab_mois_nb_match = ".json_encode($tab_mois_nb_match)."  ;
                var line3 = [];tab_date = [];
                for(var v in tab_mois_nb_match){
                    line3.push([v,Number(tab_mois_nb_match[v])]);
                    tab_date.push(v[0]+v[1]+v[2]+v[3]);
                }
                console.log(tab_mois_nb_match);
              </script>";
     }

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.