PHP пропускает мало данных из jsonarray - PullRequest
1 голос
/ 25 октября 2019

У меня есть массив json из формы, сохраненной в базе данных. Я создаю отчет из массива. В форме есть раздел «Требуется ли этот раздел?»Если пользователь выбрал «Нет» для секции, мне нужно пропустить секцию до конца секции.

В массиве json секция начинается с

'{"type":"section","label":"Do you require this section?","req":0,"Element_Values":"No"}'

, а конец раздела -

'{"type":"section-end","label":"","req":0,"Element_Values":""}'

Если Element_Values=No, то пропустить данные до типа section-end, в противном случае ничего не делать.

Для этого я сделал цикл do {} while, и он все еще не работает:

 if($jsonArray[$jsonIndex]['type']=="section")
 {
      if($jsonArray[$jsonIndex]['Element_Values']=="")
      {
           $html.='<tr><td style="color:grey;">'
                           .'Unanswered'
                           .'</td></tr>';
      }
      else if($jsonArray[$jsonIndex]['Element_Values']=="No")
      {
           do 
           {
               continue;
           }while($jsonArray[$jsonIndex]['type']=="section-end");                
      }

  }

Мой полный код приведен ниже:

<?php
$jsonIndex=0;
$jsonArray='[{"type":"header","label":"Inspection","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"WHAT TYPE OF HOME IS THIS","Element_Values":"\"SINGLE DUPLEX\"","req":1,"choices":[{"label":"SINGLE","sel":0,"notification":0,"subOptions":[]},{"label":"SINGLE DUPLEX","sel":1,"notification":0,"subOptions":[]},{"label":"SINGLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE DUPLEX","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"description","label":"IF YOU HAVE SELECTED DUPLEX or GRANNY FLAT THE CHECKLIST IS AT THE BOTTOM FOR THE SECOND UNIT","req":0,"Element_Values":"-"},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"Yes\"","req":0,"choices":[{"label":"Yes","sel":1,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection "}]}]},'
        . '{"type":"sub-header","label":"SERVICES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Stackwork and a\/c ducts have been framed if required","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]},{"label":"N\/A","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"All drainage points in correct position","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Is power connected on site","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DUPLEX \/ GRANNY FLAT","req":0,"Element_Values":"-"},'
        . '{"type":"section","label":"Do you require this section?","req":0,"Element_Values":"No"},'
        . '{"type":"dropdown","label":"What side viewing from the street is the home you are about to inspect","Element_Values":"\"\"","req":1,"choices":[{"label":"LEFT","sel":0,"notification":0,"subOptions":[]},{"label":"RIGHT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection\n"}]}]},'
        . '{"type":"sub-header","label":"BRACING - ROOF & WALL","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Is all Wall & Roof Bracing as per approved plan and correctly installed","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Sheer blocks installed as required","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOWNPIPES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Are all downpipes installed and connected to system","Element_Values":"\"\"","req":1,"choices":[{"label":"YES","sel":0,"notification":0,"subOptions":[{"DropLabel":"Where downpipes are relocated are they 300mm below bottom course of brickwork","DropOptions":["YES","NO"],"DropSel":"","reqDrop":"0"}]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"section-end","label":"","req":0,"Element_Values":""},'
        . '{"type":"sub-header","label":"SIGN OFF","req":0,"Element_Values":"-"},'
        . '{"type":"text","label":"Signature ","req":1,"Element_Values":"fd bvf"},'
        . '{"type":"dropdown","label":"Job is ready to proceed to Mandatory Pre Linings Inspection","Element_Values":"\"PROCEED\"","req":1,"choices":[{"label":"PROCEED","sel":1,"notification":0,"subOptions":[]},{"label":"DO NOT PROCEED","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Who will be the gyprocker for this job","Element_Values":"\"vgfnari\"","req":1,"choices":[{"label":"FBI-Fornari","sel":1,"notification":0,"subOptions":[]},{"label":"MOMENTUM","sel":0,"notification":0,"subOptions":[]}]}]';
