Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Convert javascript code to php

I'm trying to move the following logic to the controller because it makes my page extremely slow and I would like to optimize it if possible as well, I'm not the original author so I really can't say much about why the code is the way it is, but I'm sure it can be optimized. What the code is doing is: take survey questions and counting the answers and basically setting the data up to use with vue charts.js.

This is the code in js

let count = 1;
this.survey_question.map((item) => {
    item.count = count;
    count++;
    return item;
});

this.survey_question.map((question) => {
    if (question.response_type_id === 1) {
        let count = 0;
        let num = 0;
        question.answer.map((answer_pager) => {
            count++;
            answer_pager.count = count;
        });
        question.answer.map((answer) => {
            num++;
        });
        question.num = num;
    }
    if (question.response_type_id === 2) {
        let count = 0;
        let option = null;
        this.data = [];
        question.answer.map((answer) => {
            let record = 0;
            question.answer.map((answers) => {
                this.data.push(answers.answer);

            });
            this.undata = this.data.filter((valor, indiceActual, arreglo) => arreglo.indexOf(valor) === indiceActual);
            option = answer.survey_question.survey_question_option.map((options) => {
                this.label.push(options.option);
                this.data.forEach((evaluator) => {
                    if (options.id == evaluator) {
                        record++;
                    }
                });
                this.recording.push(record);
                record = 0;
                answer.count = count;
                if (count === question.answer.length) {
                    answer = answer.data = {
                        labels: this.label,
                        datasets: [{
                            labels: 'Resultado',
                            backgroundColor: this.colors,
                            data: this.recording,
                        }]
                    }

                }
                count++;
            });
            this.recording = [];
            this.data = [];
            this.label = [];
        });
    }
    if (question.response_type_id === 3) {
        let count = 1;
        let answers = null;
        let uno = 0;
        let dos = 0;
        let tres = 0;
        let cuatro = 0;
        let cinco = 0;
        let seis = 0;
        question.answer.map((answer) => {
            if (question.rank === 3) {
                if (answer.answer === '1') {
                    uno++;
                }
                if (answer.answer === '2') {
                    dos++;
                }
                if (answer.answer === '3') {
                    tres++;
                }
                if (question.answer.length === count) {
                    answers = answer.data = {
                        labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas'],
                        datasets: [{
                            label: 'Resultado',
                            backgroundColor: this.colors,
                            data: [uno, dos, tres]
                        }]
                    }

                }
            }
            if (question.rank === 4) {
                if (answer.answer === '1') {
                    uno++;
                }
                if (answer.answer === '2') {
                    dos++;
                }
                if (answer.answer === '3') {
                    tres++;
                }
                if (answer.answer === '4') {
                    cuatro++;
                }
                if (question.answer.length === count) {
                    answers = answer.data = {
                        labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas', 'Cuatro estrellas'],
                        datasets: [{
                            label: 'Resultado',
                            backgroundColor: this.colors,
                            data: [uno, dos, tres, cuatro]
                        }]
                    }

                }
            }
            if (question.rank === 5) {
                if (answer.answer === '1') {
                    uno++;
                }
                if (answer.answer === '2') {
                    dos++;
                }
                if (answer.answer === '3') {
                    tres++;
                }
                if (answer.answer === '4') {
                    cuatro++;
                }
                if (answer.answer === '5') {
                    cinco++;
                }
                if (question.answer.length === count) {
                    answers = answer.data = {
                        labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas', 'Cuatro estrellas',
                            'Cinco Estrellas'
                        ],
                        datasets: [{
                            label: 'Resultado',
                            backgroundColor: this.colors,
                            data: [uno, dos, tres, cuatro, cinco]
                        }]
                    }

                }
            }
            if (question.rank === 6) {

                if (answer.answer === '1') {
                    uno++;
                }
                if (answer.answer === '2') {
                    dos++;
                }
                if (answer.answer === '3') {
                    tres++;
                }
                if (answer.answer === '4') {
                    cuatro++;
                }
                if (answer.answer === '5') {
                    cinco++;
                }
                if (answer.answer === '6') {
                    seis++;
                }
                if (question.answer.length === count) {
                    answers = answer.data = {
                        labels: ['Una estrella', 'Dos estrellas', 'Tres estrellas', 'Cuatro estrellas',
                            'Cinco Estrellas', 'Seis Estrellas'
                        ],
                        datasets: [{
                            label: 'Resultado',
                            backgroundColor: this.colors,
                            data: [uno, dos, tres, cuatro, cinco, seis]
                        }]
                    }

                }
            }
            question.data = answers;
            count++;

        });
    }
    if (question.response_type_id === 4) {
        let answers = null;
        let count = 1;
        let countNo = 0;
        let countYes = 0;
        question.answer.map((answer) => {
            if (answer.answer === 'No') {
                countNo++;
            } else {
                countYes++;
            }
            if (question.answer.length === count) {
                answers = answer.data = {
                    labels: ['Si', 'No'],
                    datasets: [{
                        label: 'Resultado',
                        backgroundColor: this.colors,
                        data: [countYes, countNo]
                    }]
                };

            }
            question.data = answers;
            count++;
        });
    }
});

