1
        $currentLanguage = Language::get();
    $programLoader = new ProgramLoaderFood();

    $allPrograms = $programLoader->getAllFoodPrograms(); 
    $allCousines = $programLoader->getCousineListOfProgram(); 
    $allLocations = $programLoader->getAllRestaurantLocations(); 
    $allTextFood = $programLoader->getAllTextFood(); 

    $jsonAllPrograms = json_encode($allPrograms);
    $jsonAllCousines = json_encode($allCousines);
    $jsonAllLocations = json_encode($allLocations);
    $jsonAllTextFood = json_encode($allTextFood); 

I have this piece of code at the start of my page. I need to load in data through a php function (which gets data from our mysql database. I load in the data put it in a class and add it to an array.)

when my page is loaded (mvc pattern) i try to dynamically load in some info about tickets of our event by using javascript (DOM). I try to echo the json_encode into a js variable :

<script> 
    var allProgramsInfo = <?php echo $jsonAllPrograms ?>;
    var allCousinesInfo = <?php echo $jsonAllCousines ?>;
    var allLocationInfo = <?php echo $jsonAllLocations ?>;
    var allFoodTextInfo = <?php echo $jsonAllTextFood ?>;
    var currentLanguage = "English";
    insertAllPrograms(allProgramsInfo,allCousinesInfo,allLocationInfo,currentLanguage);
</script>

All of them work except the "var allFoodTextInfo" doesn't work. I get this error : Uncaught SyntaxError: Unexpected token ';' I tried different ways of working around this but I just can't find the solution.

the function that loads in the text info :

public function getAllTextFood() {
        //string $query, string $datatypes, array $parameters
        $parameters = array(4);
        $result = $this->ExecuteSelectQuery("SELECT * FROM www_text WHERE eventID = ?", "i",$parameters);
        $allText = [];
        while ($textInfo = $result->fetch_object()) {
            $loadedText = new CousineTextInfoFormat();
            $loadedText->textId = $textInfo->textID;
            $loadedText->textTitel = $textInfo->textTitel;
            $loadedText->textContent = $textInfo->textContent;
            $loadedText->textLanguage = $textInfo->textLanguage;

            $allText[] = $loadedText;
        }

        return $allText;
    }

I just don't understand why it works for all the other variables and for the last one it doesn't...

**edit for example this parsing the result of this function does work :

        public function getAllFoodPrograms(){
        //string $query, string $datatypes, array $parameters
        $parameters = array(4);
        $result = $this->ExecuteSelectQuery("SELECT * FROM evt_programme WHERE eventID = ?", "i",$parameters);
        $allPrograms = [];
        while ($programInfo = $result->fetch_object()) {
            $loadedProgram = new ProgramFoodFormat();
            $loadedProgram->programID = $programInfo->programmeID;
            $loadedProgram->eventID = $programInfo->eventID;
            $loadedProgram->programDate = $programInfo->programmeStart;
            $loadedProgram->programName = $programInfo->programmeHallVenue;
            $loadedProgram->programPrice = $programInfo->programmePrice;
            $loadedProgram->programTickets = $programInfo->programmeTicketSupplySeats;

            $allPrograms[] = $loadedProgram;
        }


        return $allPrograms;
    }
Mrjoker
  • 11
  • 3
  • What is the JSON that is echoed? – AbraCadaver Jan 09 '20 at 13:29
  • 1
    I would suggest dumping `$allTextFood` so you can see how the data looks before you try to encode it. either `var_dump($allTextFood);` or `print_r($allTextFood);` it looks like something in the data is an issue so it would be useful to see this – CloudTheWolf Jan 09 '20 at 13:34
  • it is filled with info i checked it with an echo on var_dump($var) – Mrjoker Jan 09 '20 at 13:37
  • Look at the echoed json in your browser's source, and then try to validate it with https://jsonlint.com – aynber Jan 09 '20 at 13:40
  • well I found out that the json parse indeed is empty bool (false) ... But I can't understand what causes this . It might be the language collation that is set in the sql database..? – Mrjoker Jan 09 '20 at 13:44
  • We don't know what `$programLoader->getAllTextFood();` looks like, but that would be a good place to start. – aynber Jan 09 '20 at 13:45
  • @aynber I included it. – Mrjoker Jan 09 '20 at 13:50
  • But It looks like language utf conversion isn't the problem. The funny thing is though that when I json_encode all the other loaded info it does work (and it is all done is the same way as $loadedText ) but for some weird reason it just doesn't work... – Mrjoker Jan 09 '20 at 13:51
  • If `$allTextFood` contains values but `$jsonAllTextFood` does not, check for [json_last_error](https://www.php.net/manual/en/function.json-last-error.php) or [json_last_error_msg](https://www.php.net/manual/en/function.json-last-error-msg.php) to see why it won't encode. – aynber Jan 09 '20 at 14:02
  • @aynber it return me the integer 5 . I found this on google : 5 JSON_ERROR_UTF8 – Mrjoker Jan 09 '20 at 14:06
  • That means your data is corrupt or incorrectly encoded and cannot be interpreted as valid UTF-8 text. You need to fix the broken data (and then ideally fix the thing which caused it to get corrupted in the first place. [This article](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) might help you with that process.). – ADyson Jan 09 '20 at 14:07
  • switching to utf8mb4 didn't do it for me... could it be that all the data has to be manually insert again? – Mrjoker Jan 09 '20 at 14:13
  • Like I said, firstly you need to fix the data which is already corrupted. Then you need to make your application fully UTF-8 compatible. Both steps are required. Simply changing the database encoding doesn't fix problems with data which already exists, it only helps with future usage. – ADyson Jan 09 '20 at 14:54

0 Answers0