如何显示数组中特定项目的计数? Laravel刀片

时间:2019-12-10 16:14:16

标签: laravel-blade

我正在做一个报告,向用户显示调查结果的摘要。 有4种类型的问题:文本,是或否,多项选择和排名。 对于每个问题,除了文字,我想说-- working case SELECT * FROM MyTable WHERE CONTAINS(MyColumn, ' "red*" NEAR "color*" ') -- failed case SELECT * FROM MyTable WHERE CONTAINS(MyColumn, ' "the*" NEAR "red*" ') ,等等。

这是我从控制器发送的数据

x amount of people selected this option

这是它提供的json编码数据,很长,所以我不会全部显示,它总共有21个答案和6个问题,我每个显示2个。

 $survey = Survey::with('answer', 'surveyQuestions.responseType', 'surveyQuestions.surveyQuestionOption')
            ->where('id', $id)->first();

现在我要显示这些问题的答案

{ 
   "id":1,
   "name":"Evaluacion al docente por parte de los estudiantes",
   "description":"Evaluacion al docente de la clase de Electronica 1 de la carrera de Ingenieria",
   "user_id":1,
   "anonymous":0,
   "created_at":"2019-12-09 10:30:50",
   "updated_at":"2019-12-10 08:43:40",
   "answer":[ 
      { 
         "id":1,
         "survey_id":1,
         "survey_question_id":1,
         "user_id":2,
         "answer":"muy bien",
         "created_at":"2019-12-10 08:25:12",
         "updated_at":"2019-12-10 08:25:12"
      },
      { 
         "id":2,
         "survey_id":1,
         "survey_question_id":2,
         "user_id":2,
         "answer":"1",
         "created_at":"2019-12-10 08:25:12",
         "updated_at":"2019-12-10 08:25:12"
      },
 ],
   "survey_questions":[ 
      { 
         "id":1,
         "question":"Cual es su opinion acerca de la oficina de telematica",
         "user_id":1,
         "survey_section_id":1,
         "response_type_id":1,
         "optional":0,
         "num":null,
         "rank":null,
         "show_text":0,
         "created_at":"2019-12-09 10:29:27",
         "updated_at":"2019-12-10 08:20:50",
         "pivot":{ 
            "survey_id":1,
            "survey_question_id":1
         },
         "response_type":{ 
            "id":1,
            "type":"Texto"
         },
         "survey_question_option":[ 

         ]
      },
      { 
         "id":2,
         "question":"elija 3 opciones",
         "user_id":1,
         "survey_section_id":1,
         "response_type_id":2,
         "optional":0,
         "num":3,
         "rank":null,
         "show_text":0,
         "created_at":"2019-12-10 08:21:21",
         "updated_at":"2019-12-10 08:21:21",
         "pivot":{ 
            "survey_id":1,
            "survey_question_id":2
         },
         "response_type":{ 
            "id":2,
            "type":"Selecci\u00f3n M\u00faltiple"
         },
         "survey_question_option":[ 
            { 
               "id":1,
               "option":"opcion a",
               "survey_question_id":2,
               "created_at":null,
               "updated_at":null
            },
            { 
               "id":2,
               "option":"opcion b",
               "survey_question_id":2,
               "created_at":null,
               "updated_at":null
            },
            { 
               "id":3,
               "option":"opcion c",
               "survey_question_id":2,
               "created_at":null,
               "updated_at":null
            },
            { 
               "id":4,
               "option":"opcion d",
               "survey_question_id":2,
               "created_at":null,
               "updated_at":null
            }
         ]
      },
   ]
}

当前输出是多项选择