There are 4 types of questions which are text (responese_type_id = 1), multiple choice (responese_type_id = 2), ranking (responese_type_id = 3) and yes or no (responese_type_id = 4).

How can I optimize this code and convert it to php code for the controller?

0 likes
5 replies
eggplantSword's avatar

@bugsysha why not? the way it is now is unbearably slow, the page freezes like 3 times, and it takes over 5 minutes just to load. Also I'm not a subscriber so I can't watch the video.

tinfoilman's avatar

I don't understand how that could take 5 minutes to load, unless it's being looped over and over with lots of questions. We need more of the code to know what's going on. Also can't really give advice on what to do in a controller without seeing the inputs.

eggplantSword's avatar

@tinfoilman That's the issue this code is looped over each question and this function is for filtering surveys and for this particular filter there are 39 questions, but realistically it could be in the hundreds, I don't know why it takes so long.

I think I'm close to converting the code, this is what I have so far in the controller, the $item is each question

foreach($f as $item) {
    if ($item -> response_type_id == 2) {
        $labels = collect([]);
        foreach($item['surveyQuestionOption'] as $option) {
            $labels[] = $option - > option;
            foreach($answer as $d) {
                if ($option -> id == $d -> answer) {
                    $record++;
                }
            }
        }
        $recording[] = $record;

        if (count($answer) == $totalAnswered) {
            $itemData = [
                'labels' => $labels,
                'datasets' => [
                    'labels' => 'Resultado',
                    'backgroundColor' => $colors,
                    'data' => $recording
                ]
            ];
        }
        $recording = collect([]);
        $record = 0;

    } else if ($item -> response_type_id == 3) {
        $uno = null;
        $dos = null;
        $tres = null;
        $cuatro = null;
        $cinco = null;
        $sies = null;

        foreach($answer as $ans) {
            if ($item -> rank == 3) {
                if ($ans -> answer == 1) {
                    $uno++;
                }
                if ($ans -> answer == 2) {
                    $dos++;
                }
                if ($ans -> answer == 3) {
                    $tres++;
                }

                if (count($answer) == $totalAnswered) {
                    $itemData = [
                        'labels' => ['Una Estrella', 'Dos Estrellas', 'Tres Estrellas'],
                        'datasets' => [
                            'labels' => 'Resultado',
                            'backgroundColor' => $colors,
                            'data' => [$uno, $dos, $tres]
                        ]
                    ];
                }

            }
            if ($item -> rank == 4) {
                if ($ans -> answer == 1) {
                    $uno++;
                }
                if ($ans -> answer == 2) {
                    $dos++;
                }
                if ($ans -> answer == 3) {
                    $tres++;
                }
                if ($ans -> answer == 4) {
                    $cuatro++;
                }

                if (count($answer) == $totalAnswered) {
                    $itemData = [
                        'labels' => ['Una Estrella', 'Dos Estrellas', 'Tres Estrellas', 'Cuatro Estrellas'],
                        'datasets' => [
                            'labels' => 'Resultado',
                            'backgroundColor' => $colors,
                            'data' => [$uno, $dos, $tres, $cuatro]
                        ]
                    ];
                }
            }
            if ($item -> rank == 5) {
                if ($ans -> answer == 1) {
                    $uno++;
                }
                if ($ans -> answer == 2) {
                    $dos++;
                }
                if ($ans -> answer == 3) {
                    $tres++;
                }
                if ($ans -> answer == 4) {
                    $cuatro++;
                }
                if ($ans -> answer == 5) {
                    $cinco++;
                }

                if (count($answer) == $totalAnswered) {
                    $itemData = [
                        'labels' => ['Una Estrella', 'Dos Estrellas', 'Tres Estrellas',
                            'Cuatro Estrellas', 'Cinco Estrellas'
                        ],
                        'datasets' => [
                            'labels' => 'Resultado',
                            'backgroundColor' => $colors,
                            'data' => [$uno, $dos, $tres, $cuatro, $cinco]
                        ]
                    ];
                }
            }
            if ($item -> rank == 6) {
                if ($ans -> answer == 1) {
                    $uno++;
                }
                if ($ans -> answer == 2) {
                    $dos++;
                }
                if ($ans -> answer == 3) {
                    $tres++;
                }
                if ($ans -> answer == 4) {
                    $cuatro++;
                }
                if ($ans -> answer == 5) {
                    $cinco++;
                }
                if ($ans -> answer == 6) {
                    $sies++;
                }

                if (count($answer) == $totalAnswered) {
                    $itemData = [
                        'labels' => ['Una Estrella', 'Dos Estrellas', 'Tres Estrellas',
                            'Cuatro Estrellas', 'Cinco Estrellas', 'Sies Estrellas'
                        ],
                        'datasets' => [
                            'labels' => 'Resultado',
                            'backgroundColor' => $colors,
                            'data' => [$uno, $dos, $tres, $cuatro, $cinco, $sies]
                        ]
                    ];
                }
            }
        }

    } else if ($item -> response_type_id == 4) {
        $countNo = 0;
        $countYes = 0;

        foreach($answer as $ans) {
            if ($ans -> answer == 'No') {
                $countNo++;
            } else {
                $countYes++;
            }

            if (count($answer) == $totalAnswered) {
                $itemData = [
                    'labels' => ['Si', 'No'],
                    'datasets' => [
                        'labels' => 'Resultado',
                        'backgroundColor' => $colors,
                        'data' => [$countYes, $countNo]
                    ]
                ];
            }
        }
    }

}

