0

I'm try to use jsondata to display the results.
This is my output. Now i just try to display the result

enter image description here

public function resultsurvey($survey_id)
    {
        //$results=[];

        $allResults= Results::where('survey_id', $survey_id)->get()->toArray(); 
        $justResults=Results::select('results')->where('survey_id', $survey_id)->get();

        $json = json_decode($justResults, true);


        $aQuestions = array();
        $aAnswers = array();
        foreach($json as $sKey => $oItem) {
            array_push($aQuestions, $sKey);
            array_push($aAnswers, $oItem);
        }

        dd($aQuestions , $aAnswers);
}

in pure php i just use a but in the laravel it's not work.

<div class="container"> 
    <table class="table table-striped">
      <thead>
        <tr>
        <?php foreach($aQuestions as $aQuestionsn){?>
          <th scope="col"><?php echo $aQuestionsn; ?></th>
        <?php }?>
        </tr>
      </thead>
      <tbody>
        <tr>
        <?php foreach($aAnswers as $aAnswersn) {?>
            <td><?php echo $aAnswersn;?></td>   
        <?php }?>
        </tr>
      </tbody>
    </table>
</div>  

How i can display the jsonstring ?

All i need look like this

enter image description here

4
  • use $json = json_encode($justResults, JSON_HEX_QUOT | JSON_HEX_APOS); instead of $json = json_decode($justResults, true); if you really need to make any json data. BTW why do you need json in your case? You can just pass the array from controller to view. Commented Nov 19, 2018 at 10:10
  • look at my edit It's will answer your questions Commented Nov 19, 2018 at 10:18
  • How does your data column look like when it is saved? If it's escaped with \" it probably means your JSON is encoded twice. Commented Nov 19, 2018 at 10:18
  • I just saved the string. Commented Nov 19, 2018 at 10:20

2 Answers 2

3

well i think in your case there is another array inside your array you need to go inside that array

foreach($aAnswers as $aAnswersn)
{
foreach ($aAnswersn as $value)
{
echo $value;
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

At least your json_encode is possibly in the wrong location, try this:

public function resultsurvey($survey_id)
{
    //$results=[];

    $allResults= Results::where('survey_id', $survey_id)->get()->toArray(); 
    $justResults=Results::select('results')->where('survey_id', $survey_id)->get();

    $aQuestions = array();
    $aAnswers = array();
    // $justResults is a collection so loop through it
    foreach($justResults as $sKey => $oItem) {
        // $oItem is possibly JSON encoded at this stage
        $oItem = json_decode($oItem, true);
        array_push($aQuestions, $sKey);
        array_push($aAnswers, $oItem);
    }

    dd($aQuestions , $aAnswers);
}

Please note all changes

1 Comment

I can't display my code is <div class="container"> <table class="table table-striped"> <thead> <tr> <?php foreach($aQuestions as $aQuestionsn){?> <th scope="col"><?php echo $aQuestionsn; ?></th> <?php }?> </tr> </thead> <tbody> <tr> <?php foreach($aAnswers as $aAnswersn) {?> <td><?php echo $aAnswersn;?></td> <?php }?> </tr> </tbody> </table> </div>

Your Answer

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