$jsonArray=json_decode($jsonArray,true);
$html="";
if(!empty($jsonArray))
{  
    for ($i=0;$i <count($jsonArray);$i++) 
    {

        if($jsonArray[$jsonIndex]['type']=="header")
        {
            $html.='<div class="header"><h3><b>'.$jsonArray[$jsonIndex]['label']. '</b></h3><hr/></div>'; 
        }
        else if($jsonArray[$jsonIndex]['type']=="sub-header")
        {
            $html.='<div class="sub-header"><h5><b>'.$jsonArray[$jsonIndex]['label'].'</b></h5></div>';
        }
        else if($jsonArray[$jsonIndex]['type']=="description")
        {
            $html.='<table style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:10px;cell-padding:20px;width:100%;min-height:100px;">'
                        . '<tbody>'
                        . '<tr>'
                        . '<td style="text-align: left;">'.$jsonArray[$jsonIndex]['label']
                        . '</td>'
                        . '</tr>'
                        . '</tbody>'
                        . '</table>';
        }

        else 
        {
            if($jsonArray[$jsonIndex]['type']=="section")
            {
                if($jsonArray[$jsonIndex]['Element_Values']=="")
                {
                    $html.='<tr><td style="color:grey;">'
                           .'Unanswered'
                           .'</td></tr>';
                }
                else if($jsonArray[$jsonIndex]['Element_Values']=="No")
                {
                    do 
                    {
                        continue;
                    }while($jsonArray[$jsonIndex]['type']=="section-end");                
                }

            }
            if($jsonArray[$jsonIndex]['type']=="signature")
            {
                    $label="Signature";
            }
            else if($jsonArray[$jsonIndex]['type']=="location")
            {
                    $label="Location";
            }
            else 
            {
                    $label=$jsonArray[$jsonIndex]['label'];
            }

            $html.='<table nobr="true" style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:5px 10px;cell-padding:10px;width:100%;">'
                       . '<tbody>'
                       . '<tr>';
            $html.='<td><label style="color:grey;font-size:12px">'.$label.'</label></td></tr>';

            if($jsonArray[$jsonIndex]['Element_Values']=="")
            {
                      $html.='<tr><td style="color:grey;">'
                               .'Unanswered'
                               .'</td></tr>';
            }
            else 
            {
                    //$html.='<tr><td>'.$jsonArray[$jsonIndex]['Element_Values'].'</td></tr>';
                    $html.='<tr><td>'.str_replace('"','', $jsonArray[$jsonIndex]['Element_Values']).'</td></tr>';
            }
            $html.='</tbody>'
         .'</table>';
        }

        $jsonIndex++;
    }    
    echo $html;
}

?>

Обновленный код:

<?php
$jsonIndex=0;
$jsonArray='[{"type":"header","label":"Inspection","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"WHAT TYPE OF HOME IS THIS","Element_Values":"\"SINGLE DUPLEX\"","req":1,"choices":[{"label":"SINGLE","sel":0,"notification":0,"subOptions":[]},{"label":"SINGLE DUPLEX","sel":1,"notification":0,"subOptions":[]},{"label":"SINGLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE DUPLEX","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"description","label":"IF YOU HAVE SELECTED DUPLEX or GRANNY FLAT THE CHECKLIST IS AT THE BOTTOM FOR THE SECOND UNIT","req":0,"Element_Values":"-"},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"Yes\"","req":0,"choices":[{"label":"Yes","sel":1,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection "}]}]},'
        . '{"type":"sub-header","label":"SERVICES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Stackwork and a\/c ducts have been framed if required","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]},{"label":"N\/A","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"All drainage points in correct position","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Is power connected on site","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DUPLEX \/ GRANNY FLAT","req":0,"Element_Values":"-"},'
        . '{"type":"section","label":"Do you require this section?","req":0,"Element_Values":"No"},'
        . '{"type":"dropdown","label":"What side viewing from the street is the home you are about to inspect","Element_Values":"\"\"","req":1,"choices":[{"label":"LEFT","sel":0,"notification":0,"subOptions":[]},{"label":"RIGHT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection\n"}]}]},'
        . '{"type":"sub-header","label":"BRACING - ROOF & WALL","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Is all Wall & Roof Bracing as per approved plan and correctly installed","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Sheer blocks installed as required","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOWNPIPES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Are all downpipes installed and connected to system","Element_Values":"\"\"","req":1,"choices":[{"label":"YES","sel":0,"notification":0,"subOptions":[{"DropLabel":"Where downpipes are relocated are they 300mm below bottom course of brickwork","DropOptions":["YES","NO"],"DropSel":"","reqDrop":"0"}]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"section-end","label":"","req":0,"Element_Values":""},'
        . '{"type":"sub-header","label":"SIGN OFF","req":0,"Element_Values":"-"},'
        . '{"type":"text","label":"Signature ","req":1,"Element_Values":"fd bvf"},'
        . '{"type":"dropdown","label":"Job is ready to proceed to Mandatory Pre Linings Inspection","Element_Values":"\"PROCEED\"","req":1,"choices":[{"label":"PROCEED","sel":1,"notification":0,"subOptions":[]},{"label":"DO NOT PROCEED","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Who will be the gyprocker for this job","Element_Values":"\"vgfnari\"","req":1,"choices":[{"label":"FBI-Fornari","sel":1,"notification":0,"subOptions":[]},{"label":"MOMENTUM","sel":0,"notification":0,"subOptions":[]}]}]';