$i = $f[0];
$i['totalAnswered'] = $totalAnswered;
$i['itemData'] = $itemData;
$i['answer'] = $answer;
$items[] = $i;

Basically the end goal is to have in the $itemData something like this, right now I'm trying to get the multiple choice (response_type_id = 2) working, the data being the count of answers that belong to that label

{
   "labels":[
      "asdfas1",
      "fasdfsfs2",
      "asdfasf3"
   ],
   "datasets":[
      {
         "labels":"Resultado",
         "backgroundColor":["#1E3260", "#00B9FF", "#04FF00", "#FBB437", "#F7FF00", "#D4D4D4", "#00FF0C", "#000000", "#006E60", "#FFFF00", "#00F3FF", "#FF00FF",  "#7A3F3F", "#D4FF00", "#FF9300", "#FF6161", "#8361FF", "#61C3FF", "#007EBD", "#00BABD", "#532400", "#00DFA2", "#C89C8D",  "#53f545", "#f5455d", "#3aab30", "#39678c", "#8c6339"
         ],
         "data":[ 2, 2, 1]
      }
   ]
}

I'm really close I'm getting this

{
   "labels":[
      "asdfas1",
      "fasdfsfs2",
      "asdfasf3"
   ],
   "datasets":{
      "labels":"Resultado",
      "backgroundColor":["#1E3260", "#00B9FF", "#04FF00", "#FBB437", "#F7FF00", "#D4D4D4",  "#00FF0C",  "#000000", "#006E60", "#FFFF00", "#00F3FF", "#FF00FF", "#7A3F3F", "#D4FF00", "#FF9300", "#FF6161", "#8361FF",  "#61C3FF", "#007EBD", "#00BABD", "#532400", "#00DFA2", "#C89C8D", "#53f545", "#f5455d", "#3aab30", "#39678c", "#8c6339"
      ],
      "data":[ 5 ]
   }
}

Please or to participate in this conversation.