@foreach($survey['surveyQuestions'] as $question)
    <div>
        <p>question: {{$question}}</p>
        <p class="q_name">{{$question->question}}</p>

        @if($question->response_type_id == 2)
            <p>Seleccion Multiple</p>
            <p>Opciones</p>
            @foreach($question['surveyQuestionOption'] as $option)
                <p>{{$option->option}}</p>
            @endforeach

            @foreach($survey['answer'] as $answer)
                @if($answer->survey_question_id == $question->id)
                    {{$answer}}
                @endif
            @endforeach
        @endif
        @if($question->response_type_id == 3)
            <p>Rango</p>
            @foreach($survey['answer'] as $answer)
                @if($answer->survey_question_id == $question->id)
                    <p>{{$answer}}</p>
                @endif
            @endforeach
        @endif
        @if($question->response_type_id == 4)
            <p>Si o No</p>
            @foreach($survey['answer'] as $answer)
                @if($answer->survey_question_id == $question->id)
                    <p>{{$answer}}</p>
                @endif
            @endforeach
        @endif
    </div>
@endforeach

@if($question->response_type_id == 2)
    <p>Seleccion Multiple</p>
    <p>Opciones</p>
    @foreach($question['surveyQuestionOption'] as $option)
        <p>{{$option->option}}</p>
    @endforeach

    @foreach($survey['answer'] as $answer)
        @if($answer->survey_question_id == $question->id)
            {{$answer}}
        @endif
    @endforeach
@endif

排名问题的输出

elija 3 opciones
Seleccion Multiple
Opciones
opcion a
opcion b
opcion c
opcion d
{"id":2,"survey_id":1,"survey_question_id":2,"user_id":2,"answer":"1","created_at":"2019-12-10
08:25:12","updated_at":"2019-12-10 08:25:12"}
{"id":8,"survey_id":1,"survey_question_id":2,"user_id":3,"answer":"3","created_at":"2019-12-10
08:25:59","updated_at":"2019-12-10 08:25:59"}
{"id":14,"survey_id":1,"survey_question_id":2,"user_id":4,"answer":"4","created_at":"2019-12-10
08:28:17","updated_at":"2019-12-10 08:28:17"}
{"id":20,"survey_id":1,"survey_question_id":2,"user_id":5,"answer":"4","created_at":"2019-12-10
08:28:51","updated_at":"2019-12-10 08:28:51"}

@if($question->response_type_id == 3)
    <p>Rango</p>
    @foreach($survey['answer'] as $answer)
        @if($answer->survey_question_id == $question->id)
            <p>{{$answer}}</p>
        @endif
    @endforeach
@endif

是或否的输出

jaksjakjf fjf a jaskj fkf ja range
Rango
{"id":6,"survey_id":1,"survey_question_id":6,"user_id":2,"answer":"3","created_at":"2019-12-10
08:25:12","updated_at":"2019-12-10 08:25:12"}
{"id":12,"survey_id":1,"survey_question_id":6,"user_id":3,"answer":"3","created_at":"2019-12-10
08:25:59","updated_at":"2019-12-10 08:25:59"}
{"id":18,"survey_id":1,"survey_question_id":6,"user_id":4,"answer":"3","created_at":"2019-12-10
08:28:17","updated_at":"2019-12-10 08:28:17"}

@if($question->response_type_id == 4)
    <p>Si o No</p>
    @foreach($survey['answer'] as $answer)
        @if($answer->survey_question_id == $question->id)
            <p>{{$answer}}</p>
        @endif
    @endforeach
@endif

我知道这是很多数据,但是我的问题是, 有没有一种方法可以计算出相同的答案,并在选项旁边显示这些结果。

bota la basura en los basureros?
Si o No
{"id":4,"survey_id":1,"survey_question_id":3,"user_id":2,"answer":"No","created_at":"2019-12-10
08:25:12","updated_at":"2019-12-10 08:25:12"}
{"id":10,"survey_id":1,"survey_question_id":3,"user_id":3,"answer":"Si","created_at":"2019-12-10
08:25:59","updated_at":"2019-12-10 08:25:59"}
{"id":16,"survey_id":1,"survey_question_id":3,"user_id":4,"answer":"No","created_at":"2019-12-10
08:28:17","updated_at":"2019-12-10 08:28:17"}

0 个答案:

没有答案