$jsonArray=json_decode($jsonArray,true);
$html="";
if(!empty($jsonArray))
{  
    for ($i=0;$i <count($jsonArray);$i++) 
    {

        if($jsonArray[$jsonIndex]['type']=="header")
        {
            $html.='<div class="header"><h3><b>'.$jsonArray[$jsonIndex]['label']. '</b></h3><hr/></div>'; 
        }
        else if($jsonArray[$jsonIndex]['type']=="sub-header")
        {
            $html.='<div class="sub-header"><h5><b>'.$jsonArray[$jsonIndex]['label'].'</b></h5></div>';
        }
        else if($jsonArray[$jsonIndex]['type']=="description")
        {
            $html.='<table style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:10px;cell-padding:20px;width:100%;min-height:100px;">'
                        . '<tbody>'
                        . '<tr>'
                        . '<td style="text-align: left;">'.$jsonArray[$jsonIndex]['label']
                        . '</td>'
                        . '</tr>'
                        . '</tbody>'
                        . '</table>';
        }

        else 
        {
            if($jsonArray[$jsonIndex]['type']=="section")
            {
                if($jsonArray[$jsonIndex]['Element_Values']=="")
                {
                    $html.='<tr><td style="color:grey;">'
                           .'Unanswered'
                           .'</td></tr>';
                }
                else if($jsonArray[$jsonIndex]['Element_Values']=="No")
                {
                    do 
                    {
                        $jsonIndex++;
                    }while($jsonArray[$jsonIndex]['type']!="section-end");                
                }

            }
            if($jsonArray[$jsonIndex]['type']=="signature")
            {
                    $label="Signature";
            }
            else if($jsonArray[$jsonIndex]['type']=="location")
            {
                    $label="Location";
            }
            else 
            {
                    $label=$jsonArray[$jsonIndex]['label'];
            }

            $html.='<table nobr="true" style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:5px 10px;cell-padding:10px;width:100%;">'
                       . '<tbody>'
                       . '<tr>';
            $html.='<td><label style="color:grey;font-size:12px">'.$label.'</label></td></tr>';

            if($jsonArray[$jsonIndex]['Element_Values']=="")
            {
                      $html.='<tr><td style="color:grey;">'
                               .'Unanswered'
                               .'</td></tr>';
            }
            else 
            {
                    //$html.='<tr><td>'.$jsonArray[$jsonIndex]['Element_Values'].'</td></tr>';
                    $html.='<tr><td>'.str_replace('"','', $jsonArray[$jsonIndex]['Element_Values']).'</td></tr>';
            }
            $html.='</tbody>'
         .'</table>';
        }

        //$jsonIndex++;
    }    
    echo $html;
}
?>

1 Ответ

1 голос
/ 25 октября 2019

Это

do 
{
    continue;
}while($jsonArray[$jsonIndex]['type']=="section-end");

Должно быть

do 
{
  $jsonIndex++;
} while ($jsonArray[$jsonIndex]['type'] != "section-end");

Логика, необходимая для цикла, противоположна той, что была у вас. Это использовать! = Вместо ==. Кроме того, очень важно увеличить значение $ jsonIndex, иначе это будет бесконечный цикл.

Также измените ...

for ($i=0;$i <count($jsonArray);$i++)

на

for ($jsonIndex=0;$jsonIndex <count($jsonArray);$jsonIndex++)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...